数组题目:可以被一步捕获的棋子数

2024-02-06 07:18

本文主要是介绍数组题目:可以被一步捕获的棋子数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 题目
    • 标题和出处
    • 难度
    • 题目描述
      • 要求
      • 示例
      • 数据范围
  • 解法
    • 思路和算法
    • 代码
    • 复杂度分析

题目

标题和出处

标题:可以被一步捕获的棋子数

出处:999. 可以被一步捕获的棋子数

难度

2 级

题目描述

要求

在一个 8 × 8 \texttt{8} \times \texttt{8} 8×8 的棋盘上,有一个白色的车( Rook \texttt{Rook} Rook),用字符 ‘R’ \texttt{`R'} ‘R’ 表示。棋盘上还可能存在空方块,白色的象( Bishop \texttt{Bishop} Bishop)以及黑色的卒( pawn \texttt{pawn} pawn),分别用字符 ‘.’ \texttt{`.'} ‘.’ ‘B’ \texttt{`B'} ‘B’ ‘p’ \texttt{`p'} ‘p’ 表示。不难看出,大写字符表示的是白棋,小写字符表示的是黑棋。

车按国际象棋中的规则移动。东,西,南,北四个基本方向任选其一,然后一直向选定的方向移动,直到满足下列四个条件之一:

  • 棋手选择主动停下来。
  • 棋子因到达棋盘的边缘而停下。
  • 棋子移动到某一方格来捕获位于该方格上敌方(黑色)的卒,停在该方格内。
  • 车不能进入/越过已经放有其他友方棋子(白色的象)的方格,停在友方棋子前。

你现在可以控制车移动一次,请你统计有多少敌方的卒处于你的捕获范围内(即,可以被一步捕获的棋子数)。

示例

示例 1:

示例 1

输入: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]] \texttt{[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]} [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出: 3 \texttt{3} 3
解释:
在本例中,车能够捕获所有的卒。

示例 2:

示例 2

输入: [[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]] \texttt{[[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]} [[".",".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出: 0 \texttt{0} 0
解释:
象阻止了车捕获任何卒。

示例 3:

示例 3

输入: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]] \texttt{[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]} [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出: 3 \texttt{3} 3
解释:
车可以捕获位置 b5 \texttt{b5} b5 d6 \texttt{d6} d6 f5 \texttt{f5} f5 的卒。

数据范围

  • board.length = board[i].length = 8 \texttt{board.length}=\texttt{board[i].length}=\texttt{8} board.length=board[i].length=8
  • board[i][j] \texttt{board[i][j]} board[i][j] 可以是 ‘R’ \texttt{`R'} ‘R’ ‘.’ \texttt{`.'} ‘.’ ‘B’ \texttt{`B'} ‘B’ ‘p’ \texttt{`p'} ‘p’
  • 只有一个格子上存在 board[i][j] = ‘R’ \texttt{board[i][j]}=\texttt{`R'} board[i][j]=‘R’

解法

思路和算法

首先需要找到白色的车在棋盘上的位置,即所在的行和列。

然后需要判断白色的车分别往四个方向移动时,是否可以捕获黑色的卒。由于只能移动一次,因此每个方向最多可以捕获一个黑色的卒,可以捕获的黑色的卒的总数不会超过 4 4 4 个。对于每个方向,如果遇到了黑色的卒,则将其捕获,然后停止移动,如果遇到了白色的象或棋盘边缘,则停止移动。

代码

class Solution {public int numRookCaptures(char[][] board) {int rookRow = -1, rookColumn = -1;int rows = board.length, columns = board[0].length;int totalSquares = rows * columns;for (int i = 0; i < totalSquares; i++) {int row = i / columns, column = i % columns;if (board[row][column] == 'R') {rookRow = row;rookColumn = column;break;}}int count = 0;int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};for (int[] direction : directions) {int tempRow = rookRow + direction[0], tempColumn = rookColumn + direction[1];while (tempRow >= 0 && tempRow < rows && tempColumn >= 0 && tempColumn < columns) {if (board[tempRow][tempColumn] != '.') {if (board[tempRow][tempColumn] == 'p') {count++;}break;}tempRow += direction[0];tempColumn += direction[1];}}return count;}
}

复杂度分析

  • 时间复杂度: O ( n 2 ) O(n^2) O(n2),其中 n n n 是棋盘的边长,这道题中 n = 8 n=8 n=8
    寻找白色的车需要遍历棋盘,时间复杂度是 O ( n 2 ) O(n^2) O(n2)
    在四个方向判断有多少敌方的卒可以被一步捕获,最多需要遍历除了车所在的格子之外的 2 n − 2 2n-2 2n2 个格子,时间复杂度是 O ( n ) O(n) O(n)
    总时间复杂度是 O ( n 2 ) O(n^2) O(n2)

  • 空间复杂度: O ( 1 ) O(1) O(1)

这篇关于数组题目:可以被一步捕获的棋子数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/683534

相关文章

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

vue如何监听对象或者数组某个属性的变化详解

《vue如何监听对象或者数组某个属性的变化详解》这篇文章主要给大家介绍了关于vue如何监听对象或者数组某个属性的变化,在Vue.js中可以通过watch监听属性变化并动态修改其他属性的值,watch通... 目录前言用watch监听深度监听使用计算属性watch和计算属性的区别在vue 3中使用watchE

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

题目1254:N皇后问题

题目1254:N皇后问题 时间限制:1 秒 内存限制:128 兆 特殊判题:否 题目描述: N皇后问题,即在N*N的方格棋盘内放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在同一斜线上。因为皇后可以直走,横走和斜走如下图)。 你的任务是,对于给定的N,求出有多少种合法的放置方法。输出N皇后问题所有不同的摆放情况个数。 输入

题目1380:lucky number

题目1380:lucky number 时间限制:3 秒 内存限制:3 兆 特殊判题:否 提交:2839 解决:300 题目描述: 每个人有自己的lucky number,小A也一样。不过他的lucky number定义不一样。他认为一个序列中某些数出现的次数为n的话,都是他的lucky number。但是,现在这个序列很大,他无法快速找到所有lucky number。既然

C语言:柔性数组

数组定义 柔性数组 err int arr[0] = {0}; // ERROR 柔性数组 // 常见struct Test{int len;char arr[1024];} // 柔性数组struct Test{int len;char arr[0];}struct Test *t;t = malloc(sizeof(Test) + 11);strcpy(t->arr,

C 语言基础之数组

文章目录 什么是数组数组变量的声明多维数组 什么是数组 数组,顾名思义,就是一组数。 假如班上有 30 个同学,让你编程统计每个人的分数,求最高分、最低分、平均分等。如果不知道数组,你只能这样写代码: int ZhangSan_score = 95;int LiSi_score = 90;......int LiuDong_score = 100;int Zhou

计算数组的斜率,偏移,R2

模拟Excel中的R2的计算。         public bool fnCheckRear_R2(List<double[]> lRear, int iMinRear, int iMaxRear, ref double dR2)         {             bool bResult = true;             int n = 0;             dou

C# double[] 和Matlab数组MWArray[]转换

C# double[] 转换成MWArray[], 直接赋值就行             MWNumericArray[] ma = new MWNumericArray[4];             double[] dT = new double[] { 0 };             double[] dT1 = new double[] { 0,2 };