ZJNU 1614 Three good friends

来源:互联网 发布:迭代器遍历数组 编辑:程序博客网 时间:2024/04/29 22:25
 题目大意:找出使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 3F.X...J.Q3 3F....XJXQ

Sample Output

2Impossible
 
 
#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;}

原创粉丝点击