Skip to content

Commit 8fbc9c4

Browse files
committed
1 parent 7249325 commit 8fbc9c4

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

Diff for: src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1475,3 +1475,4 @@ mod s1953_maximum_number_of_weeks_for_which_you_can_work;
14751475
mod s1954_minimum_garden_perimeter_to_collect_enough_apples;
14761476
mod s1955_count_number_of_special_subsequences;
14771477
mod s1957_delete_characters_to_make_fancy_string;
1478+
mod s1958_check_if_move_is_legal;

Diff for: src/solution/s1958_check_if_move_is_legal.rs

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* [1958] Check if Move is Legal
3+
*
4+
* You are given a 0-indexed 8 x 8 grid board, where board[r][c] represents the cell (r, c) on a game board. On the board, free cells are represented by '.', white cells are represented by 'W', and black cells are represented by 'B'.
5+
* Each move in this game consists of choosing a free cell and changing it to the color you are playing as (either white or black). However, a move is only legal if, after changing it, the cell becomes the endpoint of a good line (horizontal, vertical, or diagonal).
6+
* A good line is a line of three or more cells (including the endpoints) where the endpoints of the line are one color, and the remaining cells in the middle are the opposite color (no cells in the line are free). You can find examples for good lines in the figure below:
7+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/07/22/goodlines5.png" style="width: 500px; height: 312px;" />
8+
* Given two integers rMove and cMove and a character color representing the color you are playing as (white or black), return true if changing cell (rMove, cMove) to color color is a legal move, or false if it is not legal.
9+
*
10+
* Example 1:
11+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/07/10/grid11.png" style="width: 350px; height: 350px;" />
12+
* Input: board = [[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],["W","B","B",".","W","W","W","B"],[".",".",".","B",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."]], rMove = 4, cMove = 3, color = "B"
13+
* Output: true
14+
* Explanation: '.', 'W', and 'B' are represented by the colors blue, white, and black respectively, and cell (rMove, cMove) is marked with an 'X'.
15+
* The two good lines with the chosen cell as an endpoint are annotated above with the red rectangles.
16+
*
17+
* Example 2:
18+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/07/10/grid2.png" style="width: 350px; height: 351px;" />
19+
* Input: board = [[".",".",".",".",".",".",".","."],[".","B",".",".","W",".",".","."],[".",".","W",".",".",".",".","."],[".",".",".","W","B",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".","B","W",".","."],[".",".",".",".",".",".","W","."],[".",".",".",".",".",".",".","B"]], rMove = 4, cMove = 4, color = "W"
20+
* Output: false
21+
* Explanation: While there are good lines with the chosen cell as a middle cell, there are no good lines with the chosen cell as an endpoint.
22+
*
23+
*
24+
* Constraints:
25+
*
26+
* board.length == board[r].length == 8
27+
* 0 <= rMove, cMove < 8
28+
* board[rMove][cMove] == '.'
29+
* color is either 'B' or 'W'.
30+
*
31+
*/
32+
pub struct Solution {}
33+
34+
// problem: https://leetcode.com/problems/check-if-move-is-legal/
35+
// discuss: https://leetcode.com/problems/check-if-move-is-legal/discuss/?currentPage=1&orderBy=most_votes&query=
36+
37+
// submission codes start here
38+
39+
impl Solution {
40+
// Credit: https://leetcode.com/problems/check-if-move-is-legal/solutions/3241963/just-a-runnable-solution/
41+
pub fn check_move(board: Vec<Vec<char>>, r_move: i32, c_move: i32, color: char) -> bool {
42+
let x_arr = [0, 0, 1, -1, 1, -1, -1, 1];
43+
let y_arr = [1, -1, 0, 0, 1, -1, 1, -1];
44+
for i in 0..8 {
45+
let mut x = r_move + x_arr[i];
46+
let mut y = c_move + y_arr[i];
47+
let mut count = 0;
48+
while x < board.len() as i32 && x >= 0 && y < board[i].len() as i32 && y >= 0 {
49+
if board[x as usize][y as usize] == '.' {
50+
break;
51+
} else if board[x as usize][y as usize] != color {
52+
count += 1;
53+
} else if board[x as usize][y as usize] == color && count < 1 {
54+
break;
55+
} else if board[x as usize][y as usize] == color && count >= 1 {
56+
return true;
57+
}
58+
x += x_arr[i];
59+
y += y_arr[i];
60+
}
61+
}
62+
false
63+
}
64+
}
65+
66+
// submission codes end
67+
68+
#[cfg(test)]
69+
mod tests {
70+
use super::*;
71+
72+
#[test]
73+
fn test_1958_example_1() {
74+
let board = vec![
75+
vec!['.', '.', '.', 'B', '.', '.', '.', '.'],
76+
vec!['.', '.', '.', 'W', '.', '.', '.', '.'],
77+
vec!['.', '.', '.', 'W', '.', '.', '.', '.'],
78+
vec!['.', '.', '.', 'W', '.', '.', '.', '.'],
79+
vec!['W', 'B', 'B', '.', 'W', 'W', 'W', 'B'],
80+
vec!['.', '.', '.', 'B', '.', '.', '.', '.'],
81+
vec!['.', '.', '.', 'B', '.', '.', '.', '.'],
82+
vec!['.', '.', '.', 'W', '.', '.', '.', '.'],
83+
];
84+
let r_move = 4;
85+
let c_move = 3;
86+
let color = 'B';
87+
88+
let result = true;
89+
90+
assert_eq!(Solution::check_move(board, r_move, c_move, color), result);
91+
}
92+
93+
#[test]
94+
fn test_1958_example_2() {
95+
let board = vec![
96+
vec!['.', '.', '.', '.', '.', '.', '.', '.'],
97+
vec!['.', 'B', '.', '.', 'W', '.', '.', '.'],
98+
vec!['.', '.', 'W', '.', '.', '.', '.', '.'],
99+
vec!['.', '.', '.', 'W', 'B', '.', '.', '.'],
100+
vec!['.', '.', '.', '.', '.', '.', '.', '.'],
101+
vec!['.', '.', '.', '.', 'B', 'W', '.', '.'],
102+
vec!['.', '.', '.', '.', '.', '.', 'W', '.'],
103+
vec!['.', '.', '.', '.', '.', '.', '.', 'B'],
104+
];
105+
let r_move = 4;
106+
let c_move = 4;
107+
let color = 'W';
108+
109+
let result = false;
110+
111+
assert_eq!(Solution::check_move(board, r_move, c_move, color), result);
112+
}
113+
}

0 commit comments

Comments
 (0)