HDU 5024 Wang Xifeng's Little Plot(DFS)

本文介绍了一种基于《红楼梦》中大观园地图的最短路径算法问题,通过搜索算法寻找两个角色房间之间的最长直线路径,同时考虑路径转向限制。文章详细展示了如何通过DFS深度优先搜索来解决这一问题,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Wang Xifeng's Little Plot

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


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
3 4 3 5

分析:对于所有点都按题目的要求从多个方向进行搜索,处理一下转90度的地方
            meo[x][y][dirx+1][diry+1]记录在[x][y]位置上方向为dirx,diry时的最大值,
            如果到了这个位置的时候小于这个最大值,就不再进行后面的操作
代码如下:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
char map1[110][110];
int dir[8][2]={1,0,-1,0,0,1,0,-1,1,1,1,-1,-1,1,-1,-1};
int vis[110][110];
int meo[110][110][3][3];
int maxx;
 int n;
bool check(int x,int y)
{
    if(x>=1&&x<=n&&y>=1&&y<=n&&!vis[x][y]&&map1[x][y]=='.')
    return true;
     return false;
}
void  dfs(int x,int y,int step,int flag,int dirx,int diry)
{
    if(meo[x][y][dirx+1][diry+1]>step)
    return;
    meo[x][y][dirx+1][diry+1]=step;
    maxx=max(step,maxx);
    int nx=x+dirx;
    int ny=y+diry;
    //  cout<<x<<" "<<y<<" "<<step<<" "<<nx<<" "<<ny<<" "<<dirx<<" "<<diry<<endl;
    if(check(nx,ny)&&!vis[nx][ny])
    {
       vis[nx][ny]=1;
       dfs(nx,ny,step+1,flag,dirx,diry);
       vis[nx][ny]=0;
    }
    if(flag==0)
    {
      if(diry==0)
      {
        if(check(x,y+1)&&!vis[x][y+1]){
        vis[x][y+1]=1;
        dfs(x,y+1,step+1,1,0,1);
        vis[x][y+1]=0;
          }

         if(check(x,y-1)&&!vis[x][y-1]){
        vis[x][y-1]=1;
        dfs(x,y-1,step+1,1,0,-1);
        vis[x][y-1]=0;
         }
      }
      else if(dirx==0)
      {
           if(check(x+1,y)&&!vis[x+1][y]){
          vis[x+1][y]=1;
          dfs(x+1,y,step+1,1,1,0);
          vis[x+1][y]=0;
           }

       if(check(x-1,y)&&!vis[x-1][y]){
        vis[x-1][y]=1;
        dfs(x-1,y,step+1,1,-1,0);
        vis[x-1][y]=0;
       }
      }
      else if(dirx*diry==-1)
       {
        if(check(x-1,y-1)&&!vis[x-1][y-1]){
        vis[x-1][y-1]=1;
        dfs(x-1,y-1,step+1,1,-1,-1);
        vis[x-1][y-1]=0;
        }

         if(check(x+1,y+1)&&!vis[x+1][y+1]){
        vis[x+1][y+1]=1;
        dfs(x+1,y+1,step+1,1,1,1);
        vis[x+1][y+1]=0;
         }
       }
       else if(dirx*diry==1)
       {
            if(check(x+1,y-1)&&!vis[x+1][y-1]){
        vis[x+1][y-1]=1;
        dfs(x+1,y-1,step+1,1,1,-1);
        vis[x+1][y-1]=0;
        }

         if(check(x-1,y+1)&&!vis[x-1][y+1]){
        vis[x-1][y+1]=1;
        dfs(x-1,y+1,step+1,1,-1,1);
        vis[x-1][y+1]=0;
        }
       }
    }
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
         break;
        memset(vis,0,sizeof(vis));
        memset(meo,0,sizeof(meo));
        maxx=0;
      for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
         scanf(" %c",&map1[i][j]);
     for(int i=1;i<=n;i++)
       for(int j=1;j<=n;j++){
          if(map1[i][j]=='.')
        for(int k=0;k<8;k++)
        dfs(i,j,1,0,dir[k][0],dir[k][1]);
        }
        printf("%d\n",maxx);
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/a249189046/p/7992638.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值