HDU-4584-Building bridges

来源:互联网 发布:行知实验幼儿园 编辑:程序博客网 时间:2024/05/01 19: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.

 

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

题意:岛国‘H’和岛国‘C’,‘O’代表海洋,求要建一条‘H’和‘C’的之间的桥梁,并且桥梁为最短,如果最短的有多条,则按X最小的输出,否则按Y最小输出。

其实,这道题,N,M最大为40,用暴力法也不会超时,可以不用广搜去做。

思路:首先记录所有的‘H’的坐标,然后找到每个‘H’与所有的‘C’的距离,并记录最小的距离。其次,重头搜索,当距离是已记录的最小值的时候,输出坐标即可。

AC代码:

#include<iostream>#include<stdio.h>#include<math.h>#include<stdlib.h>using namespace std;struct node//用于存放'H'的坐标,以及周围最近'C'的距离{    int x,y,min;}; int fun(int a,int b,int c,int d)//计算距离{     return abs(a-c)+abs(b-d);}int main(){        char s[41][41];    int i,j,k,n,m;    while(cin>>n>>m)    {        if(!n&&!m)            break;        struct node dis[200];//用于储存所有'H'的坐标        int p=0;        for(i=0;i<n;i++)//接收记录所有'H'的坐标        {            cin>>s[i];            for(j=0;j<m;j++)            {                 if(s[i][j]=='H')                 {                     dis[p].x=i;                     dis[p++].y=j;                 }                             }        }            int mmin=1000;            for(k=0;k<p;k++)//3层for记录每个'H'与'C'的最短距离            {                for(i=0;i<n;i++)                    for(j=0;j<m;j++)                    {                        if(s[i][j]=='C')                        {                            dis[k].min=fun(dis[k].x,dis[k].y,i,j);                            if(mmin>dis[k].min)                                mmin=dis[k].min;                        }                    }                    dis[k].min=mmin;//记录最小距离            }            for(k=0;k<p;k++)//找最小距离是的第一个'H'的坐标,输出            {                if(mmin==dis[k].min)                {                    cout<<dis[k].x<<' '<<dis[k].y<<' ';                    break;                }            }            int q=0;            for(i=0;i<n;i++)//找最小距离时的第一个'C'的坐标,输出            {                for(j=0;j<m;j++)                {                    if(mmin==fun(dis[k].x,dis[k].y,i,j)&&s[i][j]=='C')                    {                       cout<<i<<' '<<j<<endl;                          q=1;                        break;                    }                                    }                if(q)                    break;            }        }    return 0;}



原创粉丝点击