Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Input
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
菜鸟分析:让r救a么 ,但是r有很多个,你就从a开始找最近的r,测试案例比较特殊,他正好有一个朋友,容易误导,每个朋友有好几种营救方法,要找到离他最近的朋友,再从这些路中找出最短的。
这条蓝色的是最短的 蓝色的加上杀警卫的时间+1=13,红色的=14
最短路径问题BFS?
(谢谢大佬)参考题解https://blog.csdn.net/u013923947/article/details/2317421
#include
#include
#include
#include
#include
#include
using namespace std;
int n,m;
int xa,ya,xr,yr;
char map[220][220];
int vis[220][220];
int dis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
int x,y,step;
bool operator < (const node& t)const{
return step>t.step;
}
};
int bfs()
{
memset(vis,0,sizeof(vis));
priority_queueq;
node f;
f.x=xa;
f.y=ya;
f.step=0;
vis[xa][ya]=1;
q.push(f);
while(!q.empty())
{
f=q.top();
q.pop();
if(map[f.x][f.y]=='r')
{
return f.step;
}
// 朋友不只是一个,而天使却是一个,所以我们可以反过来寻找,天使找朋友,那么目标状态就是找到朋友
for(int i=0;i<4;i++)
{
node next;
int xi=f.x+dis[i][0];
int yi=f.y+dis[i][1];
if(xi>=0 &&xi=0 && yi> n >> m)
{
for(int i=0;i
以下是错误代码只作参考
#include
#include
#include
#include
using namespace std;
int n,m;
char map[205][205];
int vis[205][205];
/*两个作用
1.标记作用,刚开始初始化为1,先将起始点初始化为0,以防回到起点
2.记录当前点的最短路径
*/
int x1,x2,y1,y2;
int mov[][2] = {1,0,-1,0,0,1,0,-1};
struct node
{
int x,y,step;
//友元函数 重载<
//优先队列就是node型的 在优先队列里按照step排序
friend bool operator<(node n1,node n2)
{
return n2.step=n || y>=m || !vis[x][y] || map[x][y] == '#')
return 1;//出界或者为墙
return 0;
}
int bfs()
{
int i;
priority_queue Q;//优先队列
node a,next;
a.x = x1;
a.y = y1;
a.step = 0;
Q.push(a);//进队
vis[x1][y1] = 0;//初始化
while(!Q.empty())
{
a = Q.top();//队头
Q.pop();//出队
if(a.x == x2 && a.y == y2)
return a.step;
//四个方向
for(i = 0; i<4; i++)
{
next = a;//移动后的结点
next.x+=mov[i][0];
next.y+=mov[i][1];
if(place(next.x,next.y))//判断 走不通
continue;//跳出当前循环
next.step++;//每动一次要加1
if(map[next.x][next.y] == 'x')//卫兵处多花费了一秒
next.step++;
if(vis[next.x][next.y]>=next.step)//存入最小时间
{
vis[next.x][next.y] = next.step;//第二个作用 更新最小
Q.push(next);
}//只有起点标记,其它点未标记只是最小值,还可以走
}
}
return 0;
}
int main()
{
while(cin>>n>>m)
{
for(int i = 0; i