-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_2596.java
45 lines (44 loc) · 1.4 KB
/
_2596.java
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
package com.fishercoder.solutions.thirdthousand;
public class _2596 {
public static class Solution1 {
public boolean checkValidGrid(int[][] grid) {
int n = grid.length;
int[][] offsets =
new int[][] {
{-2, 1},
{-1, 2},
{1, 2},
{2, 1},
{2, -1},
{1, -2},
{-1, -2},
{-2, -1}
};
int x = 0;
int y = 0;
int currentVal = 0;
while (currentVal != n * n - 1) {
boolean foundNext = false;
for (int[] offset : offsets) {
int newX = x + offset[0];
int newY = y + offset[1];
if (newX >= 0
&& newX < n
&& newY >= 0
&& newY < n
&& grid[newX][newY] == currentVal + 1) {
currentVal++;
x = newX;
y = newY;
foundNext = true;
break;
}
}
if (!foundNext) {
return false;
}
}
return true;
}
}
}