BFS problems.
Angel was in jail, and many of her friends were trying to save her.
# Indicates the wall,. indicates the road, X indicates the guard, and r indicates her friends.
Because there may be many friends, but there is only one angel, the Search Start Point is set to Angel. If you find a friend, you can go out.
It takes 1 to take a grid and 1 to kill the guard, but it cannot be added directly.
This is because
4 8axxxxxxr........................
If you add 2 directly, the answer is not 9.
#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<vector>#include<cmath>#define INF 0x7fffffff#define eps 1e-8#define LL long long#define PI 3.141592654#define CLR(a,b) memset(a,b,sizeof(a))#define FOR(i,a,n) for(int i= a;i< n ;i++)#define debug puts("==fuck==")#define acfun std::ios::sync_with_stdio(false)#define SIZE 1000+10using namespace std;int xx[]={0,0,-1,1};int yy[]={-1,1,0,0};int n,m;char g[201][201];struct lx{ int x,y,lv; void init(int xx,int yy,int llv) { x=xx,y=yy,lv=llv; }};lx start,thend;void bfs(){ queue<lx>q; bool vis[201][201]; CLR(vis,0); q.push(start); vis[start.x][start.y]=1; while(!q.empty()) { lx tmp=q.front(); q.pop();// printf("%d %d == %d\n",tmp.x,tmp.y,tmp.lv);// system("pause"); if(g[tmp.x][tmp.y]=='r') { printf("%d\n",tmp.lv); return ; } FOR(k,0,4) { int x=tmp.x+xx[k]; int y=tmp.y+yy[k]; if(x<0||y<0||x>=n||y>=m||g[x][y]=='#'||vis[x][y]) continue; lx now; if(g[x][y]=='x') { now.init(tmp.x,tmp.y,tmp.lv+1); g[x][y]='.'; } else { now.init(x,y,tmp.lv+1); vis[x][y]=1; } q.push(now); } } puts("Poor ANGEL has to stay in the prison all his life.");}int main(){ while(~scanf("%d%d",&n,&m)) { char str[201]; FOR(i,0,n) { scanf("%s",str); FOR(j,0,m) { g[i][j]=str[j]; if(g[i][j]=='a') start.init(i,j,0); } } bfs(); }}
HDU 1242 rescue