Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

DFS+记忆化搜都TLE,醉了,只能看大牛dp的解题报告了

Posted by kuaichenyang at 2015-08-14 09:36:56 on Problem 1992 and last updated at 2015-08-14 09:37:19
#include <iostream>
#include <cstdio>

using namespace std;

const int _MAXN=1005;
const int dirx[4]={0,0,1,-1};
const int diry[4]={1,-1,0,0};

int r,s;
char map[_MAXN][_MAXN];
bool used[_MAXN][_MAXN];
int dp[_MAXN][_MAXN];
int ans;

bool ok(int x,int y)
{
    return x>=0&&x<r&&y>=0&&y<s;
}

int dfs(int x,int y)
{
    if(map[x][y]=='#')
        return dp[x][y]=0;
    if(x==r-1&&y==s-1)
    {
        ans++;
        return dp[x][y]=1;
    }
    if(!dp[x][y])
        return 0;
    int X=0;
    for(int i=0;i<4;i++)
    {
        int tx=x+dirx[i];
        int ty=y+diry[i];
        if(ok(tx,ty)&&false==used[tx][ty])
        {
            used[tx][ty]=true;
            if(dfs(tx,ty))
                X++;
            used[tx][ty]=false;
        }
    }
    if(!x)
        dp[x][y]=0;
    return dp[x][y];
}

int main()
{
    int Z;
    for(cin>>Z;Z-->0;)
    {
        cin>>r>>s;
        getchar();
        for(int i=0;i<r;i++)
            gets(map[i]);
        for(int i=0;i<_MAXN;i++)
            for(int j=0;j<_MAXN;j++)
                used[i][j]=false,dp[i][j]=1;
        ans=0;
        used[0][0]=true;
        dfs(r-1,0);
        cout<<"Existuje "<<ans<<" ruznych cest."<<endl;
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator