Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class Solution {
vector<pair<int,int>>dir;
int memo[501][501][4][2];
public:
int lenOfVDiagonal(vector<vector<int>>& grid)
{
int m = grid.size(), n = grid[0].size();
int ret = 0;
dir = {{-1,1},{1,1},{1,-1},{-1,-1}};

for (int i=0; i<m; i++)
for (int j=0; j<n; j++)
{
if (grid[i][j]==1)
{
ret = max(ret, dfs(grid,i,j,0,1));
ret = max(ret, dfs(grid,i,j,1,1));
ret = max(ret, dfs(grid,i,j,2,1));
ret = max(ret, dfs(grid,i,j,3,1));
}
}
return ret;
}

bool canContinue(int a, int b)
{
if (a==1 && b!=2) return false;
if (a==2 && b!=0) return false;
if (a==0 && b!=2) return false;
return true;
}
bool inbound(int i, int j, int m, int n)
{
return i>=0 && i<m && j>=0 && j<n;
}

int dfs(vector<vector<int>>& grid, int x, int y, int k, int t)
{
if (memo[x][y][k][t]!=0) return memo[x][y][k][t];

int m = grid.size(), n = grid[0].size();
int ret = 1;

int i = x+dir[k].first, j = y+dir[k].second;

if (inbound(i,j,m,n) && canContinue(grid[x][y], grid[i][j]))
ret = max(ret, 1 + dfs(grid,i,j,k,t));

if (t==1)
{
int kk=(k+1)%4;
i = x+dir[k].first, j = y+dir[k].second;
if (inbound(i,j,m,n) && canContinue(grid[x][y], grid[i][j]))
ret = max(ret, 1 + dfs(grid,i,j,kk,0));
}
memo[x][y][k][t] = ret;
return ret;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### 3459.Length-of-Longest-V-Shaped-Diagonal-Segment

很常规的深度优先搜索。每个格子、每个方向只会进入一次。所以最多有`500*500*4=1e6`种状态。再加上有一次转弯的机会,所以2e6种状态是可以遍历和存储下来的。

定义dfs(x,y,k,t)表示以k的方向进入(x,y)的格子、且还有t次转弯机会时,还能走的最长路径。如果t==0,那么只能按照k的方向进入下一个(i1,j1);否则还可以考察按照k+1的方向进入下一个(i2,j2).

注意进入的下一个各自(i,j)和(x,y)要满足数值上的约束,否则即可停止往下搜索。

此外,本题的记忆化根据四个参数进行记忆化也是必须的。
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@
[2056.Number-of-Valid-Move-Combinations-On-Chessboard](https://github.com/wisdompeak/LeetCode/tree/master/DFS/2056.Number-of-Valid-Move-Combinations-On-Chessboard) (H)
[2065.Maximum-Path-Quality-of-a-Graph](https://github.com/wisdompeak/LeetCode/tree/master/DFS/2065.Maximum-Path-Quality-of-a-Graph) (M)
[2850.Minimum-Moves-to-Spread-Stones-Over-Grid](https://github.com/wisdompeak/LeetCode/tree/master/DFS/2850.Minimum-Moves-to-Spread-Stones-Over-Grid) (M)
[3459.Length-of-Longest-V-Shaped-Diagonal-Segment](https://github.com/wisdompeak/LeetCode/tree/master/DFS/3459.Length-of-Longest-V-Shaped-Diagonal-Segment) (M+)
* ``search in an array``
[090.Subsets-II](https://github.com/wisdompeak/LeetCode/tree/master/DFS/090.Subsets-II) (M+)
[301.Remove-Invalid-Parentheses](https://github.com/wisdompeak/LeetCode/tree/master/DFS/301.Remove-Invalid-Parentheses) (H)
Expand Down Expand Up @@ -1478,7 +1479,7 @@
[2781.Length-of-the-Longest-Valid-Substring](https://github.com/wisdompeak/LeetCode/tree/master/String/2781.Length-of-the-Longest-Valid-Substring) (H-)
[3394.Check-if-Grid-can-be-Cut-into-Sections](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/3394.Check-if-Grid-can-be-Cut-into-Sections) (M)
[2271.Maximum-White-Tiles-Covered-by-a-Carpet](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2271.Maximum-White-Tiles-Covered-by-a-Carpet) (M+)
[3413.Maximum-Coins-From-K-Consecutive-Bags](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/3413.Maximum-Coins-From-K-Consecutive-Bags) (H-)
[3413.Maximum-Coins-From-K-Consecutive-Bags](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/3413.Maximum-Coins-From-K-Consecutive-Bags) (H-)
3104.Find Longest Self-Contained Substring (TBD)
[1520.Maximum-Number-of-Non-Overlapping-Substrings](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1520.Maximum-Number-of-Non-Overlapping-Substrings) (H-)
[3458.Select-K-Disjoint-Special-Substrings](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/3458.Select-K-Disjoint-Special-Substrings) (H-)
Expand Down