-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck-knight-tour-configuration.rs
87 lines (72 loc) · 2.07 KB
/
check-knight-tour-configuration.rs
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
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
impl Solution {
pub fn check_valid_grid(grid: Vec<Vec<i32>>) -> bool {
if grid[0][0] != 0 {
return false;
}
let (mut x, mut y) = (0, 0);
let mut result = false;
for i in 1..grid.len() * grid.len() {
let (r1, x1, y1) = Self::check(&grid[..], x, y, i as i32);
if !r1 {
return false;
}
x = x1;
y = y1;
}
true
}
pub fn check(grid: &[Vec<i32>], x: usize, y: usize, expect: i32) -> (bool, usize, usize) {
if x > 1 {
if y > 0 {
if grid[x - 2][y - 1] == expect {
return (true, x - 2, y - 1);
}
}
if y < grid.len() - 1 {
if grid[x - 2][y + 1] == expect {
return (true, x - 2, y + 1);
}
}
}
if x < grid.len() - 2 {
if y > 0 {
if grid[x + 2][y - 1] == expect {
return (true, x + 2, y - 1);
}
}
if y < grid.len() - 1 {
if grid[x + 2][y + 1] == expect {
return (true, x + 2, y + 1);
}
}
}
if y > 1 {
if x > 0 {
if grid[x - 1][y - 2] == expect {
return (true, x - 1, y - 2);
}
}
if x < grid.len() - 1 {
if grid[x + 1][y - 2] == expect {
return (true, x + 1, y - 2);
}
}
}
if y < grid.len() - 2 {
if x > 0 {
if grid[x - 1][y + 2] == expect {
return (true, x - 1, y + 2);
}
}
if x < grid.len() - 1 {
if grid[x + 1][y + 2] == expect {
return (true, x + 1, y + 2);
}
}
}
(false, 0, 0)
}
}