-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagonal_segment.c
More file actions
54 lines (48 loc) · 1.77 KB
/
diagonal_segment.c
File metadata and controls
54 lines (48 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<vector>
#include<algorithm>
using namespace std;
class Solution{
public:
int lenOfDiagonal(vector<vector<int>>&grid){
int n= grid.size(), m= grid[0].size();
vector<vector<int>> jorvexalin=grid;
//Directions for diagonals:(dx,dy)
vector<pair<int,int>> directions={{1,1},{1,-1},{-1,1},{-1,-1}};
//Function to check the longest diagonal segment starting from (r,c)
auto dfs=[&](int r,int c, int d1, int d2){
int length=1,prev=1;
int x= r+directions[d1].first, y= c+directions[d1].second;
//Follow first diagonal
while(x>=0 && y>=0 && y<m && grid[x][y]==(prev==1?2:0)){
length++;
prev=grid[x][y];
x+=directions[d1].first;
y+=directions[d1].second;
}
//Check second diagonal after a turn
int tempX=x+directions[d2].first, tempY=y+directions[d2].second;
while(tempX>=0 && tempY>=0 && tempX<n && tempY<m && grid[tempX][tempY]==(prev==1?2:0)){
length++;
prev=grid[tempX][tempY];
tempX+=directions[d2].first;
tempY+=directions[d2].second;
}
return length;
};
//Iterate through the grid and search for longest V-shaped segment
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[i][j]==1){
for(int d1=0;d1<4;d1++){
for(int d2=0;d2<4;d2++){
if(d1!=d2){
maxLength=max(maxLength,dfs(i,j,d1,d2));
}
}
}
}
}
}
return maxLength;
}
};