Skip to content

Commit 235fac8

Browse files
authored
Added task 2056.
1 parent e85db19 commit 235fac8

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package g2001_2100.s2056_number_of_valid_move_combinations_on_chessboard;
2+
3+
// #Hard #Array #String #Simulation #Backtracking
4+
// #2022_05_30_Time_433_ms_(24.83%)_Space_144.4_MB_(12.75%)
5+
6+
import java.util.ArrayList;
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
public class Solution {
11+
// 0: rook, queen, bishop
12+
private int[][][] dirs = {
13+
{{-1, 0}, {1, 0}, {0, -1}, {0, 1}},
14+
{{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {1, 1}, {-1, -1}, {-1, 1}, {1, -1}},
15+
{{1, 1}, {-1, -1}, {-1, 1}, {1, -1}}
16+
};
17+
18+
public int countCombinations(String[] pieces, int[][] positions) {
19+
ArrayList<int[]>[] endPosition = new ArrayList[pieces.length];
20+
for (int i = 0; i < pieces.length; i++) {
21+
endPosition[i] = new ArrayList<>();
22+
}
23+
for (int i = 0; i < pieces.length; i++) {
24+
positions[i][0]--;
25+
positions[i][1]--;
26+
endPosition[i].add(positions[i]);
27+
int dirIndex = 0;
28+
switch (pieces[i]) {
29+
case "rook":
30+
dirIndex = 0;
31+
break;
32+
case "queen":
33+
dirIndex = 1;
34+
break;
35+
case "bishop":
36+
dirIndex = 2;
37+
break;
38+
default:
39+
break;
40+
}
41+
for (int[] d : dirs[dirIndex]) {
42+
int r = positions[i][0];
43+
int c = positions[i][1];
44+
while (true) {
45+
r += d[0];
46+
c += d[1];
47+
if (r < 0 || r >= 8 || c < 0 || c >= 8) {
48+
break;
49+
}
50+
endPosition[i].add(new int[] {r, c});
51+
}
52+
}
53+
}
54+
55+
return dfs(positions, endPosition, new int[pieces.length], 0);
56+
}
57+
58+
private int dfs(int[][] positions, ArrayList[] stop, int[] stopIndex, int cur) {
59+
if (cur == stopIndex.length) {
60+
int[][] p = new int[positions.length][2];
61+
for (int i = 0; i < p.length; i++) {
62+
p[i] = new int[] {positions[i][0], positions[i][1]};
63+
}
64+
return check(p, stop, stopIndex);
65+
}
66+
67+
int res = 0;
68+
for (int i = 0; i < stop[cur].size(); i++) {
69+
stopIndex[cur] = i;
70+
res += dfs(positions, stop, stopIndex, cur + 1);
71+
}
72+
73+
return res;
74+
}
75+
76+
private int check(int[][] positions, ArrayList<int[]>[] stop, int[] stopIndex) {
77+
boolean keepGoing = true;
78+
while (keepGoing) {
79+
keepGoing = false;
80+
for (int i = 0; i < positions.length; i++) {
81+
int diff = stop[i].get(stopIndex[i])[0] - positions[i][0];
82+
if (diff > 0) {
83+
keepGoing = true;
84+
positions[i][0]++;
85+
} else if (diff < 0) {
86+
keepGoing = true;
87+
positions[i][0]--;
88+
}
89+
diff = stop[i].get(stopIndex[i])[1] - positions[i][1];
90+
if (diff > 0) {
91+
keepGoing = true;
92+
positions[i][1]++;
93+
} else if (diff < 0) {
94+
keepGoing = true;
95+
positions[i][1]--;
96+
}
97+
}
98+
Set<Integer> seen = new HashSet<>();
99+
for (int i = 0; i < positions.length; i++) {
100+
int key = positions[i][0] * 100 + positions[i][1];
101+
if (seen.contains(key)) {
102+
return 0;
103+
}
104+
seen.add(key);
105+
}
106+
}
107+
return 1;
108+
}
109+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2056\. Number of Valid Move Combinations On Chessboard
2+
3+
Hard
4+
5+
There is an `8 x 8` chessboard containing `n` pieces (rooks, queens, or bishops). You are given a string array `pieces` of length `n`, where `pieces[i]` describes the type (rook, queen, or bishop) of the <code>i<sup>th</sup></code> piece. In addition, you are given a 2D integer array `positions` also of length `n`, where <code>positions[i] = [r<sub>i</sub>, c<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> piece is currently at the **1-based** coordinate <code>(r<sub>i</sub>, c<sub>i</sub>)</code> on the chessboard.
6+
7+
When making a **move** for a piece, you choose a **destination** square that the piece will travel toward and stop on.
8+
9+
* A rook can only travel **horizontally or vertically** from `(r, c)` to the direction of `(r+1, c)`, `(r-1, c)`, `(r, c+1)`, or `(r, c-1)`.
10+
* A queen can only travel **horizontally, vertically, or diagonally** from `(r, c)` to the direction of `(r+1, c)`, `(r-1, c)`, `(r, c+1)`, `(r, c-1)`, `(r+1, c+1)`, `(r+1, c-1)`, `(r-1, c+1)`, `(r-1, c-1)`.
11+
* A bishop can only travel **diagonally** from `(r, c)` to the direction of `(r+1, c+1)`, `(r+1, c-1)`, `(r-1, c+1)`, `(r-1, c-1)`.
12+
13+
You must make a **move** for every piece on the board simultaneously. A **move combination** consists of all the **moves** performed on all the given pieces. Every second, each piece will instantaneously travel **one square** towards their destination if they are not already at it. All pieces start traveling at the <code>0<sup>th</sup></code> second. A move combination is **invalid** if, at a given time, **two or more** pieces occupy the same square.
14+
15+
Return _the number of **valid** move combinations_.
16+
17+
**Notes:**
18+
19+
* **No two pieces** will start in the **same** square.
20+
* You may choose the square a piece is already on as its **destination**.
21+
* If two pieces are **directly adjacent** to each other, it is valid for them to **move past each other** and swap positions in one second.
22+
23+
**Example 1:**
24+
25+
![](https://assets.leetcode.com/uploads/2021/09/23/a1.png)
26+
27+
**Input:** pieces = ["rook"], positions = [[1,1]]
28+
29+
**Output:** 15
30+
31+
**Explanation:** The image above shows the possible squares the piece can move to.
32+
33+
**Example 2:**
34+
35+
![](https://assets.leetcode.com/uploads/2021/09/23/a2.png)
36+
37+
**Input:** pieces = ["queen"], positions = [[1,1]]
38+
39+
**Output:** 22
40+
41+
**Explanation:** The image above shows the possible squares the piece can move to.
42+
43+
**Example 3:**
44+
45+
![](https://assets.leetcode.com/uploads/2021/09/23/a3.png)
46+
47+
**Input:** pieces = ["bishop"], positions = [[4,3]]
48+
49+
**Output:** 12
50+
51+
**Explanation:** The image above shows the possible squares the piece can move to.
52+
53+
**Constraints:**
54+
55+
* `n == pieces.length`
56+
* `n == positions.length`
57+
* `1 <= n <= 4`
58+
* `pieces` only contains the strings `"rook"`, `"queen"`, and `"bishop"`.
59+
* There will be at most one queen on the chessboard.
60+
* <code>1 <= x<sub>i</sub>, y<sub>i</sub> <= 8</code>
61+
* Each `positions[i]` is distinct.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2001_2100.s2056_number_of_valid_move_combinations_on_chessboard;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void countCombinations() {
11+
assertThat(
12+
new Solution().countCombinations(new String[] {"rook"}, new int[][] {{1, 1}}),
13+
equalTo(15));
14+
}
15+
16+
@Test
17+
void countCombinations2() {
18+
assertThat(
19+
new Solution().countCombinations(new String[] {"queen"}, new int[][] {{1, 1}}),
20+
equalTo(22));
21+
}
22+
23+
@Test
24+
void countCombinations3() {
25+
assertThat(
26+
new Solution().countCombinations(new String[] {"bishop"}, new int[][] {{4, 3}}),
27+
equalTo(12));
28+
}
29+
}

0 commit comments

Comments
 (0)