Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 15 Accepted Submission(s) : 0
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
营救天使
思路很简单~ 但是在做的题声明队列的时候 本应该是queue<Node> q 写成了queue<int> q 就郁闷了~ 这道题 在题库里交一直是CE 但是原题交就过了~
而且在开始入队的时候 没有注意入队的位置 也是一直没过的原因之一 最后的时候要注意图的存入
代码:
#include<iostream>
#include<queue>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<stdlib.h>
using namespace std;
#define maxn 201
int dir[4][2]={
{1, 0},
{0, 1},
{-1, 0},
{0, -1},
};
bool vis[maxn][maxn];
int map[maxn][maxn];
struct Node
{
int x,y;
int step;
}node;
int n,m;
int x1,y1,x2,y2;
int dax,day;
int bfs()
{
Node now,next;
memset(vis,0,sizeof(vis));
queue<Node> q;
now.x=x1;
now.y=y1;
now.step=0;
q.push(now);
vis[now.x][now.y]=1;
if(x1==x2&&y1==y2)
return now.step;
while(!q.empty())
{
now=q.front();
// cout<<now.step<<endl;
q.pop();
for(int i=0;i<4;i++)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
if(next.x==x2&&next.y==y2)
{
return next.step-1;
}
if(next.x<=m&&next.y<=n&&next.x>=0&&next.y>=0&&!vis[next.x][next.y]&&map[next.x][next.y]==0)
{
vis[next.x][next.y]=1;
map[next.x][next.y]=1;
if(next.x!=dax&&next.y!=day)
next.step=now.step+1;
else
next.step=now.step+2;
q.push(next);
}
}
}
}
int main()
{
cin>>n>>m;
char ch;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
cin>>ch;
if(ch=='a')
{
x2=i;
y2=j;
map[i][j]=0;
cout<<i<<" "<<j<<endl;
continue;
}
if(ch=='r')
{
y1=j;
x1=i;
map[i][j]=0;
continue;
}
if(ch=='x')
{
dax=i;
day=j;
map[i][j]=0;
continue;
}
if(ch=='.')
{
map[i][j]=0;
continue;
}
if(ch=='#')
{
map[i][j]=1;
continue;
}
}
if(bfs())
cout<<bfs()<<endl;
else
cout<<"Poor ANGEL has to stay in the prison all his life." <<endl;
return 0;
}