Wang Xifeng's Little Plot - HDU 5024 搜索

来源:互联网 发布:java中private的作用 编辑:程序博客网 时间:2024/05/16 12:15

Wang Xifeng's Little Plot

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


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
 

题意:找到一条最多只有一个90度拐角的路,使得路径最长。

思路:先搜索找到一个直线的最长距离,然后再考虑拐弯。

AC代码如下:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char s[110][110];int ox[9]={0,-1,-1,0,1,1,1,0,-1},oy[9]={0,0,1,1,1,0,-1,-1,-1},ans,dir[20];int t,rem[110][110][9],vis[110][110][9];void dfs(int xx,int yy,int x,int y,int k,int l){    if(vis[x][y][k]==t && rem[x][y][k]>=2)    {        int ret=rem[x][y][k]-1;        dfs(xx,yy,x+ox[k]*ret,y+oy[k]*ret,k,l+ret);    }    else if(s[x+ox[k]][y+oy[k]]!='#')      dfs(xx,yy,x+ox[k],y+oy[k],k,l+1);        if(vis[xx][yy][k]!=t)        {            vis[xx][yy][k]=t;            rem[xx][yy][k]=l;        }        else          rem[xx][yy][k]=max(rem[xx][yy][k],l);}int main(){    int n,m,i,j,k,x,y,ret;    dir[0]=8;    for(i=1;i<=8;i++)    {        dir[i]=i;        dir[i+8]=i;    }    while(~scanf("%d",&n) && n)    {        t++;        for(i=1;i<=n;i++)           scanf("%s",s[i]+1);        for(i=0;i<=n+1;i++)        {            s[0][i]='#';            s[n+1][i]='#';            s[i][0]='#';            s[i][n+1]='#';        }        ans=2;        for(i=1;i<=n;i++)           for(j=1;j<=n;j++)              if(s[i][j]=='.')                for(k=1;k<=8;k++)                   dfs(i,j,i,j,k,1);        for(i=1;i<=n;i++)           for(j=1;j<=n;j++)              if(s[i][j]=='.')                for(k=1;k<=8;k++)                {                    ret=rem[i][j][k]-1;                    if(ret==0)                      continue;                    else                    {                        x=i+ox[k]*ret;                        y=j+oy[k]*ret;                        ans=max(ans,ret+rem[x][y][dir[(k+2)%8]]);                        ans=max(ans,ret+rem[x][y][dir[(k+6)%8]]);                    }                }        printf("%d\n",ans);    }}





0 0
原创粉丝点击