首页 > ACM题库 > HDU-杭电 > HDU 4584-Building bridges[解题报告]HOJ
2015
09-17

HDU 4584-Building bridges[解题报告]HOJ

Building bridges

问题描述 :

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:

Coffee shop in Jioufen

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

0 1 0 0
0 1 0 2

不能一搜到C就直接return,根据题意限制可能得到相同distance但不合要求的答案

比赛时看错题,艹,把x,y弄反了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;

char str[44][44];
int vis[44][44],dist[44][44];
int dir[4][2]={{0,-1},{0,1},{-1,0},{1,0}};
int qx[20000],qy[20000];
int m,n;

int solve(int r,int c,int &resr,int &resc)
{
    int i=r;int j=c;
    memset(vis,0,sizeof(vis));
    memset(dist,127,sizeof(dist));
    int head=0;int rear=0;
    qx[rear]=i;qy[rear++]=j;
    vis[i][j]=1;dist[i][j]=0;
    while(head<rear)
    {
        int ux=qx[head];
        int uy=qy[head++];
        for(int k=0;k<4;++k)
        {
            int nx=ux+dir[k][0];
            int ny=uy+dir[k][1];
            if(nx>=0&&nx<m&&ny>=0&&ny<n&&!vis[nx][ny])
            {
                dist[nx][ny]=dist[ux][uy]+1;
                vis[nx][ny]=1;
                qx[rear]=nx;qy[rear++]=ny;
                /*if(str[nx][ny]=='C')
                {
                    resr=nx;resc=ny;
                    return dist[nx][ny];
                }*/
            }
        }
    }
    int ans=100000;
    for(int i=0;i<m;++i)
        for(int j=0;j<n;++j)
            if(str[i][j]=='C'&&ans>dist[i][j])
        {
            ans=dist[i][j];
            resr=i;resc=j;
        }
    return ans;
}
int main ()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        if(m==0&&n==0) break;
        for(int i=0;i<m;++i)
            scanf("%s",str[i]);
        int ans=50*50;
        int x1,y1,x2,y2,tx,ty;
        for(int i=0;i<m;++i)
            for(int j=0;j<n;++j)
            if(str[i][j]=='H')
        {
            int res=solve(i,j,tx,ty);
            if(ans>res)
            {
                ans=res;
                x1=i;y1=j;
                x2=tx;y2=ty;
            }
        }
        printf("%d %d %d %d\n",x1,y1,x2,y2);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/jackyguo1992/article/details/9883495