HDU 5024

来源:互联网 发布:用nginx搭建web服务器 编辑:程序博客网 时间:2024/05/17 23:24

      一道搜索题,挺好的一题,可惜开始理解错题目的意思了。

      题目里说“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”,也就是只拐一次而且拐弯的角度都是90度。开始不知道只拐90度在能走八个方向的图里怎么理解,后来看到一篇博文里的图片(链接:HDU 5024),才明白其含义。

      另外,这道题的代码我参考了http://chengzi.aemiot.com/hdu-5024.html。

代码(C++):

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define MAX 109using namespace std;const int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};int n,len[8];char matrix[MAX][MAX];int search(int a,int b){    int i,tmp,res,x,y;    res=0;    memset(len,0,sizeof(len));    for(i=0;i<8;i++)    {        x=a;        y=b;        while(x>=0&&y>=0&&x<n&&y<n&&matrix[x][y]=='.')        {            len[i]++;            x+=dir[i][0];            y+=dir[i][1];        }    }    for(i=0;i<8;i++)    {        tmp=len[i]+len[(i+2)%8]-1;        res=max(tmp,res);    }    return res;}int main(){    //freopen("in.txt","r",stdin);    int i,j,ans;    while(scanf("%d",&n)&&n!=0)    {        for(i=0;i<n;i++) scanf("%s",matrix[i]);        ans=0;        for(i=0;i<n;i++)        {            for(j=0;j<n;j++)            {                if(matrix[i][j]=='.')                {                    ans=max(search(i,j),ans);                }            }        }        printf("%d\n",ans);    }    return 0;}

题目(http://acm.hdu.edu.cn/showproblem.php?pid=5024):

Wang Xifeng's Little Plot

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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

0 0
原创粉丝点击