<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)576:出界的路徑數(shù)

          共 1159字,需瀏覽 3分鐘

           ·

          2022-04-11 11:52

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?出界的路徑數(shù),我們先來看題面:
          https://leetcode-cn.com/problems/out-of-boundary-paths/

          There is an m x n grid with a ball. The ball is initially at the position [startRow, startColumn]. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply at most maxMove moves to the ball.


          Given the five integers m, n, maxMove, startRow, startColumn, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it modulo 109 + 7.

          給你一個大小為 m x n 的網(wǎng)格和一個球。球的起始坐標(biāo)為 [startRow, startColumn] 。你可以將球移到在四個方向上相鄰的單元格內(nèi)(可以穿過網(wǎng)格邊界到達(dá)網(wǎng)格之外)。你 最多 可以移動 maxMove 次球。

          給你五個整數(shù) m、n、maxMove、startRow 以及 startColumn ,找出并返回可以將球移出邊界的路徑數(shù)量。因?yàn)榇鸢缚赡芊浅4?,返回?109 + 7 取余 后的結(jié)果。

          示例? ? ? ? ? ? ? ? ? ? ? ? ?

          解題


          利用動態(tài)規(guī)劃求解,假設(shè)球在第 t 步到達(dá) (mi, nj) 位置有 dp[t][mi][nj] 種路徑,我們可以想一下當(dāng)前狀態(tài)如何從上一步運(yùn)動中得來。無非就是上下左右四個方向移動過來的,而移動步數(shù)是 t-1。

          于是有轉(zhuǎn)態(tài)轉(zhuǎn)移方程:dp[t][mi][nj] = dp[t-1][mi-1][nj] + dp[t-1][mi][nj-1] + dp[t-1][mi+1][nj] + dp[t-1][mi][nj+1]

          而初始狀態(tài)下有 dp[0][i][j]=1。

          具體實(shí)現(xiàn)時,為了避免邊界判斷,對二維網(wǎng)格上下左右都補(bǔ) 0,有 1<=mi<=m; 1<=ni<=n。

          class?Solution?{
          public:
          ????int?findPaths(int?m, int?n, int?N, int?i, int?j)?{
          ????????if?(N == 0) return?0;
          ????????vector<vector<vector<long>>> dp(N, vector<vector<long>>(m+2, vector<long>(n+2)));
          ????????dp[0][i+1][j+1] = 1;
          ????????long?res = 0;
          ????????if?(i == 0) res++;
          ????????if?(i == m-1) res++;
          ????????if?(j == 0) res++;
          ????????if?(j == n-1) res++;
          ????????for?(int?t = 1; t < N; t++) {
          ????????????for?(int?mi = 1; mi <= m; mi++) {
          ????????????????for?(int?nj = 1; nj <= n; nj++) {
          ????????????????????dp[t][mi][nj] = (dp[t-1][mi-1][nj] + dp[t-1][mi+1][nj] + dp[t-1][mi][nj-1] + dp[t-1][mi][nj+1]) % 1000000007;
          ????????????????????if?(mi == 1) res += dp[t][mi][nj];
          ????????????????????if?(mi == m) res += dp[t][mi][nj];
          ????????????????????if?(nj == 1) res += dp[t][mi][nj];
          ????????????????????if?(nj == n) res += dp[t][mi][nj];
          ????????????????????res %= 1000000007;
          ????????????????}
          ????????????}
          ????????}
          ????????return?int(res);
          ????}
          };


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點(diǎn)個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:
          LeetCode1-560題匯總,希望對你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)561:數(shù)組拆分 I
          LeetCode刷題實(shí)戰(zhàn)562:矩陣中最長的連續(xù)1線段
          LeetCode刷題實(shí)戰(zhàn)563:二叉樹的坡度
          LeetCode刷題實(shí)戰(zhàn)564:尋找最近的回文數(shù)
          LeetCode刷題實(shí)戰(zhàn)565:數(shù)組嵌套
          LeetCode刷題實(shí)戰(zhàn)566:重塑矩陣
          LeetCode刷題實(shí)戰(zhàn)567:字符串的排列
          LeetCode刷題實(shí)戰(zhàn)568:最大休假天數(shù)
          LeetCode刷題實(shí)戰(zhàn)569:員工薪水中位數(shù)
          LeetCode刷題實(shí)戰(zhàn)570:至少有5名直接下屬的經(jīng)理
          LeetCode刷題實(shí)戰(zhàn)571:給定數(shù)字的頻率查詢中位數(shù)
          LeetCode刷題實(shí)戰(zhàn)572:另一棵樹的子樹
          LeetCode刷題實(shí)戰(zhàn)573:松鼠模擬
          LeetCode刷題實(shí)戰(zhàn)574:當(dāng)選者
          LeetCode刷題實(shí)戰(zhàn)575:分糖果

          瀏覽 22
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  婷婷丁香五月激情 | 欧美日韩在线观看视频 | 亚洲区免费 | 一本大道中文字幕无码29 | 亚洲视频手机在线播放 |