7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
13
广搜,遇到守卫,时间 +2 ,进入队列,因为需要在队列中重新排序,因此,要用到优先队列!
这一题与 hdu 上面的 1180 诡异的楼梯 属于同一类型!
附上hdu 1180 诡异楼梯 链接:http://blog.csdn.net/xia842655187/article/details/47361835
附上源代码:
#include <iostream> #include <algorithm> #include <cstring> #include <string> #include <cstdio> #include <queue> using namespace std; int b[4][2] = {1,0,0,1,-1,0,0,-1}; // 下右左上 char Map[250][250]; int visit[250][250]; int n,m; struct A { int x,y,step; bool operator < (const A &a) const { return a.step < step; } }; int dfs(int x1,int y1,int x2,int y2) { int sx,sy,t; priority_queue<A> Q; A e; e.x = x1; e.y = y1; e.step = 0; Q.push(e); visit[x1][y1] = 1; while(!Q.empty()) { e = Q.top(); if(e.x == x2 && e.y == y2) break; Q.pop(); for(int i = 0;i < 4;i++) { sx = e.x + b[i][0]; sy = e.y + b[i][1]; if(sx >= 0 && sx <= n && sy >= 0 & sy <= m && (Map[sx][sy] == '.' || Map[sx][sy] == 'x' || Map[sx][sy] == 'a') && !visit[sx][sy]) { if(Map[sx][sy] == 'x') { t = e.step + 2; } else t = e.step + 1; A e1; e1.x = sx; e1.y = sy; e1.step = t; Q.push(e1); visit[sx][sy] = 1; } } } if(Q.empty()) return -1; else { while(!Q.empty()) Q.pop(); return e.step; } } int main() { while(cin >> n >> m) { int x1,x2,y1,y2; memset(Map,'\0',sizeof(Map)); memset(visit,0,sizeof(visit)); for(int i = 0;i < n;i++) { scanf("%s",Map[i]); for(int j = 0;j < m;j++) { if(Map[i][j] == 'a') { x2 = i; y2 = j; } if(Map[i][j] == 'r') { x1 = i; y1= j; } } } int ans = dfs(x1,y1,x2,y2); if(ans >= 0) cout << ans << endl; else cout << "Poor ANGEL has to stay in the prison all his life." << endl; } return 0; }