HDU 5024 Wang Xifeng's Little Plot(2014广州网络赛1003)

写了1h的DFS,简直被自己的代码吓哭了。。不过起码还是思路清晰,QUQ~

说一下题意吧: 题意是求一条最长路,最多能经过一次转弯,并且其角度只能为90度。

拿第一个样例来说:(0,1)->(1,2)->【转弯】(2,1) ,所以答案是3.

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

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<string>
#include<map>
#define N 111
#define LL long long
using namespace std;

int n,ans;
char g[N][N];
//八个dfs因为要相互调用,故用类包装。
class dfs
{
public:
    //x,y,s分别表示坐标和路长,v是记录发生过几次转向。
    void dfsl(int x,int y,int s,int v) //向左。
    {
        ans=max(ans,s);
        if(y-1>=0 && g[x][y-1]=='.') //还能向左
            dfsl(x,y-1,s+1,v);
        else
        {
            if(v<1)//否则向上或向下
            {
                if(x-1>=0 && g[x-1][y]=='.') dfsu(x-1,y,s+1,v+1);
                if(x+1<n && g[x+1][y]=='.') dfsd(x+1,y,s+1,v+1);
            }
            return ;
        }
    }
    void dfsr(int x,int y,int s,int v) //向右。
    {
        ans=max(ans,s);
        if(y+1<n && g[x][y+1]=='.')
            dfsr(x,y+1,s+1,v);
        else
        {
            if(v<1)
            {
                if(x-1>=0 && g[x-1][y]=='.') dfsu(x-1,y,s+1,v+1);
                if(x+1<n && g[x+1][y]=='.') dfsd(x+1,y,s+1,v+1);
            }
            return ;
        }
    }
    void dfsu(int x,int y,int s,int v) //向上。
    {
        ans=max(ans,s);
        if(x-1>=0 && g[x-1][y]=='.')
            dfsu(x-1,y,s+1,v);
        else
        {
            if(v<1)
            {
                if(y-1>=0 && g[x][y-1]=='.') dfsl(x,y-1,s+1,v+1);
                if(y+1<n && g[x][y+1]=='.') dfsr(x,y+1,s+1,v+1);
            }
            return ;
        }
    }
    void dfsd(int x,int y,int s,int v) //向下。
    {
        ans=max(ans,s);
        if(x+1<n && g[x+1][y]=='.')
            dfsd(x+1,y,s+1,v);
        else
        {
            if(v<1)
            {
                if(y-1>=0 && g[x][y-1]=='.') dfsl(x,y-1,s+1,v+1);
                if(y+1<n && g[x][y+1]=='.') dfsr(x,y+1,s+1,v+1);
            }
            return ;
        }
    }
    void dfslu(int x,int y,int s,int v) //向左上。
    {
        ans=max(ans,s);
        if(x-1>=0 && y-1>=0 && g[x-1][y-1]=='.')
            dfslu(x-1,y-1,s+1,v);
        else
        {
            if(v<1)
            {
                if(x+1<n && y-1>=0 && g[x+1][y-1]=='.')
                    dfsld(x+1,y-1,s+1,v+1);
                if(x-1>=0 && y+1<n && g[x-1][y+1]=='.')
                    dfsru(x-1,y+1,s+1,v+1);
            }
            return ;
        }
    }
    void dfsrd(int x,int y,int s,int v) //向右下。
    {
        ans=max(ans,s);
        if(x+1<n && y+1<n && g[x+1][y+1]=='.')
            dfsrd(x+1,y+1,s+1,v);
        else
        {
            if(v<1)
            {
                if(x+1<n && y-1>=0 && g[x+1][y-1]=='.')
                    dfsld(x+1,y-1,s+1,v+1);
                if(x-1>=0 && y+1<n && g[x-1][y+1]=='.')
                    dfsru(x-1,y+1,s+1,v+1);
            }
            return ;
        }
    }
    void dfsru(int x,int y,int s,int v) //向右上。
    {
        ans=max(ans,s);
        if(x-1>=0 && y+1<n && g[x-1][y+1]=='.')
            dfsru(x-1,y+1,s+1,v);
        else
        {
            if(v<1)
            {
                if(x-1>=0 && y-1>=0 && g[x-1][y-1]=='.')
                    dfslu(x-1,y-1,s+1,v+1);
                if(x+1<n && y+1<n && g[x+1][y+1]=='.')
                    dfsrd(x+1,y+1,s+1,v+1);
            }
            return ;
        }
    }
    void dfsld(int x,int y,int s,int v) //向左下。
    {
        ans=max(ans,s);
        if(x+1<n && y-1>=0 && g[x+1][y-1]=='.')
            dfsld(x+1,y-1,s+1,v);
        else
        {
            if(v<1)
            {
                if(x-1>=0 && y-1>=0 && g[x-1][y-1]=='.')
                    dfslu(x-1,y-1,s+1,v+1);
                if(x+1<n && y+1<n && g[x+1][y+1]=='.')
                    dfsrd(x+1,y+1,s+1,v+1);
            }
            return ;
        }
    }
};

int main()
{
    while(~scanf("%d",&n),n)
    {
        for(int i=0;i<n;i++)
            scanf("%s",g[i]);
        dfs q;
        ans=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(g[i][j]=='.')
                {
                    if(j-1>=0 && g[i][j-1]=='.')
                        q.dfsl(i,j-1,1,0);
                    if(j+1<n && g[i][j+1]=='.')
                        q.dfsr(i,j+1,1,0);
                    if(i-1>=0 && g[i-1][j]=='.')
                        q.dfsu(i-1,j,1,0);
                    if(i+1<n && g[i+1][j]=='.')
                        q.dfsd(i+1,j,1,0);
                    if(i-1>=0 && j-1>=0 && g[i-1][j-1]=='.')
                        q.dfslu(i-1,j-1,1,0);
                    if(i+1<n && i+1<n && g[i+1][j+1]=='.')
                        q.dfsrd(i+1,j+1,1,0);
                    if(i+1<n && j-1>=0 && g[i+1][j-1]=='.')
                        q.dfsld(i+1,j-1,1,0);
                    if(i-1>=0 && j+1<n && g[i-1][j+1]=='.')
                        q.dfsru(i-1,j+1,1,0);
                }
            }
        }
        printf("%d\n",ans+1); //ans+1.因为我没算起始点。
    }
    return 0;
}

HDU 5024 Wang Xifeng's Little Plot(2014广州网络赛1003)

时间: 2024-10-02 05:09:39

HDU 5024 Wang Xifeng's Little Plot(2014广州网络赛1003)的相关文章

HDU 5024 Wang Xifeng&#39;s Little Plot(广州网络赛C题)

HDU 5024 Wang Xifeng's Little Plot 题目链接 思路:先利用记忆化搜索预处理出每个结点对应8个方向最远能走多远,然后枚举拐点记录最大值即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int d[8][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 1}, {1,

HDU 5024 Wang Xifeng&#39;s Little Plot (搜索)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 157    Accepted Submission(s): 105 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

hdu 5024 Wang Xifeng&#39;s Little Plot【暴力dfs,剪枝】

2014年广州网络赛的C题,也是水题.要你在一个地图中找出一条最长的路,这条路要保证最多只能有一个拐角,且只能为90度 我们直接用深搜,枚举每个起点,每个方向进行dfs,再加上剪枝. 但是如果直接写的话,那一定会特别麻烦,再加上方向这一属性也是我们需要考虑的方面,我们将从别的地方到当前点的方向编一个号:往右为0,如下图顺时针顺序编号 (往右下方向为1,往下为2......以此类推) 我们知道了当前点的坐标,来到当前点的方向,以及到目前有没有拐弯,这几个属性之后,就可以记忆化搜索了,用一个四维数组

HDU 5024 Wang Xifeng&#39;s Little Plot(暴力枚举+瞎搞)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5024 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. Thi

[ACM] HDU 5024 Wang Xifeng&#39;s Little Plot (构造,枚举)

Wang Xifeng's Little Plot 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 Qin

HDU 5024 Wang Xifeng&#39;s Little Plot (枚举 + DFS记忆化搜索)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 513    Accepted Submission(s): 338 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

hdu 5024 Wang Xifeng&#39;s Little Plot (dfs+暴力)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 194    Accepted Submission(s): 131 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

HDU - 5024 Wang Xifeng&#39;s Little Plot

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.

HDU 5024 Wang Xifeng&#39;s Little Plot (bfs)

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.