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;
    }
    
  • 相关阅读:
    tp5后台开发某些心得
    some note
    py数据抓取小案例(博客
    vue axios的使用
    C# WebApi POST 提交
    WebApi 返回Json
    C# WebApi 处理Area路径问题
    时间比较
    将多行数据合并为一列
    汉字转换拼音
  • 原文地址:https://www.cnblogs.com/strategist-614/p/13030541.html
Copyright © 2011-2022 走看看