7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
13题意:“#”墙,“a”公主,“r”救兵,“x”守卫士兵,走路没步一秒,打败士兵一秒:救兵救公主代码:#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #define INF 0xfffffff using namespace std; #include<queue> #define mx 500 char map[mx][mx]; int visit[mx][mx]; int n,m,bx,by,ex,ey,ans; int dx[4]={1,0,0,-1}; int dy[4]={0,1,-1,0}; struct line { int x,y,step; }; void Getmap()//取图 { int i,j; for(i=0;i<n;i++) { getchar(); for(j=0;j<m;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='#') visit[i][j]=1; else if(map[i][j]=='r') { bx=i; by=j; } else if(map[i][j]=='a') { ex=i;ey=j; } else if(map[i][j]=='x') visit[i][j]=2; } } } void bfs() { int i; queue<line>Q; line a,temp; a.x =bx; a.y =by; a.step =0; visit[bx][by]=1; Q.push(a); while(!Q.empty() ) { a=Q.front() ; Q.pop() ; for(i=0;i<4;i++) { temp.x =a.x +dx[i]; temp.y =a.y +dy[i]; if(visit[temp.x ][temp.y ]==2) temp.step =a.step +2; else temp.step =a.step +1; if(temp.x >=0&&temp.x <n&&temp.y >=0&&temp.y <m&&visit[temp.x][temp.y ]!=1) { if(temp.x==ex&&temp.y ==ey) { if(ans>temp.step ) ans=temp.step ; } visit[temp.x ][temp.y ]=1; Q.push(temp) ; } } } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(visit,0,sizeof(visit)); Getmap(); ans=INF; dfs(); if(ans==0xfffffff) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",ans); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/zhangxiaoxiang123/article/details/47814253