标签:
#include <cstdio> #include <iostream> #include <string> #include <sstream> #include <cstring> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <map> #define PI acos(-1.0) #define ms(a) memset(a,0,sizeof(a)) #define msp memset(mp,0,sizeof(mp)) #define msv memset(vis,0,sizeof(vis)) using namespace std; //#define LOCAL int n,m; char mp[210][210]; int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; int ex,ey; int vis[210][210]; struct Node { int x,y; int time; } cp; int bfs() { queue<Node> q; while(!q.empty())q.pop(); q.push(cp); while(!q.empty()) { cp=q.front(),q.pop(); if(cp.x==ex&&cp.y==ey)return cp.time; vis[cp.x][cp.y]=1; for(int i=0; i<4; i++) { Node np; np.x=cp.x+dir[i][0]; np.y=cp.y+dir[i][1]; np.time=cp.time+1; if(mp[np.x][np.y]==‘#‘)continue; if(vis[np.x][np.y])continue; if(np.x<0||np.y<0||np.x>=n||np.y>=m)continue; if(mp[np.x][np.y]==‘x‘)np.time++; q.push(np); } } return -1; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); #endif // LOCAL ios::sync_with_stdio(false); while(cin>>n>>m) { msp,msv; for(int i=0; i<n; i++)cin>>mp[i]; for(int i=0; i<n; i++) for(int j=0; j<m; j++) { if(mp[i][j]==‘a‘)ex=i,ey=j; else if(mp[i][j]==‘r‘)cp.x=i,cp.y=j; } cp.time=0; vis[cp.x][cp.y]=1; int ans=bfs(); if(ans==-1)printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/gpsx/p/5184734.html