标签:
广搜2次,照着模版总算a了
#include<iostream> #include<queue> #include<cstring> #define maxn 200+5 using namespace std; int m,n,ax,ay,bx,by; char mapp[maxn][maxn]; int visit[maxn][maxn]; int rem1[maxn][maxn]; int rem2[maxn][maxn]; int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; struct stu { int x,y,s,sign; }; void bfs(int x,int y,int sign) { stu xx,yy; queue<stu>root; xx.x=x;xx.y=y;xx.s=0;xx.sign=sign; visit[xx.x][xx.y]=1; root.push(xx); while(root.size()) { xx=root.front(); root.pop(); if(mapp[xx.x][xx.y]=='@'&&xx.sign==1){rem1[xx.x][xx.y]=xx.s;} else if(mapp[xx.x][xx.y]=='@'&&xx.sign==2){rem2[xx.x][xx.y]=xx.s;} for(int i=0;i<4;i++) { yy.x=xx.x+dir[i][0]; yy.y=xx.y+dir[i][1]; yy.s=xx.s; yy.sign=xx.sign; if(yy.x<0||yy.x>=n||yy.y<0||yy.y>=m||visit[yy.x][yy.y]||mapp[yy.x][yy.y]=='#'){continue;} yy.s++; root.push(yy); visit[yy.x][yy.y]=1; } } } int main() { while(cin>>n>>m) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>mapp[i][j]; if(mapp[i][j]=='Y') ax=i,ay=j; if(mapp[i][j]=='M') bx=i,by=j; } } memset(visit,0,sizeof(visit)); memset(rem1,0,sizeof(rem1)); bfs(ax,ay,1); memset(visit,0,sizeof(visit)); memset(rem2,0,sizeof(rem2)); bfs(bx,by,2); int sum=1<<20; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if((rem1[i][j]!=0)&&(rem2[i][j]!=0)) sum=min(sum,rem1[i][j]+rem2[i][j]); } } cout<<sum*11<<endl; } return 0; }
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/44756945