Three Kingdoms(优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=3442
题意:ABCD有各自的攻击力与攻击范围,刘备只能走"C"与".",问刘备从"s" 走到"!"受到的最小攻击力。ps(受过一次攻击的下次将不会再对刘备造成伤害)
思路:用优先队列,每次搜索攻击力最小的,并对当前的状态标记。。。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <queue>
#include <stdlib.h>
using namespace std;
const int N=;
char Map[N][N];
int a[N][N][],vis[N][N][];
int dir[][] = {{,-},{,},{-,},{,}};
int n,m,sx,sy,ex,ey;
struct node
{
int x,y,hp,state;
friend bool operator < (node a,node b)
{
return b.hp < a.hp;//在这跪了n遍。。要想优先队列里的值从小到大排列,则比较函数应该从大到小排
}
};
bool in(int x,int y)
{
if (x >= &&x <= n&&y >= &&y <= m)
return true;
return false;
}
bool judge(int x1,int y1,int x2,int y2,int c)
{
if(abs(x1-x2)+abs(y1-y2) <= c)
return true;
return false;
} void deal(int x,int y,char ch)
{
if (ch=='A')
{
for (int i = x-; i <= x+; i++)
{
for (int j = y-; j <= y+; j++)
{
if(in(i,j)&&judge(x,y,i,j,))
a[i][j][] = ;
}
}
}
else if (ch=='B')
{
for (int i = x-; i <= x+; i++)
{
for (int j = y-; j <= y+; j++)
{
if (in(i,j)&&judge(x,y,i,j,))
a[i][j][] = ;
}
}
}
else if (ch=='C')
a[x][y][] = ;
else if (ch=='D')
{
for (int i = x-; i <= x+; i++)
{
for (int j = y-; j <= y+; j++)
{
if (in(i,j)&&judge(x,y,i,j,))
a[i][j][] = ;
}
}
}
else if (ch=='E')
{
for (int i = x-; i <= x+; i ++)
{
for (int j = y-; j <= y+; j ++)
{
if (in(i,j)&&judge(x,y,i,j,))
a[i][j][] = ;
}
}
}
}
int bfs()
{
priority_queue<node>q;
while(!q.empty()) q.pop();
q.push((struct node)
{
sx,sy,,
});
vis[sx][sy][] = ;
while(!q.empty())
{
node temp,t = q.top();
q.pop();
printf("%d %d %d\n",t.x,t.y,t.hp);
if (t.x==ex&&t.y==ey)
return t.hp;
for (int i = ; i < ; i++)
{
temp.x = t.x+dir[i][];
temp.y = t.y+dir[i][];
temp.hp = t.hp;
temp.state = t.state;
if (in(temp.x,temp.y)&&(Map[temp.x][temp.y]=='C'||Map[temp.x][temp.y]=='$'||Map[temp.x][temp.y]=='.'||Map[temp.x][temp.y]=='!'))
{
for (int k = ; k < ; k++)
{
if((temp.state&(<<k))==&&a[temp.x][temp.y][k])
{
temp.hp+=a[temp.x][temp.y][k];
temp.state+=(<<k);
}
}
if (!vis[temp.x][temp.y][temp.state])
{
vis[temp.x][temp.y][temp.state] = ;
q.push(temp);
}
}
}
}
return -;
}
int main()
{
int t,o = ;
cin>>t;
while((t--))
{
o++;
cin>>n>>m;
memset(a,,sizeof(a));
memset(vis,,sizeof(vis));
for (int i = ; i <= n; i++)
{
for (int j = ; j <= m; j++)
{
cin>>Map[i][j];
if (Map[i][j]=='A'||Map[i][j]=='B'||Map[i][j]=='C'||Map[i][j]=='D'||Map[i][j]=='E')
deal(i,j,Map[i][j]);
if (Map[i][j]=='$')
{
sx = i;
sy = j;
}
if (Map[i][j]=='!')
{
ex = i;
ey = j;
}
}
}
int ans = bfs();
printf("Case %d: %d\n",o,ans);
}
return ;
}
Three Kingdoms(优先队列+bfs)的更多相关文章
- hdu 1026 Ignatius and the Princess I【优先队列+BFS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- ZOJ 649 Rescue(优先队列+bfs)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 【POJ3635】Full Tank 优先队列BFS
普通BFS:每个状态只访问一次,第一次入队时即为该状态对应的最优解. 优先队列BFS:每个状态可能被更新多次,入队多次,但是只会扩展一次,每次出队时即为改状态对应的最优解. 且对于优先队列BFS来说, ...
- Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]
题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- 【UESTC 482】Charitable Exchange(优先队列+bfs)
给你n个物品交换,每个交换用r,v,t描述,代表需要用r元的东西花费t时间交换得v元的东西.一开始只有1元的东西,让你求出交换到价值至少为m的最少时间代价.相当于每个交换是一条边,时间为边权,求走到价 ...
- cdoj 482 优先队列+bfs
Charitable Exchange Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Othe ...
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
随机推荐
- linux vim 常用操作
vim 字符级 上k下j左h右i,键盘的方向键也可以移动 单词级 b上个单词首字母 w下个单词首字母 e下个单词的尾字母 行级 0行首 $行尾 删除 dd 删除光标所在行 文档级 gg 文档首行,首个 ...
- 支持向量机(SVM)原理浅析
因为网页博客输入公式很麻烦,所以就在word上面写了,然后截图发上来. 后续关于SVM和FC在深度学习当中得使用对比分析,我再补充.
- knockout.js--基本用法
1,HTML元素的面向对象的赋值,数据绑定 text绑定:为p,span,div,td等加text属性值(即元素内部显示的文本), value绑定:为input添加value属性值, attr绑定:为 ...
- webpack3 + vue 添加 serviceWorker
新的vue脚手架已经可以自带pwa了,本文主要针对旧版的webpack. 先装三个插件: $npm i register-service-worker sw-precache-webpack-plug ...
- 【瞎扯】 About Me
手动博客搬家: 本文发表于20181218 13:54:31, 原地址https://blog.csdn.net/suncongbo/article/details/85063885 来了?坐,欢迎来 ...
- nyoj 8 一种排序(用vector,sort,不用set)
一种排序 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都是整数 ...
- mysql执行show processlist unauthenticated user 解决方法
一台unibilling机器前几天突然负载变重. 在top中发现cpu被大量占用. agi程序运行的很慢,并出现僵尸进程. 其实当时只有50个左右的并发呼叫. 远远达不到正常水准. 重新启动机器问题也 ...
- [luoguP2896] [USACO08FEB]一起吃饭Eating Together(DP)
传送门 由于 Di 只有 3 种情况,那么就很简单了 f[i][j][0] 表示前 i 个,且第 i 个变成 j 的 递增序列最小修改次数 f[i][j][1] 表示前 i 个,且第 i 个变成 j ...
- poj 2420 模拟退火法基础
/* 题意:给n个电脑,求一个点到这n个电脑的距离和最小. 模拟退火法:和poj1379的方法类似 因为坐标范围是0-10000 不妨把它看成是10000*10000的正方形来做 */ #includ ...
- 为 Windows Phone 8.1 app 解决“The type does not support direct content.”的问题
我在 VS 14 CTP 中新建了一个空的 app store 项目名叫 PlayWithXaml ,项目的 MainPage.xaml 文件改为了以下内容: <Page x:Class=&qu ...