zoukankan      html  css  js  c++  java
  • P1535

    P1535

    P1535

    记忆化搜索

    (dp[i][j][t]) 表示从 (i,j) 开始走还剩下 (t) 秒时方案数

    那么开始时就是 (dfs(stx,sty,t))

    到达 (edx,edy,0) 时算一种路线

    那么整个的结构就很清晰了:

        if(judge(x + 1,y)) ans += dfs(x + 1,y,t - 1);
        if(judge(x - 1,y)) ans += dfs(x - 1,y,t - 1);
        if(judge(x,y + 1)) ans += dfs(x,y + 1,t - 1);
        if(judge(x,y - 1)) ans += dfs(x,y - 1,t - 1);
    

    最终的答案是: (dp[stx][sty][t])

    int n,m,t;
    int dp[N][N][20];
    char s[N][N];
    int stx,sty,edx,edy;
    bool judge(int x,int y)
    {
        if(x >= 1 && x <= n && y >= 1 && y <= m && s[x][y] == '.') return true;
        else return false;
    }
    int dfs(int x,int y,int t)
    {
        if(dp[x][y][t] != -1) return dp[x][y][t];
        if(t < 0) return 0;
        if(x == edx && y == edy && t == 0) return 1;
        int ans = 0;
        if(judge(x + 1,y)) ans += dfs(x + 1,y,t - 1);
        if(judge(x - 1,y)) ans += dfs(x - 1,y,t - 1);
        if(judge(x,y + 1)) ans += dfs(x,y + 1,t - 1);
        if(judge(x,y - 1)) ans += dfs(x,y - 1,t - 1);
        return (dp[x][y][t] = ans); 
    }
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(NULL);
        cin >> n >> m >> t;
        memset(dp,-1,sizeof dp);
        for(int i =1;i <= n;i++)
        {
            for(int j =1;j <= m;j++)
                cin >> s[i][j];
        }
        cin >> stx >> sty >> edx >> edy;
        cout << dfs(stx,sty,t) << endl;
        return 0;
    }
    
  • 相关阅读:
    SQL SERVER DBCC命令参考
    Sqlserver 死锁问题
    收集面试题目DB
    收集面试题目Net
    [转]Virtual PC 2007虚拟机上安装Ubuntu 8.10桌面版
    Tcl/tk基础-2
    【转】内存详解
    [转]C# P2P与NAT技术之二
    泛型跟KeyNotFoundException
    用InstallAware 9制作BDE安装程序
  • 原文地址:https://www.cnblogs.com/strategist-614/p/13030541.html
Copyright © 2011-2022 走看看