ZJNU 1614 Three good friends

 题目大意:找出使F、Q、J三点连通的最少所需步数。。。

Description

FF,JJ and QQ are three good friends. But their house are very far away form each others.Their want to build a road so that their houses could connect together.
"F" on the map indicate FF's house,"J" indicates JJ's house ,"Q" indicates QQ's house,"." indicates there could build road on it. "X" indicates a barrier,there can't build road.
Now, tell you the information of the map,please help three lovely girls to calculate the minimum cost to build the road;

Input

The first line with two numbers n and m ( 0 < n , m <= 100 )
stands for the size of the map;then n lines follow ,each line with m characters.
There are exact one "J",one "X" and one "F".

Output

If could build please output the minimum cost;others output"Impossible";

Sample Input

 
 
3 3 F.X ... J.Q 3 3 F.. ..X JXQ

Sample Output

2
Impossible
 
 
#include<stdio.h>
#include<string.h>
#define M 105
#define oo 1000000
/*开始一直WA,后来才知道,原来忽略了最严重的一个问题,就是忽略了起点不一定是F、J、Q。。。*/

char str[M][M];
int mark1[M][M],mark2[M][M],mark3[M][M];
int n,m,sx[3],sy[3];
struct node{
	int x,y,step;
}Q[M*M*10],s,p;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};

int judge(int x,int y){
	return (x>=0 && x<n && y>=0 && y<m);
}

void bfs1(int sxx,int syy){
	int i,j,L,H;
	L=H=0;
	s.x=sxx; s.y=syy; s.step=0;
	Q[H++]=s;
	while(L<H){
		p=Q[L++];
		for(i=0;i<4;i++){
			s=p;
			s.x=p.x+dx[i]; s.y=p.y+dy[i];
			s.step++;
			if(judge(s.x,s.y) && mark1[s.x][s.y]>s.step){
				mark1[s.x][s.y]=s.step;
				Q[H++]=s;
			}
		}
	}
	mark1[sxx][syy]=0;/*起点得重新赋值*/
}

void bfs2(int sxx,int syy){
	int i,j,L,H;
	L=H=0;
	s.x=sxx; s.y=syy; s.step=0;
	Q[H++]=s;
	while(L<H){
		p=Q[L++];
		for(i=0;i<4;i++){
			s=p;
			s.x=p.x+dx[i]; s.y=p.y+dy[i];
			s.step++;
			if(judge(s.x,s.y) && mark2[s.x][s.y]>s.step){
				mark2[s.x][s.y]=s.step;
				Q[H++]=s;
			}
		}
	}
	mark2[sxx][syy]=0;
}

void bfs3(int sxx,int syy){
	int i,j,L,H;
	L=H=0;
	s.x=sxx; s.y=syy; s.step=0;
	Q[H++]=s;
	while(L<H){
		p=Q[L++];
		for(i=0;i<4;i++){
			s=p;
			s.x=p.x+dx[i]; s.y=p.y+dy[i];
			s.step++;
			if(judge(s.x,s.y) && mark3[s.x][s.y]>s.step){
				mark3[s.x][s.y]=s.step;
				Q[H++]=s;
			}
		}
	}
	mark3[sxx][syy]=0;
}

int main(){
	int i,j,k;
	int min;
	while(scanf("%d%d",&n,&m)!=EOF){
		k=0;
		for(i=0;i<n;i++){
			scanf("%s",str[i]);
			for(j=0;j<m;j++){
				if(str[i][j]=='X'){
					mark1[i][j]=mark2[i][j]=mark3[i][j]=-1;
				}
				else {
					mark1[i][j]=mark2[i][j]=mark3[i][j]=oo;
				}
				if(str[i][j]=='F' || str[i][j]=='J' || str[i][j]=='Q'){
					sx[k]=i; sy[k++]=j;
				}
			}
		}
		bfs1(sx[0],sy[0]);
		bfs2(sx[1],sy[1]);
		bfs3(sx[2],sy[2]);	
		min=5*oo;  // int ii=0,jj=0;	
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				if(mark1[i][j]>=0 && mark2[i][j]>=0 && mark3[i][j]>=0){
					if(mark1[i][j]+mark2[i][j]+mark3[i][j]<min){
						min=mark1[i][j]+mark2[i][j]+mark3[i][j];
						//ii=i; jj=j;
					}
				}
			}
		}
		
		
		//printf("i=%d  j=%d\n",ii,jj);
		/*for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				printf("%d  ",mark1[i][j]+mark2[i][j]+mark3[i][j]);
			}
			puts("");
		}
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				printf("%d  ",mark1[i][j]);
			}
			puts("");
		}
		puts("********************");
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				printf("%d  ",mark2[i][j]);
			}
			puts("");
		}
		puts("********************");
		for(i=0;i<n;i++){
			for(j=0;j<m;j++){
				printf("%d  ",mark3[i][j]);
			}
			puts("");
		}
		puts("********************");*/
		if(min>=oo) puts("Impossible");
		else printf("%d\n",min-2);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值