zoukankan      html  css  js  c++  java
  • leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard

    576. Out of Boundary Paths 

    给你一个棋盘,并放一个东西在一个起始位置,上、下、左、右移动,移动n次,一共有多少种可能移出这个棋盘

    https://www.cnblogs.com/grandyang/p/6927921.html

    dp表示上一次移动,所有位置的路径数;t表示的是当前移动,所有位置的路径数。然后每次用t去更新dp,即当前次移动去更新上一次移动。

    每次只要超过了边界,就记录可能的路径数更新最终的结果。

    class Solution {
    public:
        int findPaths(int m, int n, int N, int i, int j) {
            int res = 0;
            vector<vector<int>> dp(m,vector<int>(n,0));
            dp[i][j] = 1;
            for(int k = 0;k < N;k++){
                vector<vector<int>> tmp(m,vector<int>(n,0));
                for(int r = 0;r < m;r++){
                    for(int c = 0; c < n;c++){
                        for(auto dir:dirs){
                            int x = r + dir[0];
                            int y = c + dir[1];
                            if(x < 0 || x >= m || y < 0 || y >= n)
                                res = (res + dp[r][c])% 1000000007;
                            else
                                tmp[x][y] = (tmp[x][y] + dp[r][c])% 1000000007;
                        }
                    }
                }
                dp = tmp;
            }
            return res;
        }
    private:
        vector<vector<int>> dirs{{-1,0},{1,0},{0,-1},{0,1}};
    };

    688. Knight Probability in Chessboard

    https://www.cnblogs.com/grandyang/p/7639153.html

    https://www.cnblogs.com/Dylan-Java-NYC/p/7633409.html

    这道题给了我们一个大小为NxN国际象棋棋盘,上面有个骑士,相当于我们中国象棋中的马,能走‘日’字,给了我们一个起始位置,然后说允许我们走K步,问走完K步之后还能留在棋盘上的概率是多少。

     反过来想,走完K步棋子在board上的哪个位置呢. 反过来走, 看board上所有位置走完K步后能到初始位置(r,c)的数目和。

    这个题必须用double才不会越界,有点搞不懂为什么。

    class Solution {
    public:
        double knightProbability(int N, int K, int r, int c) {
            vector<vector<double>> dp(N,vector<double>(N,1));
            for(int k = 0;k < K;k++){
                vector<vector<double>> tmp(N,vector<double>(N,0));
                for(int i = 0;i < N;i++){
                    for(int j = 0;j < N;j++){
                        for(auto dir :dirs){
                            int x = i + dir[0];
                            int y = j + dir[1];
                            if(x < 0 || x >= N || y < 0 || y >= N)
                                continue;
                            else
                                tmp[x][y] += dp[i][j];
                        }
                    }
                }
                dp = tmp;
            }
            return dp[r][c]/pow(8,K);
        }
    private:
        vector<vector<int>> dirs{{-1,2},{1,2},{-1,-2},{1,-2},{2,1},{-2,1},{2,-1},{-2,-1}};
    };
  • 相关阅读:
    ALV_TREE(二:cl_gui_simple_tree…
    ALV_TREE(一:cl_gui_alv_tree_si…
    SQLPlus命令详细说明
    PL/SQL中,declare定义变量和variable定义变量的区别?
    Oracle 多表视图更新(待看完触发器后再来看)
    Oracle 函数 Translate 的用法
    Merge into
    savepoint(回退点)
    Oracle之分页查询
    对于package中全局变量的一点点初级理解
  • 原文地址:https://www.cnblogs.com/ymjyqsx/p/10962214.html
Copyright © 2011-2022 走看看