LeetCode 999. 车的可用捕获量 - 方向数组思想
问题描述:
在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和黑色的卒(pawn)。它们分别以字符 “R”,“.”,“B” 和 “p” 给出。大写字符表示白棋,小写字符表示黑棋。
车按国际象棋中的规则移动:它选择四个基本方向中的一个(北,东,西和南),然后朝那个方向移动,直到它选择停止、到达棋盘的边缘或移动到同一方格来捕获该方格上颜色相反的卒。另外,车不能与其他友方(白色)象进入同一个方格。
求解问题:
返回车能够在一次移动中捕获到的卒的数量。
输入:[[".",".",".",".",".",".",".","."],[".",".",".",“p”,".",".",".","."],[".",".",".",“R”,".",".",".",“p”],[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."],[".",".",".",“p”,".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出:3
解释:在本例中,车能够捕获所有的卒。
问题解决方案
1. 方案一(普通暴力法):
不优美的方式:
- 首先找到 “车” 这个棋的位置为(2,3)
- 以车(2,3)的位置为起点,分别向上下左右进行遍历,判断移动的位置是否为 符合条件的卒。
public int numRookCaptures(char[][] board) {
int count = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
// 查找车的位置
if (board[i][j] == 'R') {
//上
for (int l = j; l >= 0; l--) {
if (board[i][l] == 'B') {
break;
} else if (board[i][l] == 'p') {
count++;
break;
}
}
//下
for (int l = j; l < 8; l++) {
if (board[i][l] == 'B') {
break;
} else if (board[i][l] == 'p') {
count++;
break;
}
}
//左
for (int h = i; h >= 0; h--) {
if (board[h][j] == 'B') {
break;
} else if (board[h][j] == 'p') {
count++;
break;
}
}
//右
for (int h = i; h < 8; h++) {
if (board[h][j] == 'B') {
break;
} else if (board[h][j] == 'p') {
count++;
break;
}
}
}
}
}
return count;
}
这种方式写出来的代码会出现四个重复的for循环,十分不优美。
遂经过思考与参看资料,发现对于这一类的网格类的问题,可以通过方向数组来优化移动问题,具体讲解见方式二。
2. 方式二(优化):
通过方向数组的思想,优化方案一中的上下左右四个暴力遍历的方式。
方向数组
我们看面图的案例,车的位置为(2,3),车向上移1格之后的坐标为(1,3),其实(1,3)的由来为:(2,3) +
(
−
1
,
0
)
\color{red}(-1,0)
(−1,0)
同理,车向下移动一格之后的坐标为:(2,3) +
(
1
,
0
)
\color{red}(1,0)
(1,0) = (3,3)
车向坐移动一格之后的坐标为:(2,3) +
(
0
,
−
1
)
\color{red}(0,-1)
(0,−1) = (2,2)
车向下移动一格之后的坐标为:(2,3) +
(
0
,
1
)
\color{red}(0,1)
(0,1) = (2,4)
我们可以定义两个数组来模拟车移动的状态:
// 定义方向数组
int[] dx = {-1, 1, 0, 0};
int[] dy = {0, 0, -1, 1};
优化之后的完整代码:
package indi.pentiumcm.leetcode;
/**
* @projName: algorithm
* @packgeName: indi.pentiumcm.leetcode
* @className: Q11
* @author: pentiumCM
* @email: 842679178@qq.com
* @date: 2020/3/26 15:26
* @describe: LeetCode 999. 车的可用捕获量
*/
public class Q11 {
public int numRookCapturesV2(char[][] board) {
int len = board.length;
// 定义吃到卒的个数
int count = 0;
// 定义方向数组
int[] dx = {-1, 1, 0, 0};
int[] dy = {0, 0, -1, 1};
// 查找车的位置
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if (board[i][j] == 'R') {
// 上下左右四个方向分别移动
for (int k = 0; k < 4; k++) {
int x = i, y = j;
while (true) {
// 移动一步的增量:(-1, 0)-向上移动 1 格
x += dx[k];
y += dy[k];
// 移动超出棋盘
if (x <= 0 || x >= 8 || y <= 0 || y >= 8) {
break;
}
// 移动到白象的位置
else if (board[x][y] == 'B') {
break;
} else if (board[x][y] == 'p') {
count++;
break;
}
}
}
return count;
}
}
}
return 0;
}
public static void main(String[] args) {
char[][] boards = {{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', 'R', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'}};
int num = new Q11().numRookCapturesV2(boards);
}
}