Problem Description
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 |
代码:
#include<bits/stdc++.h>
using namespace std;
struct node{
int x,y,step;
};
char a[200][201];
int n,m,cnt,bx,by;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
bool can[200][201];
void bfs()
{
queue<node> Q;
node p,t;
p.x=bx; p.y=by; p.step=0;
Q.push(p);
while(!Q.empty())
{
p=Q.front();
Q.pop();
if(a[p.x][p.y]=='a') {cnt=p.step; return;}
else if(a[p.x][p.y]=='x')
{
a[p.x][p.y]='.';
t.x=p.x; t.y=p.y;
t.step=p.step+1;
Q.push(t);
continue; //因为干掉守卫需要一个战斗时间,
//故要把搜索步数延迟一步,让其他更优的搜索先搜,以防出错。
}
for(int k=0;k<4;k++)
{
t.x=p.x+dx[k];
t.y=p.y+dy[k];
if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m){
if(can[t.x][t.y]==false&&a[t.x][t.y]!='#')
{
can[t.x][t.y]=true;
t.step=p.step+1;
Q.push(t);
}}
}
}
}
int main()
{
ios::sync_with_stdio(false);
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%s",a[i]);
for(j=0;j<m;j++)
{
if(a[i][j]=='r')
{
bx=i; by=j;
}
can[i][j]=false;
}
}
can[bx][by]=true; cnt=-1;
bfs();
if(cnt!=-1) printf("%d\n",cnt);
else printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}
深搜代码:
#include<bits/stdc++.h>
using namespace std;
#define maxx 0x7fffffff
char a[200][201];
int n,m,cnt;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
bool can[200][201];
bool ok(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m) return true;
return false;
}
void bfs(int x,int y,int step)
{
if(a[x][y]=='a') {if(cnt>step) cnt=step; return;}
for(int k=0;k<4;k++)
{
int xx=x+dx[k];
int yy=y+dy[k];
if(ok(xx,yy)==false) continue;
if(a[xx][yy]!='#'&&can[xx][yy]==false)
{
can[xx][yy]=true;
if(a[xx][yy]=='x')
bfs(xx,yy,step+2);
else bfs(xx,yy,step+1);
can[xx][yy]=false;
}
}
}
int main()
{
ios::sync_with_stdio(false);
int i,j,x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%s",a[i]);
for(j=0;j<m;j++)
{
if(a[i][j]=='r')
{
x=i; y=j;
}
can[i][j]=false;
}
}
can[x][y]=true; cnt=maxx;
bfs(x,y,0);
if(cnt!=maxx) printf("%d\n",cnt);
else printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}