hdu Rescue 1242
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11566 Accepted Submission(s): 4205
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.)
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.
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...
..#..#.#
#...##..
.#......
........
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
/* 广搜 检查了很久 最后 被困死的时候是 0, if(visit[i][j]<num && visit[i][j]!=0) 考虑了一些情况,题意很清晰,有多个r。一个a么?应该是。但是我没有处理。 由于x的存在使得 到达各点的时间可能存在多样,也加进去比较了。 但是之前写的dfs,没有考虑过这样的情况。 */ #include<stdio.h> #include<stdlib.h> #define HH 11111111 char a[202][205]; int map[4][2]={{1,0},{0,1},{0,-1},{-1,0}}; int zhan[50000],len; int visit[202][202]; int n,m; void bfs(int x,int y) { int i,x1,y1; zhan[++len]=x; zhan[++len]=y; visit[x][y]=0; while(len>0) { y=zhan[len--]; x=zhan[len--]; for(i=0;i<4;i++) { x1=x+map[i][0]; y1=y+map[i][1]; if(x1>=1&&x1<=n && y1>=1&&y1<=m) { if(visit[x1][y1]==0 && a[x1][y1]!='#') { if(a[x1][y1]=='.'||a[x1][y1]=='r') visit[x1][y1]=visit[x][y]+1; else if(a[x1][y1]=='x') visit[x1][y1]=visit[x][y]+2; zhan[++len]=x1; zhan[++len]=y1; } if(visit[x1][y1]>0 && a[x1][y1]!='#') { if((a[x1][y1]=='.'||a[x1][y1]=='r')&&visit[x1][y1]>visit[x][y]+1) { visit[x1][y1]=visit[x][y]+1; zhan[++len]=x1; zhan[++len]=y1; } if(a[x1][y1]=='x' && visit[x1][y1]>visit[x][y]+2) { visit[x1][y1]=visit[x][y]+2; zhan[++len]=x1; zhan[++len]=y1; } } } } } } int main() { int i,j,num; while(scanf("%d%d",&n,&m)>0) { for(i=1;i<=n;i++) scanf("%s",a[i]+1); for(i=1;i<=n;i++) for(j=1;j<=m;j++) visit[i][j]=0; for(i=1;i<=n;i++) for(j=1;j<=m;j++) { if(a[i][j]=='a') { len=0; bfs(i,j); } } num=HH; for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(a[i][j]=='r') { if(visit[i][j]<num && visit[i][j]!=0) num=visit[i][j]; } if(num==HH) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",num); } return 0; }
单纯的广搜,在浙大oj超时.... 蒋神却过了,思想很厉害。
/* 优先队列 */ #include<stdio.h> #include<iostream> #include<cstdlib> #include<string.h> #include<queue> #define HH 11111111 using namespace std; char a[202][202]; int visit[202][202]; int n,m; int map[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; struct node { friend bool operator< (node n1,node n2) { return n1.p>n2.p; } int p; int x; int y; }; void bfs(int x,int y) { int i,x1,y1; priority_queue<node>b; while(!b.empty()) { b.pop(); } node tmp,tmp1; tmp.x=x; tmp.y=y; tmp.p=0; b.push(tmp); visit[x][y]=1; while(b.size()>0) { tmp=b.top(); b.pop(); for(i=0;i<4;i++) { x1=tmp.x+map[i][0]; y1=tmp.y+map[i][1]; if(x1>=1&&x1<=n && y1>=1&&y1<=m && visit[x1][y1]==0 && a[x1][y1]!='#') { if(a[x1][y1]=='x') visit[x1][y1]=tmp.p+2; else if(a[x1][y1]=='.' || a[x1][y1]=='r') visit[x1][y1]=tmp.p+1; tmp1=tmp; tmp.x=x1; tmp.y=y1; tmp.p=visit[x1][y1]; b.push(tmp); tmp=tmp1; if(a[x1][y1]=='r')return; } } } } int main() { int i,j,num; while(scanf("%d%d",&n,&m)>0) { for(i=1;i<=n;i++) scanf("%s",a[i]+1); memset(visit,0,sizeof(visit)); for(i=1;i<=n;i++) for(j=1;j<=m;j++) { if(a[i][j]=='a') { bfs(i,j); } } num=HH; for(i=1;i<=n;i++) for(j=1;j<=m;j++) { if(a[i][j]=='r' && visit[i][j]!=0 && visit[i][j]<num) num=visit[i][j]; } if(num==HH) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",num); } return 0; }