Try   HackMD
Dark Theme License

The MIT License (MIT)

Copyright © 2022-2023 Lumynous

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

2328. Number of Increasing Paths in a Grid

題目連結: Leetcode

題目概要

一個n×m的2維陣列grid, 可以從四個方向移動到四個格子
請計算從任意格子出發並到達任意格子, 且路線上的數字是一個嚴格遞增數列的路徑數
由於答案可能會很大, 請將答案模除

109+7後回傳

想法

DFS

在任意格子的路徑數為1加上四格方向可以走的格子的路徑數
若一個格子四周無可走的格子, 則回傳 1

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

計算所有格子的路徑和

參考解法

歡迎補充

CPP DFS
class Solution { public: int n, m; int MOD = 1e9+7; vector<vector<int>> table; int x_move[4] = {1, -1, 0, 0}, y_move[4] = {0, 0, 1, -1}; int dfs(vector<vector<int>>& grid, int x, int y){ if (table[x][y] != -1) return table[x][y]; int r = 1; for (int i=0; i<4; i++){ int tx = x + x_move[i], ty = y + y_move[i]; if (tx < 0 or ty < 0 or tx >= n or ty >= m) continue; if (grid[x][y] < grid[tx][ty]){ table[tx][ty] = dfs(grid, tx, ty) % MOD; r += table[tx][ty]; } } return r % MOD; } int countPaths(vector<vector<int>>& grid) { n = grid.size(); m = grid[0].size(); long long ans = 0; table.resize(n, vector<int>(m, -1)); for (int i=0; i<n; i++){ for (int j=0; j<m; j++){ table[i][j] = dfs(grid, i, j) % MOD; ans += table[i][j] % MOD; ans %= MOD; cout<<table[i][j]<<" "; } cout<<"\n"; } return ans % MOD; } };

Dark Theme License

The MIT License (MIT)

Copyright © 2022 Luminous-Coder

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.