Question:
Angel was captured by the mysterious and evil Moligpy man! He is in a maze. The length and width of the Maze cannot exceed 200. The maze contains walls that cannot be crossed and prison guards.
Angel's friends brought rescue teams to the maze. Their task is: close to Angel. We assume that the location close to Angel is the location where Angel is located.
Assume that moving takes 1 unit of time and killing a guard takes 1 unit of time. After arriving at a grid, if the grid is guarded, it must be killed. What is the task assigned to you? How many units of time does it take to reach the location where Angel is located? (You can only move up, down, left, and right)
Input
This question contains multiple groups of test data. The second integer n and m in the first line of each group of test data. The size of the maze is n * m (N, M <= 200 ). Next n rows, with m characters in each line. "#" Indicates the wall, "." indicates the movement, "x" indicates the guard, "a" indicates Angel, and "r" indicates the rescue team. All letters are in lowercase.
Output
A row represents the shortest time to rescue Angel. If the rescue team can never reach Angel, output "Poor ANGEL has to stay in the prison all his life ."
Sample Input
7 8
#.#####.
#. A #... r.
#... # X...
..#..#.#
#...##..
.#......
........
Sample Output
13
Analysis:
(N, M <= 200) first thought of BFS, {P.S someone dfs 0 ms ac ~~}
There may be multiple 'R', and 'A' has only one. From 'A', the first 'r' is obtained.
Because "x" is added to the question to guard this stuff, it takes an additional unit of time to pass through "x. Therefore, if we use a common wide search, we can find the target at Layer k, but it may take more than k units of time {Maybe this path has passed through "x "}.
I have three ideas:
1. Use the priority queue (when it reaches the point) to ensure that the time consumed is the minimum when it is first extended to the target State.
2. When we extend it to "x", we place it on the layer that is the same as the time consumption. This ensures that the time consumed is the minimum when the target State is extended.
Code1: (priority queue)
#include <iostream> #include <cstring> #include <cstdio> #include <queue> using namespace std; int n, m; char map[205][205]; int sx, sy; bool flag; struct node { int x, y, step; bool operator <(const node & t) const { return step>t.step; } }; int dx[]= {-1,0,0,1}; int dy[]= {0,-1,1,0}; void bfs() { node now, tmp; int i,xx,yy; priority_queue<node> q; now.x = sx, now.y = sy, now.step = 0; map[sx][sy] = '#'; q.push(now); while(!q.empty()) { now = q.top(); q.pop(); // cout<<now.x<<" "<<now.y<<" "<<now.step<<endl; for(i=0; i<4; i++) { xx = now.x +dx[i]; yy = now.y +dy[i]; if(xx<0||xx>=n||yy<0||yy>=m||map[xx][yy]=='#') continue; if(map[xx][yy]=='r') { cout<<now.step+1<<endl; flag = true; return ; } if(map[xx][yy]=='x') { tmp.x =xx, tmp.y = yy, tmp.step = now.step+2; q.push(tmp); } else { tmp.x =xx, tmp.y = yy, tmp.step = now.step+1; q.push(tmp); } map[xx][yy] = '#'; } } } int main() { int i, j; while(~scanf("%d%d",&n,&m)) { for(i=0; i<n; i++) for(j=0; j<m; j++) { cin>>map[i][j]; if(map[i][j]=='a') sx=i,sy=j; } flag = false; bfs(); if(!flag) printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0; } #include <iostream>#include <cstring>#include <cstdio>#include <queue>using namespace std;int n, m;char map[205][205];int sx, sy;bool flag;struct node { int x, y, step; bool operator <(const node & t) const { return step>t.step; }};int dx[]= {-1,0,0,1};int dy[]= {0,-1,1,0};void bfs() { node now, tmp; int i,xx,yy; priority_queue<node> q; now.x = sx, now.y = sy, now.step = 0; map[sx][sy] = '#'; q.push(now); while(!q.empty()) { now = q.top(); q.pop();// cout<<now.x<<" "<<now.y<<" "<<now.step<<endl; for(i=0; i<4; i++) { xx = now.x +dx[i]; yy = now.y +dy[i]; if(xx<0||xx>=n||yy<0||yy>=m||map[xx][yy]=='#') continue; if(map[xx][yy]=='r') { cout<<now.step+1<<endl; flag = true; return ; } if(map[xx][yy]=='x') { tmp.x =xx, tmp.y = yy, tmp.step = now.step+2; q.push(tmp); } else { tmp.x =xx, tmp.y = yy, tmp.step = now.step+1; q.push(tmp); } map[xx][yy] = '#'; } }}int main() { int i, j; while(~scanf("%d%d",&n,&m)) { for(i=0; i<n; i++) for(j=0; j<m; j++) { cin>>map[i][j]; if(map[i][j]=='a') sx=i,sy=j; } flag = false; bfs(); if(!flag) printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0;}
Code2: (idea 2)
# Include <iostream >#include <queue> # include <cstring> using namespace std; struct node {int x, y; int step ;}; queue <node> q; int N, M, prove, sx, sy, visit [202] [202]; char map [202] [202]; int dx [4] = {-1, 1, 0, 0}; int dy [4] = {0, 0, 1,-1,}; int check (int x, int y) {if (x <0 | x> = N | y <0 | y> = M) return 0; else return 1;} void BFS () {while (! Q. empty () q. pop (); node s, e; memset (visit, 0, sizeof (visit); s. step = 0; s. x = sx; s. y = sy; q. push (s); visit [s. x] [s. y] = 1; while (! Q. empty () {s = q. front (); q. pop (); if (map [s. x] [s. y] = 'A') {cout <s. step <endl; prove = 1;} // if the retrieved result is in a guard cell, that is, killing the guard and entering the queue again if (map [s. x] [s. y] = 'X') {map [s. x] [s. y] = '. '; s. step + = 1; q. push (s); continue;} for (int I = 0; I <4; I ++) {e. x = s. x + dx [I]; e. y = s. y + dy [I]; if (! Check (e. x, e. y) | visit [e. x] [e. y] | map [e. x] [e. y] = '#') continue; e. step = s. step + 1; q. push (e); visit [e. x] [e. y] = 1 ;}}int main () {while (cin> N> M) {for (int I = 0; I <N; I ++) for (int j = 0; j <M; j ++) {cin> map [I] [j]; if (map [I] [j] = 'R') {sx = I; sy = j ;}} prove = 0; BFS (); if (! Prove) cout <"Poor ANGEL has to stay in the prison all his life. "<endl ;}return 0 ;}# include <iostream >#include <queue >#include <cstring> using namespace std; struct node {int x, y; int step;}; queue <node> q; int N, M, prove, sx, sy, visit [202] [202]; char map [202] [202]; int dx [4] = {-1, 1, 0, 0}; int dy [4] = {0, 0, 1,-1 ,}; int check (int x, int y) {if (x <0 | x> = N | y <0 | y> = M) return 0; el Se return 1;} void BFS () {while (! Q. empty () q. pop (); node s, e; memset (visit, 0, sizeof (visit); s. step = 0; s. x = sx; s. y = sy; q. push (s); visit [s. x] [s. y] = 1; while (! Q. empty () {s = q. front (); q. pop (); if (map [s. x] [s. y] = 'A') {cout <s. step <endl; prove = 1;} // if the retrieved result is in a guard cell, that is, killing the guard and entering the queue again if (map [s. x] [s. y] = 'X') {map [s. x] [s. y] = '. '; s. step + = 1; q. push (s); continue;} for (int I = 0; I <4; I ++) {e. x = s. x + dx [I]; e. y = s. y + dy [I]; if (! Check (e. x, e. y) | visit [e. x] [e. y] | map [e. x] [e. y] = '#') continue; e. step = s. step + 1; q. push (e); visit [e. x] [e. y] = 1 ;}}int main () {while (cin> N> M) {for (int I = 0; I <N; I ++) for (int j = 0; j <M; j ++) {cin> map [I] [j]; if (map [I] [j] = 'R') {sx = I; sy = j ;}} prove = 0; BFS (); if (! Prove) cout <"Poor ANGEL has to stay in the prison all his life." <endl;} return 0 ;}