[kuangbin] M - Find a way(简单广搜)
题目链接:https://vjudge.net/contest/215603#problem/M
其中三维数组dis将两个广搜合并到了一起
#include<iostream> #include<sstream> #include<algorithm> #include<cstdio> #include<string.h> #include<cctype> #include<string> #include<cmath> #include<vector> #include<stack> #include<queue> #include<map> #include<set> using namespace std; const int INF=220; const int M=0x1f1f1f1f; int n,m,z; char cnt[INF][INF]; int dir[][2]= {{1,0},{0,-1},{-1,0},{0,1}}; int dis[INF][INF][2]; int vis[INF][INF]; struct node { int x,y; int step; node(int x,int y,int z):x(x),y(y),step(z){} node() {} }; void BFS(int x,int y) { memset(vis,0,sizeof(vis)); vis[x][y]=1; queue<node>q; q.push(node(x,y,0)); while(!q.empty()) { node top=q.front(); q.pop(); for(int i=0; i<4; i++) { int newx=top.x+dir[i][0]; int newy=top.y+dir[i][1]; if(!vis[newx][newy]&& newx>=1&&newx<=n&&newy>=1&&newy<=m&&cnt[newx][newy]!='#') { vis[newx][newy]=1; dis[newx][newy][z]=top.step+1; q.push(node(newx,newy,top.step+1)); } } } } int main() { while(cin>>n>>m) { memset(dis,M,sizeof(dis)); for(int i=1; i<=n; i++) scanf("%s",cnt[i]+1); for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) { if(cnt[i][j]=='Y') { z=1; BFS(i,j); } if(cnt[i][j]=='M') { z=0; BFS(i,j); } } int ans=M; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { if(cnt[i][j]=='@') { ans=min(ans,dis[i][j][0]+dis[i][j][1]); } } } cout<<ans*11<<endl; } return 0; }