题目链接:http://blog.csdn.net/u012860063
http://acm.hdu.edu.cn/showproblem.php?pid=4584
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.
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
Source
2013ACM-ICPC杭州赛区全国邀请赛
Recommend
We have carefully selected several similar problems for you: 4846 4845 4844 4843 4842
题目大意:给出一张图,图由‘H',’O'和‘C'组成,找到哈夫曼距离最小的’H'和‘C',即所有的’H'和‘C'中哈夫曼距离最小的那对,如果相同按照’H'的行坐标、列坐标、‘C’的行坐标、列坐标输出。
解题思路:暴力搜索,先将所有的H和C分别处理出来,然后暴力枚举。
代码如下:
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define PI acos(-1.0)
#define INF 0x3fffffff
struct D
{
int x,y;
}HH[2047],CC[2047];
bool cmp(D a , D b)
{
if(a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
int main()
{
int m,n,i,j,k,ans1,ans2;
char map[47][47];
while(~scanf("%d%d",&n,&m))
{
ans1 = ans2 = 0;
if(n == 0 && m == 0)
break;
for(i = 0 ; i < n ; i++)
{
scanf("%s",map[i]);
}
for(i = 0 ; i < n ; i++)
{
for(j = 0 ; j < m ; j++)
{
if(map[i][j] == 'H')
{
HH[ans1].x = i;
HH[ans1].y = j;
ans1++;
}
else if(map[i][j] == 'C')
{
CC[ans2].x = i;
CC[ans2].y = j;
ans2++;
}
}
}
sort(HH,HH+ans1,cmp);
sort(CC,CC+ans2,cmp);
int ans = INF;int xx,yy;
for( i = 0 ; i < ans1 ; i++)
{
for(j = 0 ; j < ans2 ; j++)
{
int t = abs(CC[j].x-HH[i].x)+abs(CC[j].y-HH[i].y);
if(t < ans)
{
ans = t;
xx = i ;
yy = j;
}
}
}
printf("%d %d %d %d\n",HH[xx].x,HH[xx].y,CC[yy].x,CC[yy].y);
}
return 0;
}