hdu4584(Building bridges)-简单题

Building bridges

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


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
  
  
0 1 0 0 0 1 0 2
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAX 50
#define INF 0x3fffffff 
typedef struct{
	int x;
	int y;
}node;
node Hu[2000],Cu[2000]; 
int main(){
	int m,n;
	while(scanf("%d%d",&m,&n)!=EOF&&m!=0&&n!=0){
		int i,j,coth=0,cotc=0;
		char s[MAX];
		getchar();
		for(i=0;i<m;i++){
			gets(s);
			for(j=0;j<n;j++){
				if(s[j]=='H')
					Hu[coth].x=i,Hu[coth].y=j,coth++;
				else if(s[j]=='C')
					Cu[cotc].x=i,Cu[cotc].y=j,cotc++;
			}
		}
		int ansh,ansc,mindis=INF;
		for(i=0;i<coth;i++){
			for(j=0;j<cotc;j++){
				int temp=abs(Cu[j].x-Hu[i].x)+abs(Cu[j].y-Hu[i].y);
				if(mindis>temp)
					mindis=temp,ansh=i,ansc=j;
			}
		}
		printf("%d %d %d %d\n",Hu[ansh].x,Hu[ansh].y,Cu[ansc].x,Cu[ansc].y);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值