*【HDU - 1242 】 Rescue (反向dfs,或bfs)
題干:
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題目大意:
天使被困在監獄,他的朋友們想見他,監獄的地形復雜,包括路(用點標示),墻(用#標示),天使的位置(用a標示),他的朋友(用r標示),監獄里還有守衛(用x標示),他的朋友只能向左右上下四個方向走,走以不花一單位時間,若碰上守衛,消滅守衛需要額外花費一單位時間。問最少多長時間天使能見到他的朋友。
解題報告:
? ? ? ?注意這題,可能有多個朋友,所以需要從Angel作為起點,朋友作為判斷出口,而不是讓朋友來找Angel,所以需要反著跑dfs。
錯誤代碼:
#include<bits/stdc++.h>using namespace std; const int INF = 0x3f3f3f3f; int n,m; int ans; int ex,ey; int nx[4] = {0,1,0,-1}; int ny[4] = {1,0,-1,0}; char maze[205][205]; bool vis[205][205]; bool fit(int x,int y) {if(x > n || x < 1 || y > m || y < 1) return false;return true; } void dfs(int x,int y,int step) {if(step >= ans) return ;if(maze[x][y] == 'r'/*x == ex && y == ey*/) {ans = min(ans,step);return;}for(int k = 0; k<4; k++) {int tx = x + nx[k];int ty = y + ny[k];if(maze[tx][ty] == '#' || vis[tx][ty] == 1) continue;if(!fit(tx,ty)) continue;if(maze[tx][ty] == 'x') step++;vis[tx][ty]=1;dfs(tx,ty,step+1);vis[tx][ty]=0;//step--;} }int main() {int xx,yy;while(~scanf("%d%d",&n,&m)) {ans = INT_MAX;for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == 'a') xx=i,yy=j;}}dfs(xx,yy,0);if(ans == INT_MAX) printf("Poor ANGEL has to stay in the prison all his life.\n");else printf("%d\n",ans);}return 0; }AC代碼:
#include<bits/stdc++.h>using namespace std; const int INF = 0x3f3f3f3f; int n,m; int ans; int ex,ey; int nx[4] = {0,1,0,-1}; int ny[4] = {1,0,-1,0}; char maze[205][205]; bool vis[205][205]; bool fit(int x,int y) {if(x > n || x < 1 || y > m || y < 1) return false;return true; } void dfs(int x,int y,int step) {if(step >= ans) return ;if(maze[x][y] == 'r'/*x == ex && y == ey*/) {ans = min(ans,step);return;}for(int k = 0; k<4; k++) {int tx = x + nx[k];int ty = y + ny[k];if(maze[tx][ty] == '#' || vis[tx][ty] == 1) continue;if(!fit(tx,ty)) continue;vis[tx][ty]=1;if(maze[tx][ty] == 'x') dfs(tx,ty,step+2);else dfs(tx,ty,step+1);vis[tx][ty]=0;} }int main() {int xx,yy;while(~scanf("%d%d",&n,&m)) {ans = INT_MAX;for(int i = 1; i<=n; i++) {scanf("%s",maze[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(maze[i][j] == 'a') xx=i,yy=j;}}dfs(xx,yy,0);if(ans == INT_MAX) printf("Poor ANGEL has to stay in the prison all his life.\n");else printf("%d\n",ans);}return 0; }?對于這個錯誤代碼,第一次修改的時候,加上了注釋掉的那句step--,然后樣例就wa,然后一想 還是不對,應該是如果step++過,那才step--,不然肯定答案就不正確了啊,于是就放棄了這種step++這種方式,改成了下面AC代碼中的形式。
以后可以補充一下bfs的形式、、、
總結
以上是生活随笔為你收集整理的*【HDU - 1242 】 Rescue (反向dfs,或bfs)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 格力为了研发电饭煲有多拼?董明珠自曝:用
- 下一篇: 网传华为奇瑞联手“造车”官方回应:暂无相