当前位置: 首页 > article >正文

leetcode 999. 可以被一步捕获的棋子数 简单

给定一个 8 x 8 的棋盘,只有一个 白色的车,用字符 'R' 表示。棋盘上还可能存在白色的象 'B' 以及黑色的卒 'p'。空方块用字符 '.' 表示。

车可以按水平或竖直方向(上,下,左,右)移动任意个方格直到它遇到另一个棋子或棋盘的边界。如果它能够在一次移动中移动到棋子的方格,则能够 吃掉 棋子。

注意:车不能穿过其它棋子,比如象和卒。这意味着如果有其它棋子挡住了路径,车就不能够吃掉棋子。

返回白车 攻击 范围内 兵的数量

示例 1:

输入:[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出:3
解释:
在本例中,车能够吃掉所有的卒。

示例 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",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出:0
解释:
象阻止了车吃掉任何卒。

示例 3:

输入:[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出:3
解释: 
车可以吃掉位置 b5,d6 和 f5 的卒。

提示:

  1. board.length == 8
  2. board[i].length == 8
  3. board[i][j] 可以是 'R''.''B' 或 'p'
  4. 只有一个格子上存在 board[i][j] == 'R'

分析:模拟。先找到车的位置,再分别向四个方向移动直到碰到第一个棋子。若棋子是黑卒,则答案数量加1,否则不加。最后返回答案。

int numRookCaptures(char** board, int boardSize, int* boardColSize) {
    int ans=0;
    int li,lj;
    for(int i=0;i<boardSize;++i)
    {
        int f=0;
        for(int j=0;j<boardSize;++j)
        {
            if(board[i][j]=='R')
            {
                li=i,lj=j;break;
            }
        }
    }
    for(int j=lj-1;j>=0;j--)
        if(board[li][j]=='B')break;
        else if(board[li][j]=='p'){
            ans++;break;
        }
    for(int j=lj+1;j<boardSize;j++)
        if(board[li][j]=='B')break;
        else if(board[li][j]=='p'){
            ans++;break;
        }
    for(int i=li-1;i>=0;i--)
        if(board[i][lj]=='B')break;
        else if(board[i][lj]=='p')
        {
            ans++;break;
        }
    for(int i=li+1;i<boardSize;i++)
        if(board[i][lj]=='B')break;
        else if(board[i][lj]=='p'){
            ans++;break;
        }

    return ans;
}


http://www.kler.cn/a/428220.html

相关文章:

  • 【Rust自学】15.7. 循环引用导致内存泄漏
  • 知识库管理驱动企业知识流动与工作协同创新模式
  • Celery
  • Node.js与MySQL模块结合:打造安全高效的用户信息管理系统
  • jemalloc 5.3.0的tsd模块的源码分析
  • 02-机器学习-核心概念
  • 【数字化】华为企业数字化转型-认知篇
  • centos安装jdk17 并自由切换jdk版本
  • 实用|金融银行项目测试业务流分析+常问面试题
  • 新能源汽车无钥匙进入一键启动功能介绍
  • 关于VS2019scanf的安全问题
  • Uniapp Android SpringBoot3 对接支付宝支付(最新教程附源码)
  • 【Linux】网络服务
  • k8s 之 DaemonSet
  • 【强化学习】策略梯度(Policy Gradient,PG)算法
  • 【MySQL】数据类型的注意点和应用
  • 008.精读《Apache Paimon Docs - Table w/o PK》
  • 【PyQt5教程 二】Qt Designer 信号与槽的使用方法及PyQt5基本小部件说明
  • CentOS8.5.2111(10)基于域名访问的邮件服务配置与管理
  • python录制鼠标键盘操作循环播放
  • LeetCode:28. 找出字符串中第一个匹配项的下标(KMP算法)
  • 【PlantUML系列】思维导图(五)
  • Flink自定义数据源
  • Netty 的多线程模型详解
  • LabVIEW中“this VI‘s owning library is missing”错误及解决
  • openharmony开发资料合集