-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2D Hopscotch.java
More file actions
86 lines (78 loc) · 3.59 KB
/
2D Hopscotch.java
File metadata and controls
86 lines (78 loc) · 3.59 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
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
//{ Driver Code Starts
//Initial Template for Java
import java.io.*;
import java.util.*;
class GFG{
public static void main(String args[])throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
while(t-- > 0){
String arr[] = in.readLine().trim().split("\\s+");
int n = Integer.parseInt(arr[0]);
int m = Integer.parseInt(arr[1]);
int mat[][] = new int[n][m];
for(int i = 0;i < n;i++){
String a[] = in.readLine().trim().split("\\s+");
for(int j = 0;j < m;j++)
mat[i][j] = Integer.parseInt(a[j]);
}
String a1[] = in.readLine().trim().split("\\s+");
int ty = Integer.parseInt(a1[0]);
int i = Integer.parseInt(a1[1]);
int j = Integer.parseInt(a1[2]);
Solution ob = new Solution();
System.out.println(ob.hopscotch(n, m, mat, ty, i, j));
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution{
static int hopscotch(int n, int m, int mat[][], int ty, int i, int j)
{
// code here
int ans=0;
if(ty==0) {
if(isValid(i+1,j,n,m)) ans+=mat[i+1][j]; //down
if(isValid(i-1,j,n,m)) ans+=mat[i-1][j]; //up
if(isValid(i,j+1,n,m)) ans+=mat[i][j+1]; //right
if(isValid(i,j-1,n,m)) ans+=mat[i][j-1]; //left
if(j%2==0) {
if(isValid(i-1,j+1,n,m)) ans+=mat[i-1][j+1]; // right-up
if(isValid(i-1,j-1,n,m)) ans+=mat[i-1][j-1]; // left-up
} else {
if(isValid(i+1,j+1,n,m)) ans+=mat[i+1][j+1]; // right-down
if(isValid(i+1,j-1,n,m)) ans+=mat[i+1][j-1]; // left-down
}
} else {
if(isValid(i+2,j,n,m)) ans+=mat[i+2][j]; // down
if(isValid(i-2,j,n,m)) ans+=mat[i-2][j]; // up
if(isValid(i,j+2,n,m)) ans+=mat[i][j+2]; // front
if(isValid(i,j-2,n,m)) ans+=mat[i][j-2]; // back
if(j%2==0) {
if(isValid(i+1,j+1,n,m)) ans+=mat[i+1][j+1]; // right-down
if(isValid(i-2,j+1,n,m)) ans+=mat[i-2][j+1]; // right-up
if(isValid(i+1,j-1,n,m)) ans+=mat[i+1][j-1]; // left-down
if(isValid(i-2,j-1,n,m)) ans+=mat[i-2][j-1]; // left-up
if(isValid(i+1,j+2,n,m)) ans+=mat[i+1][j+2]; // right-down-right-down
if(isValid(i+1,j-2,n,m)) ans+=mat[i+1][j-2]; // left-down-left-down
if(isValid(i-1,j+2,n,m)) ans+=mat[i-1][j+2]; // right-up-right-up
if(isValid(i-1,j-2,n,m)) ans+=mat[i-1][j-2]; // left-up-left-up
} else {
if(isValid(i-1,j+1,n,m)) ans+=mat[i-1][j+1]; // right-up
if(isValid(i+2,j+1,n,m)) ans+=mat[i+2][j+1]; // right-down
if(isValid(i-1,j-1,n,m)) ans+=mat[i-1][j-1]; // left-up
if(isValid(i+2,j-1,n,m)) ans+=mat[i+2][j-1]; // left-down
if(isValid(i-1,j+2,n,m)) ans+=mat[i-1][j+2]; // right-up-right-up
if(isValid(i-1,j-2,n,m)) ans+=mat[i-1][j-2]; // left-up-left-up
if(isValid(i+1,j+2,n,m)) ans+=mat[i+1][j+2]; // right-down-right-down
if(isValid(i+1,j-2,n,m)) ans+=mat[i+1][j-2]; // left-down-left-down
}
}
return ans;
}
static boolean isValid(int i,int j,int n,int m){
return i>=0 && j>=0 && i<n && j<m;
}
}