2015
07-16

# Building bridges

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 62 Accepted Submission(s): 53

Problem Description
Hululu and Cululu are two pacific ocean countries made up of many islands. These two country has so many years of friendship so they decide to build bridges to connect their islands. Now they want to build the first bridge to connect
an island of Hululu and an island of Culuu .
Their world can be considered as a matrix made up of three letters ‘H’,'C’ and ‘O’.Every ‘H’ stands for an island of Hululu, every ‘C’ stands for an island of Cululu, and ‘O’ stands for the ocean. Here is a example:

The coordinate of the up-left corner is (0,0), and the down-right corner is (4, 3). The x-direction is horizontal, and the y-direction is vertical.
There may be too many options of selecting two islands. To simplify the problem , they come up with some rules below:
1. The distance between the two islands must be as short as possible. If the two islands’ coordinates are (x1,y1) and (x2,y2), the distance is |x1-x2|+|y1-y2|.
2. If there are more than one pair of islands satisfied the rule 1, choose the pair in which the Hululu island has the smallest x coordinate. If there are still more than one options, choose the Hululu island which has the smallest y coordinate.
3.After the Hululu island is decided, if there are multiple options for the Cululu island, also make the choice by rule 2.

Input
There are multiple test cases.
In each test case, the first line contains two integers M and N, meaning that the matrix is M×N ( 2<=M,N <= 40).
Next M lines stand for the matrix. Each line has N letters.
The input ends with M = 0 and N = 0.
It’s guaranteed that there is a solution.

Output
For each test case, choose two islands, then print their coordinates in a line in following format:
x1 y1 x2 y2
x1,y1 is the coordinate of Hululu island, x2 ,y2 is the coordinate of Cululu island.

Sample Input
4 4
CHCH
HCHC
CCCO
COHO
5 4
OHCH
HCHC
CCCO
COHO
HCHC
0 0

Sample Output
0 1 0 0
0 1 0 2

Source
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
struct node {
int x,y,step;
};
int dir[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
node queue[1000000];
node prime[2000];
int visit[50][50],n,m;
node qfront,temp;
int map[50][50],minxx,startx,starty,endx,endy;
int fmin(int a,int b){if(a<b)return a;return b;}
int bfs(int sx,int sy)
{
int s=0,e=0,x,y,i;

memset(visit,0,sizeof(visit));
visit[sx][sy]=1;
queue[s].x=sx,queue[s].y=sy,queue[s].step=0;
while(s<=e)
{
qfront=queue[s];
if(queue[s].step>=minxx)
return minxx;
for(i=0;i<4;i++)
{
temp.x=qfront.x+dir[i][0];
temp.y=qfront.y+dir[i][1];
if(map[temp.x][temp.y])
{
return qfront.step+1;
}
if(!visit[temp.x][temp.y]&&temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m)
{
temp.step=qfront.step+1;
queue[e++]=temp;
}

}
s++;
}
return -1;
}
int main()
{
int i,j,hnum;
char c;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
if(m==0&&n==0)
break;
hnum=0;
for(i=0;i<n;i++)
{

for(j=0;j<m;j++)
{
c=getchar();
if(c=='H')
{
map[i][j]=0;
prime[hnum].x=i;
prime[hnum].y=j;
hnum++;
}
else if(c=='C')
{
map[i][j]=1;
}
else
{
map[i][j]=0;
}
}
getchar();
}
minxx=1000000;
for(i=0;i<hnum;i++)
{
int t=bfs(prime[i].x,prime[i].y);
if(t<minxx)
{
startx=prime[i].x;
starty=prime[i].y;
minxx=t;
endx=temp.x;
endy=temp.y;
}

}
printf("%d %d %d %d\n",startx,starty,endx,endy);

}
return 0;
}