Re: [闲聊] 每日leetcode

楼主: oin1104 (是oin的说)   2024-10-29 13:12:58
题目:
给你二维的阵列
从左边挑一个地方开始
开始之后每次都往右边 右上 右下看
只要这格比要走的下一个小 就可以走
最多能走多远
思路:
dp
只要比较小就可以走
```cpp
class Solution {
public:
int maxMoves(vector<vector<int>>& grid)
{
int n = grid.size();
int m = grid[0].size();
vector<vector<int>> paper(n,vector<int>(m,0));
for(int i = 0 ; i < n ; i ++)
{
paper[i][0] = 1;
}
int res = 0;
for(int i = 1 ; i < m ; i ++)
{
int go = 0;
for(int j = 0 ; j < n ; j ++ )
{
if(grid[j][i] > grid[j][i-1] && paper[j][i-1] == 1)
{
paper[j][i] = 1;
go = 1;
}
if(j-1>=0 && grid[j][i] > grid[j-1][i-1] && paper[j-1][i-1] == 1
)
{
paper[j][i] = 1;
go = 1;
}
if(j+1<n && grid[j][i] > grid[j+1][i-1] && paper[j+1][i-1] == 1)
{
paper[j][i] = 1;
go = 1;
}
}
if(go) res ++;
}
return res;
}
};
```

Links booklink

Contact Us: admin [ a t ] ucptt.com