kuangbin专题一 简单搜索 N - Find a way

N - Find a way

 

Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki. 
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest. 
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes. 
InputThe input contains multiple test cases. 
Each test case include, first two integers n, m. (2<=n,m<=200). 
Next n lines, each line included m character. 
‘Y’ express yifenfei initial position. 
‘M’    express Merceki initial position. 
‘#’ forbid road; 
‘.’ Road. 
‘@’ KCF 
OutputFor each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.Sample Input
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
Sample Output
66
88
66

很简单的一道题,双向bfs练手,只不过在判断是否出图的时候,y<m写成了y<n,wa了很多次,在测试数据1 5 YM@#.才发现错误。。。

#include<iostream>  
#include<cstring>  
#include<algorithm>  
#include<queue>  
using namespace std;  
const int _max=300;
const int _m=10000;
char mp[_max][_max];
int stepy[_max][_max],stepm[_max][_max];
bool visy[_max][_max],vism[_max][_max];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
struct node{
	int x,y;
	int t;
};
int n,m,kcount,scount;
int xy,yy,xm,ym;

bool check(node n1,bool vis[_max][_max]){
	if(n1.x<1||n1.x>n) return false;
	if(n1.y<1||n1.y>m) return false;
	if(!vis[n1.x][n1.y]) return false;
	return true;
}
void bfs(int sx,int sy,bool vis[_max][_max],int step[_max][_max]){
	queue<node> q;
	node now,next;
	now.x=sx;
	now.y=sy;
	now.t=0;
	step[sx][sy]=0;
	vis[now.x][now.y]=0;
	q.push(now);
	scount=0;
	while(!q.empty()){
		now=q.front();
		q.pop();	
	    if(scount==kcount){
			return;
		}
		for(int i=0;i<4;i++){
		next.x=now.x+dx[i];
		next.y=now.y+dy[i];
		next.t=now.t+1;
		if(!check(next,vis)) continue;
		vis[next.x][next.y]=0;
		if(mp[next.x][next.y]=='@'){
			scount++;
			step[next.x][next.y]=next.t;
		}  

		q.push(next);
	    }
	}
}
int main()
{
	while(cin>>n>>m){
		memset(visy,1,sizeof(visy));
		memset(vism,1,sizeof(vism));
		memset(stepy,100000,sizeof(stepy));
		memset(stepm,100000,sizeof(stepm));
		kcount=0;
		int kfcmin=_m;
		for(int i=1;i<=n;i++)
		  for(int j=1;j<=m;j++){
		  	cin>>mp[i][j];
		  	if(mp[i][j]=='@')
		  	   {
		  	   	kcount++;
				}
				if(mp[i][j]=='#')
				vism[i][j]=visy[i][j]=0;
				if(mp[i][j]=='Y'){
					xy=i;
					yy=j;
				}
				if(mp[i][j]=='M'){
					xm=i;ym=j;
				}
          }
          bfs(xy,yy,visy,stepy);
          bfs(xm,ym,vism,stepm);
          for(int i=1;i<=n;i++)
		      for(int j=1;j<=m;j++){
              	if(mp[i][j]=='@'&&stepy[i][j]!=0){
              	kfcmin=min(stepy[i][j]+stepm[i][j],kfcmin);
              }
			  }
			  cout<<kfcmin*11<<endl;
	}
}

阅读更多
上一篇kuangbin专题一 简单搜索 K - 迷宫问题
下一篇kuangbin专题一 简单搜索 L - Oil Deposits
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭