您的位置:首页 > Web前端

HDU 5024 Wang Xifeng's Little Plot

2015-05-03 12:18 302 查看

HDU 5024 Wang Xifeng's Little Plot

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 453    Accepted Submission(s): 306

Problem Description

《Dream of the Red Chamber》(also 《The Story of the Stone》) is one of the Four Great Classical Novels of Chinese literature, and it is commonly regarded as the best one. This novel was created in Qing Dynasty, by Cao Xueqin. But the last 40 chapters of the original version is missing, and that part of current version was written by Gao E. There is a heart breaking story saying that after Cao Xueqin died, Cao's wife burned the last 40 chapter manuscript for heating because she was desperately poor. This story was proved a rumor a couple of days ago because someone found several pages of the original last 40 chapters written by Cao. 

In the novel, Wang Xifeng was in charge of Da Guan Yuan, where people of Jia family lived. It was mentioned in the newly recovered pages that Wang Xifeng used to arrange rooms for Jia Baoyu, Lin Daiyu, Xue Baochai and other teenagers. Because Jia Baoyu was the most important inheritor of Jia family, and Xue Baochai was beautiful and very capable , Wang Xifeng didn't want Jia Baoyu to marry Xue Baochai, in case that Xue Baochai might take her place. So, Wang Xifeng wanted Baoyu's room and Baochai's room to be located at two ends of a road, and this road should be as long as possible. But Baoyu was very bad at directions, and he demanded that there could be at most one turn along the road from his room to Baochai's room, and if there was a turn, that turn must be ninety degree. There is a map of Da Guan Yuan in the novel, and redists (In China English, one whose job is studying 《Dream of the Red Chamber》is call a "redist") are always arguing about the location of Baoyu's room and Baochai's room. Now you can solve this big problem and then become a great redist.

 

Input

The map of Da Guan Yuan is represented by a matrix of characters '.' and '#'. A '.' stands for a part of road, and a '#' stands for other things which one cannot step onto. When standing on a '.', one can go to adjacent '.'s through 8 directions: north, north-west, west, south-west, south, south-east,east and north-east.

There are several test cases.

For each case, the first line is an integer N(0<N<=100) ,meaning the map is a N × N matrix.

Then the N × N matrix follows.

The input ends with N = 0.

 

Output

For each test case, print the maximum length of the road which Wang Xifeng could find to locate Baoyu and Baochai's rooms. A road's length is the number of '.'s it includes. It's guaranteed that for any test case, the maximum length is at least 2.

 

Sample Input

3#.###...#3...##...#3...###..#3...##....0

 

Sample Output

3435

 

 

思路分析

一个来源于红楼梦的难题,意思是王熙凤不想让贾宝玉和薛宝钗住的太近,要让他们的房子尽可能分开的远,但是贾宝玉又比较脑残,只能直走并且最多只能拐一个直角的弯,问王熙凤怎样安排他们俩的住所才能满足以上的条件。其中在地图上的某一点可以朝相邻的八个方向走动,“.”表示能走,“#”表示不能走。该题需要枚举地图上每一个点能满足题意的最大长度,然后求出所有点对应长度的最大值。在求每个点满足题意的最大长度时,即求以该点为起点按照某一方向的最长路径加上与该方向终点相垂直的方向的最长路径。

方向的处理可以由一个dir[8][2]数组搞定,分别表示左,左上,上,右上,右,右下,下,左下。

代码如下

#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <cstring>

#include <string>

#include <set>

#include <vector&g
4000
t;

#include <stack>

#include <queue>

#include <algorithm>

using namespace std;

char mp[110][110];

int dir[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};//表示每个点走的八个方向

int ds[8];//表示在该点按某个方向走的最远距离

int res[8];//表示在该点最多经过一次拐弯走的最远距离

int main()

{

    int n;

    //freopen("A.txt","r",stdin);

    while (scanf("%d",&n) != EOF && n)

    {

        getchar();

        for (int i=0;i<n;i++)

        {

            for (int j=0;j<n;j++)

                scanf("%c",&mp[i][j]);

            getchar();

        }

        int ans=-1;

        for (int i=0;i<n;i++)

            for (int j=0;j<n;j++)

        {

            if (mp[i][j] == '.')//假设从该点起步

            {

                memset(ds,0,sizeof(ds));

                memset(res,0,sizeof(res));

                for (int k=0;k<8;k++)//k代表dir数组中的方向第k个方向

                {

                    int x=i;

                    int y=j;

                    while (x>=0 && x<n && y>=0 && y<n && mp[x][y] == '.')//第k个方向能走的长度

                    {

                        ds[k]++;

                        x+=dir[k][0];

                        y+=dir[k][1];//x,y按照第k个方向“走下去”

                    }

                }

                for (int k=0;k<8;k++)

                {

                    res[k]=ds[k]+ds[(k+2)%8];//表示一对互相垂直的方向组合,即一条最多能拐一个弯的路长

                    ans=max(ans,res[k]);

                }

 

            }

        }

        printf("%d\n",ans-1);//减去重叠的

    }

 

    return 0;

}

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: