标签:
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
66 88 66
#include<stdio.h> #include<string.h> #include<queue> #include<iostream> #define min(a,b) (a>b?b:a) #define INF 0xfffffff using namespace std; int vis1[1010][1010],ans1[1010][1010],ans2[1010][1010],n,m,vis2[1010][1010]; char map[1010][1010]; struct s { int x,y,step; }a,temp; int dx[4]={0,1,0,-1}; int dy[4]={1,0,-1,0}; int jud(struct s a,int vis[1010][1010]) { if(a.x<0||a.x>=n) return 0; if(a.y<0||a.y>=m) return 0; if(vis[a.x][a.y]) return 0; if(map[a.x][a.y]=='#') return 0; return 1; } void bfs(int x,int y,int ans[1010][1010],int vis[1010][1010]) { a.x=x; a.y=y; a.step=0; vis[x][y]=1; ans[x][y]=a.step; queue<struct s>q; q.push(a); while(!q.empty()) { a=q.front(); q.pop(); for(int i=0;i<4;i++) { temp.x=a.x+dx[i]; temp.y=a.y+dy[i]; if(!jud(temp,vis)) continue; temp.step=a.step+1; ans[temp.x][temp.y]=temp.step; vis[temp.x][temp.y]=1; q.push(temp); } } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { int i,j,x1,x2,y1,y2; for(i=0;i<n;i++) { scanf("%s",map[i]); for(j=0;j<m;j++) { if(map[i][j]=='M') { x1=i; y1=j; } if(map[i][j]=='Y') { x2=i; y2=j; } } } memset(vis1,0,sizeof(vis1)); memset(ans1,0,sizeof(ans1)); bfs(x1,y1,ans1,vis1); memset(vis2,0,sizeof(vis2)); memset(ans2,0,sizeof(ans2)); bfs(x2,y2,ans2,vis2); int ans=INF; for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(map[i][j]=='@'&&vis1[i][j]&&vis2[i][j]) { //printf("%d %d\n",ans1[i][j],ans2[i][j]); ans=min(ans,ans1[i][j]+ans2[i][j]); } } } printf("%d\n",ans*11); } }
标签:
原文地址:http://blog.csdn.net/yu_ch_sh/article/details/44759647