-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2D Hopscotch
114 lines (56 loc) · 1.51 KB
/
2D Hopscotch
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
class Solution{
private :
int fun(int n,int m,vector<vector<int>> grid,int delrow[],int delcol[],int i,int j,int size)
{
int ans = 0;
for(int k = 0;k<size;k++)
{
int nrow = delrow[k] + i;
int ncol = delcol[k] + j;
if(nrow>=0&&ncol>=0&&nrow<n&&ncol<m){
ans = ans + grid[nrow][ncol];
}
}
return ans;
}
public:
int hopscotch(int n, int m, vector<vector<int>> mat, int ty, int i, int j)
{
// for 1 step
int dx1o[] = {-1,0,1,1,1,0};
int dy1o[] = {0,1,1,0,-1,-1};
int dx1e[] = {-1,-1,0,1,0,-1};
int dy1e[] = {0,1,1,0,-1,-1};
//for 2step
int dx2o[] = {-1,-1,-2,-1,-1,0,0,1,1,2,2,2};
int dy2o[] = {-2,-1,0,1,2,-2,2,-2,2,-1,0,1};
int dx2e[] = {-2,-2,-2,-1,-1,0,0,1,1,1,1,2};
int dy2e[] = {-1,0,1,-2,2,-2,2,-2,-1,1,2,0};
// 1step
int ans = 0;
if(ty == 0)
{
if(j%2 == 0)
{
ans = fun(n,m,mat,dx1e,dy1e,i,j,6);
}
else
{
ans = fun(n,m,mat,dx1o,dy1o,i,j,6);
}
}
else
{
//2step
if(j%2 == 0)
{
ans = fun(n,m,mat,dx2e,dy2e,i,j,12);
}
else
{
ans = fun(n,m,mat,dx2o,dy2o,i,j,12);
}
}
return ans;
}
};