【问题描述】
在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和黑色的卒(pawn)。它们分别以字符 “R”,“.”,“B” 和 “p” 给出。大写字符表示白棋,小写字符表示黑棋。
车按国际象棋中的规则移动:它选择四个基本方向中的一个(北,东,西和南),然后朝那个方向移动,直到它选择停止、到达棋盘的边缘或移动到同一方格来捕获该方格上颜色相反的卒。另外,车不能与其他友方(白色)象进入同一个方格。
返回车能够在一次移动中捕获到的卒的数量。
示例 1:
输入:[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出:3
解释:
在本例中,车能够捕获所有的卒。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/available-captures-for-rook
【解答思路】
1. 先找出 ”车“的位置 ,再找出所有“卒”的位置,如果他们之间没有“象”和"卒",则吃“卒”数+1 时间复杂度:O(N^2) 空间复杂度:O(1)
public int numRookCaptures(char[][] board) {
int num = 0;
int x=0 ,y=0;
//找出 ”车“的位置
for(int i = 0 ; i < board.length ;i++ )
{
for(int j = 0 ;j< board[0].length;j++)
{
if( board[i][j]== 'R'){
x = i;
y = j;
}
}
}
//找出所有“卒”的位置,如果他们之间没有“象”和"卒",则吃“卒”数+1
for(int i = 0 ; i < board.length ;i++ )
{
for(int j = 0 ;j< board[0].length;j++)
{
if( board[i][j]== 'p'){
if(i == x){
boolean flag= true;
for(int k =Math.min(j, y)+1; k<Math.max(j, y);k++)
{
if( board[i][k]== 'B'||board[i][k]== 'p')
flag= false;
}
if(flag){
num++;
}
}
if(j== y){
boolean flag= true;
for(int k =Math.min(i, x)+1; k<Math.max(i, x);k++)
{
if( board[k][j]== 'B'||board[k][j]== 'p')
flag= false;
}
if(flag){
num++;
}
}
}
}
}
return num;
}
###### 2. 方向函数(伟哥做法-工程代码) 时间复杂度:(N^2) 空间复杂度:O(1)
)
- 遍历棋盘确定白色车的下标,用 (st,ed)(st,ed) 表示。
- 模拟车移动的规则,朝四个基本方向移动,直到碰到卒或者白色象或者碰到棋盘边缘时停止,用 cnt 记录捕获到的卒的数量
public class Solution {
public int numRookCaptures(char[][] board) {
// 因为题目已经明确给出 board.length == board[i].length == 8,所以不做输入检查
// 定义方向数组,可以认为是四个方向向量,在棋盘问题上是常见的做法
int[][] directions = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == 'R') {
int res = 0;
for (int[] direction : directions) {
if (burnout(board, i, j, direction)) {
res++;
}
}
return res;
}
}
}
// 代码不会走到这里,返回 0 或者抛出异常均可
return 0;
}
/**
* burnout 横冲直撞的意思(来自欧路词典)
*
* @param board 输入棋盘
* @param x 当前白象位置的横坐标
* @param y 当前白象位置的纵坐标
* @param direction 方向向量
* @return 消灭一个 p,就返回 true
*/
private boolean burnout(char[][] board, int x, int y, int[] direction) {
int i = x;
int j = y;
while (inArea(i, j)) {
// 是友军,路被堵死,直接返回
if (board[i][j] == 'B') {
break;
}
// 是敌军,拿下一血(不知道一血这个词是不是这么用的)
if (board[i][j] == 'p') {
return true;
}
i += direction[0];
j += direction[1];
}
return false;
}
/**
* @param i 当前位置横坐标
* @param j 当前位置纵坐标
* @return 是否在棋盘有效范围内
*/
private boolean inArea(int i, int j) {
return i >= 0 && i < 8 && j >= 0 && j < 8;
}
public static void main(String[] args) {
char[][] board = {
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', 'p', '.', '.', '.', '.'},
{'.', '.', '.', 'R', '.', '.', '.', 'p'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', 'p', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.'}};
Solution solution = new Solution();
int res = solution.numRookCaptures(board);
System.out.println(res);
}
}
作者:liweiwei1419
链接:https://leetcode-cn.com/problems/available-captures-for-rook/solution/mo-ni-ti-an-zhao-ti-mu-yi-si-shi-xian-ji-ke-java-b/
###### 4. 方向函数(甜姨做法-机智代码) 时间复杂度:(N^2) 空间复杂度:O(1)
class Solution {
public int numRookCaptures(char[][] board) {
// 定义上下左右四个方向
int[] dx = {-1, 1, 0, 0};
int[] dy = {0, 0, -1, 1};
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
// 找到白车所在的位置
if (board[i][j] == 'R') {
// 分别判断白车的上、下、左、右四个方向
int res = 0;
for (int k = 0; k < 4; k++) {
int x = i, y = j;
while (true) {
x += dx[k];
y += dy[k];
if (x < 0 || x >= 8 || y < 0 || y >= 8 || board[x][y] == 'B') {
break;
}
if (board[x][y] == 'p') {
res++;
break;
}
}
}
return res;
}
}
}
return 0;
}
}
作者:sweetiee
链接:https://leetcode-cn.com/problems/available-captures-for-rook/solution/jian-dan-java100-by-sweetiee/
【总结】
- 方向函数
// 定义上下左右四个方向
int[] dx = {-1, 1, 0, 0};
int[] dy = {0, 0, -1, 1};
2.直接考虑题意,面试时类似题目,多思考多问多沟通