标签:
# include <iostream> # include <cstring> # include <queue> # include <fstream> using namespace std; struct Info { int x, y; }a, r; int n, m; char map[500][500]; int dir[4][2] = {{-1, 0},{1, 0},{0, -1},{0, 1},}; int jishu[500][500]; bool border(int x, int y) { if(x >= 1 && x <= n && y >= 1 && y <= m) return true; return false; } int bfs(); int main() { //fstream cin("aaa.txt"); 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] == ‘a‘) { a.x = i; a.y = j; } if(map[i][j] == ‘r‘) { r.x = i; r.y = j; } } } int haha = bfs(); if(haha == 88888888) cout << "Poor ANGEL has to stay in the prison all his life." << endl; else cout << haha << endl; } return 0; } int bfs() { for(int i = 0; i < 500; i++) for(int j = 0; j < 500; j++) jishu[i][j] = 88888888; jishu[r.x][r.y] = 0; queue <Info> Q; Q.push(r); Info t, tep; while(!Q.empty()) { tep = Q.front(); Q.pop(); if(tep.x == a.x && tep.y == a.y) { break; } for(int i = 0; i < 4; i++) { t.x = tep.x + dir[i][0]; t.y = tep.y + dir[i][1]; if(!border(t.x, t.y))// 出界, 走过, 石墙 continue; if(map[t.x][t.y] == ‘#‘) continue; int min; if(map[t.x][t.y] == ‘x‘) min = jishu[tep.x][tep.y] + 2; else min = jishu[tep.x][tep.y] + 1; if(min >= jishu[t.x][t.y]) continue; jishu[t.x][t.y] = min; //cout << jishu[t.x][t.y] << endl; Q.push(t); } } return jishu[a.x][a.y]; }
标签:
原文地址:http://www.cnblogs.com/lyf-acm/p/5399355.html