HDU--2612.Find a way (BFS)

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.

Input

The 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

Output

For 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.

Input

4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
Output

66
88
66
 

本题的思路:

由两个起点分别bfs一遍,然后看到哪个kfc的距离之和最短

scanf(" %c", &a);

scanf格式化字符串中的空格会匹配任意多个空白字符,所以上面只会读取缓冲区中的第一个非空白字符,而跳过任何空白字符,即使缓冲区中一个空白字符都没有,也不会因此出错。所以推荐在明确不需要读入空白字符的时候使用这种方式,这样可以防止出错读到了前一行的换行符

C语言scanf格式化字符串中%c的使用建议_Shane Zhang的博客-CSDN博客_scanf(%c)

如果搜到的是kfc那么怎么判断两个kfc是同一个kfc呢?

1.我们可以用结构体来存储,kfc的坐标,写个重载。那么sort一下就一定是同一个kfc了(sort太慢了),但是也可以AC就是不是很好的做法

2.直接再开个二维数组来存储(完美)

while(~scanf("%d%d",&n,&m))

不写~就会时间超限

 sort重载来写:

#include<string.h>
#include<stdio.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
char g[210][210];
int d[210][210];
int n,m;
typedef long long ll;
typedef pair<int,int>PII;
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
int f; 
int cnt1,cnt2;

struct node
{
    int x,y,d;	
    bool operator<(const node&t)
    {
    	if(x==t.x)
    	return y<t.y;
    	else
    	return x<t.x;
	}
}kfc1[40010],kfc2[40010];


void bfs(PII x)
{
	queue<PII>q;
	q.push(x);
	memset(d,0,sizeof(d));
	d[x.first][x.second]=0;
//	cout<<x.first<<x.second<<endl;

	
	while(q.size())
	{
		PII t=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			int nx=dx[i]+t.first;
			int ny=dy[i]+t.second;
			if(nx>=0&&ny>=0&&nx<n&&ny<m&&(g[nx][ny]=='.'||g[nx][ny]=='@')&&d[nx][ny]==0)
			{
				if(g[nx][ny]=='@'&&f)//f为1表示为起点为Y搜到的kfc 
				{
				//	cout<<nx<<ny<<d[t.first][t.second]+1<<endl;
					
					kfc1[cnt1].x=nx;
					kfc1[cnt1].y=ny;
					kfc1[cnt1++].d=d[t.first][t.second]+1;
				}
				if(g[nx][ny]=='@'&&!f)//f为0表示为起点为M搜到的kfc 
				{
				//	cout<<nx<<ny<<d[t.first][t.second]+1<<endl;
					kfc2[cnt2].x=nx;
					kfc2[cnt2].y=ny;
					kfc2[cnt2++].d=d[t.first][t.second]+1;
				}
				d[nx][ny]=d[t.first][t.second]+1;
				q.push({nx,ny});
				
				
				
			}
		}
	}
	
	
}

int main()
{
	while(~scanf("%d%d",&n,&m))
	{	
	   cnt1=0,cnt2=0;
		PII st1,st2;
	//	getchar();
	for(int i=0;i<n;i++)
 	    for(int j=0;j<m;j++)
 	    {
 	   	    scanf(" %c",&g[i][j]);
 	   	    if(g[i][j]=='M')
			{
				st2={i,j};
			} 
			if(g[i][j]=='Y')
			{
				st1={i,j};	
			}
 	   	    
	    }
	
	
	
	 //   cout<<st2.first<<""<<st2.second<<endl;
	    
	
	
//		for(int i=0;i<n;i++)
//		{
//			for(int j=0;j<m;j++)
//			{
//				cin>>g[i][j];
//				printf("%c........",&g[i][j]);
//				if(g[i][j]=='Y')
//				{
//					st1={i,j};
//					
//					
//				}
//				if(g[i][j]=='M')
//				{
//				//	cout<<i<<j<<endl;
//					st2={i,j};
//				}
//			}
//		}
		f=1;
		bfs(st1);
		
	//	cout<<1<<endl;
		
		f=0;
		bfs(st2);
		
		
	//	cout<<cnt1<<endl;
		
		sort(kfc1,kfc1+cnt1);
		sort(kfc2,kfc2+cnt2);
		
		ll ans=0x3f3f3f3f;
		
		for(int i=0;i<cnt1;i++)
		{
			if(ans>kfc1[i].d+kfc2[i].d) 
			{
				ans=kfc1[i].d+kfc2[i].d;
			} 
		}
		cout<<ans*11<<endl;
	}
	return 0;
}

 二维数组来写

#include<string.h>
#include<stdio.h>
#include<iostream>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
char g[210][210];
int d[210][210];
int kfc[210][210];
int book[210][210];


int n,m;
typedef long long ll;
typedef pair<int,int>PII;
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};

void bfs(PII x)
{
	queue<PII>q;
	q.push(x);
	memset(d,0,sizeof(d));
    memset(book,0,sizeof(book));
	book[x.first][x.second]=1;
	
	while(q.size())
	{
		PII t=q.front();
		q.pop();
		
		
		for(int i=0;i<4;i++)
		{
			int nx=dx[i]+t.first;
			int ny=dy[i]+t.second;
			if(nx>=0&&ny>=0&&nx<n&&ny<m&&(g[nx][ny]=='.'||g[nx][ny]=='@')&&book[nx][ny]==0)
			{
				
				d[nx][ny]=d[t.first][t.second]+1;
				book[nx][ny]=1;

                if(g[nx][ny]=='@')
				{
                    kfc[nx][ny]+=d[nx][ny];
				}
		
				q.push({nx,ny});
				
				
				
			}
		}
	}
	
	
}

int main()
{
	while(~scanf("%d%d",&n,&m))
	{	
	    memset(kfc,0,sizeof(kfc));
	
		PII st1,st2;

    	for(int i=0;i<n;i++)
 	    for(int j=0;j<m;j++)
 	    {
 	   	    scanf(" %c",&g[i][j]);
 	   	    if(g[i][j]=='M')
			{
				st2={i,j};
			} 
			if(g[i][j]=='Y')
			{
				st1={i,j};	
			}
 	   	    
	    }
	
	
		bfs(st1);

		bfs(st2);
		
	
		
		int ans=0x3f3f3f3f;
		


        for(int i=0;i<n;i++)
        {
        	for(int j=0;j<m;j++)
        	{
        	//	cout<<kfc[i][j];
        		if(kfc[i][j]!=0)
        		{
        			ans=min(ans,kfc[i][j]);
				}

			}
		//	cout<<endl;
		}
		printf("%ld\n",ans*11);
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值