-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
165 lines (155 loc) · 5.8 KB
/
main.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
fn main() {
assert_eq!(Solution::moves_to_chessboard(
vec![
vec![0,0,1,0,1,0,1,1],
vec![1,1,0,1,0,1,0,0],
vec![0,0,1,0,1,0,1,1],
vec![1,1,0,1,0,1,0,0],
vec![1,1,0,1,0,1,0,0],
vec![0,0,1,0,1,0,1,1],
vec![0,0,1,0,1,0,1,1],
vec![1,1,0,1,0,1,0,0]]
),2);
}
struct Solution {}
/// https://www.cnblogs.com/h-hkai/p/10662760.html
///
impl Solution {
pub fn chekc_valid(board: &Vec<Vec<i32>>) -> bool {
let n = board.len();
for i in 1..n {
let eq = if board[0][0] == board[i][0] { true } else { false };
for j in 1..n {
if eq && board[0][j] != board[i][j] ||
! eq && board[0][j] == board[i][j] { return false }
}
}
for i in 0..n {
let ones = board[i].iter().filter(|x| x.is_positive()).count();
if ones == 0 { return false }
if n % 2 == 0 && ones * 2 != n { return false }
if n % 2 == 1 && (ones * 2 + 1 != n && ones * 2 - 1 != n) { return false }
}
for i in 0..n {
let ones = board.iter().filter(|x| x[i].is_positive()).count();
if ones == 0 { return false }
if n % 2 == 0 && ones * 2 != n { return false }
if n % 2 == 1 && (ones * 2 + 1 != n && ones * 2 - 1 != n) { return false }
}
true
}
pub fn count_odd(mut arr: Vec<i32>) -> i32 {
let n = arr.len();
let mut cnt = 0;
let ones = arr.iter().filter(|x| x.is_positive()).count();
let start_from_one = if ones * 2 + 1 == n { false } else { true };
let (even, odd) = if start_from_one { (1,0) } else { (0,1) };
for i in 0..n {
if i % 2 == 0 && arr[i] != even {
for j in i+1..n {
if j % 2 == 1 && arr[j] != odd {
arr[j] = odd;
cnt += 1;
break
}
}
} else if i % 2 == 1 && arr[i] != odd {
for j in i+1..n {
if j % 2 == 0 && arr[j] != even {
arr[j] = even;
cnt += 1;
break;
}
}
}
}
cnt
}
pub fn count_even(mut arr: Vec<i32>, start_from_one: bool) -> i32 {
let n = arr.len();
let mut cnt = 0;
let (even, odd) = if start_from_one { (1,0) } else { (0,1) };
for i in 0..n {
if i % 2 == 0 && arr[i] != even {
for j in i+1..n {
if j % 2 == 1 && arr[j] != odd {
arr[j] = odd;
cnt += 1;
break
}
}
} else if i % 2 == 1 && arr[i] != odd {
for j in i+1..n {
if j % 2 == 0 && arr[j] != even {
arr[j] = even;
cnt += 1;
break;
}
}
}
}
cnt
}
pub fn deal_with_odd(board: Vec<Vec<i32>>) -> i32 {
let arr = board.iter().fold(Vec::with_capacity(board.len()), |mut res, row| {
res.push(row[0]);
res
});
// count row & count column
Self::count_odd(board[0].to_owned()) + Self::count_odd(arr)
}
pub fn deal_with_even(board: Vec<Vec<i32>>) -> i32 {
let mut res = i32::max_value();
let arr = board.iter().fold(Vec::with_capacity(board.len()), |mut res, row| {
res.push(row[0]);
res
});
let cnt = Self::count_even(board[0].to_owned(),true) + Self::count_even(arr.to_owned(),true);
if cnt < res { res = cnt }
let cnt = Self::count_even(board[0].to_owned(),true) + Self::count_even(arr.to_owned(),false);
if cnt < res { res = cnt }
let cnt = Self::count_even(board[0].to_owned(),false) + Self::count_even(arr.to_owned(),true);
if cnt < res { res = cnt }
let cnt = Self::count_even(board[0].to_owned(),false) + Self::count_even(arr,false);
if cnt < res { res = cnt }
res
}
pub fn moves_to_chessboard(board: Vec<Vec<i32>>) -> i32 {
let n = board.len();
// check valid
if ! Self::chekc_valid(&board) { return -1 }
// count min step
if n % 2 == 1 {
Self::deal_with_odd(board)
} else {
Self::deal_with_even(board)
}
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::moves_to_chessboard(vec![vec![0,1,1,0],vec![0,1,1,0],vec![1,0,0,1],vec![1,0,0,1]]),2);
assert_eq!(Solution::moves_to_chessboard(vec![vec![1,0],vec![1,0]]),-1);
assert_eq!(Solution::moves_to_chessboard(vec![vec![0,1],vec![1,0]]),0);
assert_eq!(Solution::moves_to_chessboard(vec![vec![1,1,0],vec![1,1,0],vec![0,0,1]]),2);
assert_eq!(Solution::moves_to_chessboard(vec![vec![1,0,1],vec![1,0,1],vec![0,1,0]]),1);
assert_eq!(Solution::moves_to_chessboard(vec![vec![1,0,1],vec![0,1,0],vec![1,0,1]]),0);
assert_eq!(Solution::moves_to_chessboard(vec![vec![0,1,0,1],vec![1,0,1,0],vec![0,1,0,1],vec![1,0,1,0]]),0);
assert_eq!(Solution::moves_to_chessboard(vec![vec![0,1,0,1],vec![0,1,0,1],vec![1,0,1,0],vec![1,0,1,0]]),1);
assert_eq!(Solution::moves_to_chessboard(vec![vec![1,1,0],vec![0,0,1],vec![0,0,1]]),2);
assert_eq!(Solution::moves_to_chessboard(
vec![
vec![0,0,1,0,1,0,1,1],
vec![1,1,0,1,0,1,0,0],
vec![0,0,1,0,1,0,1,1],
vec![1,1,0,1,0,1,0,0],
vec![1,1,0,1,0,1,0,0],
vec![0,0,1,0,1,0,1,1],
vec![0,0,1,0,1,0,1,1],
vec![1,1,0,1,0,1,0,0]]
),2);
}
}