← 返回 snowflake 的题目列表Connect Four - Can Play Win
类型:qbank
You are given an m x n board for a two-player game.
Connect Four - Can Play Win
You are given an m x n board for a two-player game.
SWE
game-engine
simulation
backtracking
medium
Frequency
Single report
Last asked
2026-06-06
Stage
phone-screen · onsite-coding
Connect Four - Can Play Win
Problem Requirements
You are provided with a game board of size m x n. Two players, "a" and "b", take turns playing. Each spot on the board contains one of three characters:
"." representing an empty space.
"a" representing a piece from Player A.
"b" representing a piece from Player B.
You will receive a specific move consisting of a row x, a column y, and the player making the move. Your task is to simulate placing that player's piece at board[x][y]. After placing the piece, determine if the player has won the game.
Rules for winning: A player wins if they connect 4 consecutive pieces in a straight line. This line can be in any of the following directions:
Horizontal (Left to Right)
Vertical (Up and Down)
Diagonal (\ direction)
Anti-diagonal (/ direction)
Important Rule: If the target spot board[x][y] is not empty (it already has a piece), the move is invalid. In this case, simply return false.
Note: The coordinates x and y are 0-indexed.
Test Cases
Case 1:
Input: board = [["a","a","a","."],[".",".",".","."]] x = 0, y = 3, player = "a"
Output: true
Why? When you place a piece at (0, 3), Player A gets four "a" pieces in a row horizontally.
Case 2:
Input: board = [["a","b",".","."],["b","a",".","."],[".",".",".","."],[".",".",".","."]] x = 2, y = 2, player = "a"
Output: false
Input Limits
The board dimensions m and n are between 1 and 200.
Each cell in board[i][j] will only contain ".", "a", or "b".
The player input is always either "a" or "b".
Row x is between 0 and m - 1.
Column y is between 0 and n - 1.