返回介绍

solution / 1900-1999 / 1958.Check if Move is Legal / README

发布于 2024-06-17 01:03:12 字数 5662 浏览 0 评论 0 收藏 0

1958. 检查操作是否合法

English Version

题目描述

给你一个下标从 0 开始的 8 x 8 网格 board ,其中 board[r][c] 表示游戏棋盘上的格子 (r, c) 。棋盘上空格用 '.' 表示,白色格子用 'W' 表示,黑色格子用 'B' 表示。

游戏中每次操作步骤为:选择一个空格子,将它变成你正在执行的颜色(要么白色,要么黑色)。但是,合法 操作必须满足:涂色后这个格子是 好线段的一个端点 (好线段可以是水平的,竖直的或者是对角线)。

好线段 指的是一个包含 三个或者更多格子(包含端点格子)的线段,线段两个端点格子为 同一种颜色 ,且中间剩余格子的颜色都为 另一种颜色 (线段上不能有任何空格子)。你可以在下图找到好线段的例子:

给你两个整数 rMove 和 cMove 以及一个字符 color ,表示你正在执行操作的颜色(白或者黑),如果将格子 (rMove, cMove) 变成颜色 color 后,是一个 合法 操作,那么返回 true ,如果不是合法操作返回 false 。

 

示例 1:

输入:board = [[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],[".",".",".","W",".",".",".","."],["W","B","B",".","W","W","W","B"],[".",".",".","B",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","W",".",".",".","."]], rMove = 4, cMove = 3, color = "B"
输出:true
解释:'.','W' 和 'B' 分别用颜色蓝色,白色和黑色表示。格子 (rMove, cMove) 用 'X' 标记。
以选中格子为端点的两个好线段在上图中用红色矩形标注出来了。

示例 2:

输入:board = [[".",".",".",".",".",".",".","."],[".","B",".",".","W",".",".","."],[".",".","W",".",".",".",".","."],[".",".",".","W","B",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".","B","W",".","."],[".",".",".",".",".",".","W","."],[".",".",".",".",".",".",".","B"]], rMove = 4, cMove = 4, color = "W"
输出:false
解释:虽然选中格子涂色后,棋盘上产生了好线段,但选中格子是作为中间格子,没有产生以选中格子为端点的好线段。

 

提示:

  • board.length == board[r].length == 8
  • 0 <= rMove, cMove < 8
  • board[rMove][cMove] == '.'
  • color 要么是 'B' 要么是 'W' 。

解法

方法一

class Solution:
  def checkMove(
    self, board: List[List[str]], rMove: int, cMove: int, color: str
  ) -> bool:
    dirs = [(1, 0), (0, 1), (-1, 0), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]
    n = 8
    for a, b in dirs:
      i, j = rMove, cMove
      t = 0
      while 0 <= i + a < n and 0 <= j + b < n:
        t += 1
        i, j = i + a, j + b
        if board[i][j] in ['.', color]:
          break
      if board[i][j] == color and t > 1:
        return True
    return False
class Solution {
  private static final int[][] DIRS
    = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
  private static final int N = 8;

  public boolean checkMove(char[][] board, int rMove, int cMove, char color) {
    for (int[] d : DIRS) {
      int i = rMove, j = cMove;
      int t = 0;
      int a = d[0], b = d[1];
      while (0 <= i + a && i + a < N && 0 <= j + b && j + b < N) {
        ++t;
        i += a;
        j += b;
        if (board[i][j] == '.' || board[i][j] == color) {
          break;
        }
      }
      if (board[i][j] == color && t > 1) {
        return true;
      }
    }
    return false;
  }
}
class Solution {
public:
  vector<vector<int>> dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
  int n = 8;

  bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) {
    for (auto& d : dirs) {
      int a = d[0], b = d[1];
      int i = rMove, j = cMove;
      int t = 0;
      while (0 <= i + a && i + a < n && 0 <= j + b && j + b < n) {
        ++t;
        i += a;
        j += b;
        if (board[i][j] == '.' || board[i][j] == color) break;
      }
      if (board[i][j] == color && t > 1) return true;
    }
    return false;
  }
};
func checkMove(board [][]byte, rMove int, cMove int, color byte) bool {
  dirs := [8][2]int{{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
  n := 8
  for _, d := range dirs {
    a, b := d[0], d[1]
    i, j := rMove, cMove
    t := 0
    for 0 <= i+a && i+a < n && 0 <= j+b && j+b < n {
      t++
      i += a
      j += b
      if board[i][j] == '.' || board[i][j] == color {
        break
      }
    }
    if board[i][j] == color && t > 1 {
      return true
    }
  }
  return false
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文