两次搜索找最小路径和—— Find a way

Find a way

Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format:      Java class name:
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.

Sample Input

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

Sample Output

66
88
66

题意:给一幅图,有墙,有KFC,有路。两个人要去KFC约会,有很多个KFC,问两个人去一间KFC总共走的最少步数。

由于输入判断 Y M 时打错了,呜呜~~~找了两个小时的错误

方案一:题目两个人到KFC见面,两个人在不同的位置,KFC也不只一个,对每个人BFS广搜,求出每个人到各个KFC的最短距离,最后对于多个KFC地点,求出两个人到这的最小步数的和,取最小值就可以了。


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
#include<queue>
using namespace std;
int n,m;
char Map[210][210];
int dis_a[210][210],dis_b[210][210];
const int c[4][2]={-1,0,0,-1,1,0,0,1};
struct node{
	int x,y;
};
bool check(int i,int j){
	if(i<n && i>=0 && j<m && j>=0 && Map[i][j]!='#')	return true;
	return false;
}

void bfs(int xx,int yy,int num[210][210])
{
	node t,tmp;
	t.x=xx;	t.y=yy;	num[xx][yy]=0;
	queue<node> q;
	q.push(t);
	while(!q.empty()){
		t=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			tmp.x=t.x+c[i][0];
			tmp.y=t.y+c[i][1];
			if(num[tmp.x][tmp.y]==0 && check(tmp.x,tmp.y)){
				num[tmp.x][tmp.y]=num[t.x][t.y]+1;
				q.push(tmp);
			}	
		}
	}
	
}
int main()
{
	int x1,y1,x2,y2;
	while(~scanf("%d%d",&n,&m)){
		memset(Map,0,sizeof(Map));
		memset(dis_a,0,sizeof(dis_a));
		memset(dis_b,0,sizeof(dis_b));
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				cin>>Map[i][j];
				if(Map[i][j]=='Y'){
					x1=i;
					y1=j;
				}
				if(Map[i][j]=='M'){
					x2=i;
					y2=j;
				}
			}
		}
		bfs(x1,y1,dis_a);
		bfs(x2,y2,dis_b);
		int minn=INF;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(Map[i][j]=='@' && dis_a[i][j]!=0)
					minn=min(minn,dis_a[i][j]+dis_b[i][j]);					
			}
		}
		printf("%d\n",minn*11);
	}
	return 0;
}



方案二:

#include<iostream>
#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f
#include<queue>
using namespace std;
int n,m;
char Map[210][210];
int vis[210][210],dis[210][210];
const int c[4][2]={-1,0,0,-1,1,0,0,1};
struct node{
	int x,y;
	int step;
};
bool check(int i,int j){
	if(i<n && i>=0 && j<m && j>=0 && Map[i][j]!='#')	return true;
	return false;
}

void bfs(int xx,int yy,int num)
{
	memset(vis,0,sizeof(vis));
	node t,tmp;
	t.x=xx;	t.y=yy;	t.step=0;
	queue<node> q;
	q.push(t);
	vis[t.x][t.y]=1;
	while(!q.empty()){
		t=q.front();	q.pop();
		for(int i=0;i<4;i++){
			tmp=t;
			tmp.x=t.x+c[i][0];
			tmp.y=t.y+c[i][1];
			tmp.step++;
			if(!vis[tmp.x][tmp.y] && check(tmp.x,tmp.y)){
				vis[tmp.x][tmp.y]=1;
				q.push(tmp);
				if(Map[tmp.x][tmp.y]=='@'){
					if(num==1)	dis[tmp.x][tmp.y]=tmp.step;
					else	dis[tmp.x][tmp.y]+=tmp.step;
				}
			}	
		}
	}
	
}

int main()
{
	int x1,y1,x2,y2;
	while(~scanf("%d%d",&n,&m)){
		memset(Map,0,sizeof(Map));
		memset(dis,INF,sizeof(dis));
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				cin>>Map[i][j];
				if(Map[i][j]=='Y'){
					x1=i;
					y1=j;
				}
				if(Map[i][j]=='M'){
					x2=i;
					y2=j;
				}
			}
		}
		bfs(x1,y1,1);
		bfs(x2,y2,2);
		int minn=INF;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(Map[i][j]=='@' && dis[i][j]<minn)
					minn=dis[i][j];
			}
		}
		printf("%d\n",minn*11);
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黎轩栀海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值