Building bridges
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 45 Accepted Submission(s): 39
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.
Please help their people to build the bridge.
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:
![](https://i-blog.csdnimg.cn/blog_migrate/63f924edd000cf8c6c2a96f6a870028d.jpeg)
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.
Please help their people to build the bridge.
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.
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.
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
水题。。。
胡乱弄
1 /* ********************************************** 2 Author : kuangbin 3 Created Time: 2013/8/10 12:14:09 4 File Name : F:\2013ACM练习\比赛练习\2013杭州邀请赛重现\1010.cpp 5 *********************************************** */ 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <math.h> 17 #include <stdlib.h> 18 using namespace std; 19 struct Point 20 { 21 int x,y; 22 Point(int _x = 0,int _y = 0) 23 { 24 x = _x; y = _y; 25 } 26 }; 27 int dis(Point a,Point b) 28 { 29 return abs(a.x-b.x)+abs(a.y-b.y); 30 } 31 Point p1[2000]; 32 Point p2[2000]; 33 char str[50][50]; 34 int main() 35 { 36 //freopen("in.txt","r",stdin); 37 //freopen("out.txt","w",stdout); 38 int n,m; 39 while(scanf("%d%d",&n,&m) == 2) 40 { 41 if(n == 0 && m == 0)break; 42 for(int i = 0;i < n;i++) 43 scanf("%s",str[i]); 44 int uN = 0, vN = 0; 45 for(int i = 0;i < n;i++) 46 for(int j = 0;j < m;j++) 47 { 48 if(str[i][j] == 'H')p1[uN++] = Point(i,j); 49 else if(str[i][j] == 'C') p2[vN++] = Point(i,j); 50 } 51 int ans1 = 0,ans2 = 0; 52 int Min = 100000000; 53 for(int i = 0;i < uN;i++) 54 for(int j = 0;j < vN;j++) 55 { 56 if(dis(p1[i],p2[j]) < Min) 57 { 58 Min = dis(p1[i],p2[j]); 59 ans1 = i; 60 ans2 = j; 61 } 62 } 63 printf("%d %d %d %d\n",p1[ans1].x,p1[ans1].y,p2[ans2].x,p2[ans2].y); 64 } 65 return 0; 66 }