Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 32204 Accepted Submission(s): 11245
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.)
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.
7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
13
#include <iostream>
#include <queue>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn = 205;
int n,m,ex,ey;
char map[maxn][maxn];
bool used[maxn][maxn];
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
struct node{
int x,y,t;
};
queue<node>Q;
node q,p;
bool islegal(int x ,int y){
if (x<1 || y<1 || x>n || y>m || map[x][y]=='#') return 0;
return 1;
}
int bfs(){
while (!Q.empty()){
p=Q.front();
Q.pop();
if (p.x== ex && p.y == ey) return p.t;
if (map[p.x][p.y]=='x'){ //警卫特殊处理
map[p.x][p.y]='.'; //修改地图
p.t++; //队列元素加一
Q.push(p); //重入队列
continue;
}
for (int i=0;i<4;i++){
q=p;
q.x+=dx[i];
q.y+=dy[i];
q.t++;
if (islegal(q.x,q.y) && !used[q.x][q.y]) {
used[q.x][q.y]=1;
Q.push(q);
}
}
}
return -1;
}
int main(){
while (cin>>n>>m){
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
cin>>map[i][j];
if (map[i][j]=='r'){
p.x=i;
p.y=j;
p.t=0;
Q.push(p);
}
if (map[i][j]=='a'){
ex=i;
ey=j;
}
}
}
memset(used,0,sizeof(used));
int ans = bfs();
if (ans==-1) printf("Poor ANGEL has to stay in the prison all his life.\n");
else printf("%d\n",ans);
}
return 0;
}
1720



被折叠的 条评论
为什么被折叠?



