题目:
A Tic-Tac-Toe board is given as a string array
board
. Return True if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.
The
board
is a 3 x 3 array, and consists of characters
" "
,
"X"
, and
"O"
. The " " character represents an empty square.
Here are the rules of Tic-Tac-Toe:
- Players take turns placing characters into empty squares (" ").
- The first player always places "X" characters, while the second player always places "O" characters.
- "X" and "O" characters are always placed into empty squares, never filled ones.
- The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal.
- The game also ends if all squares are non-empty.
- No more moves can be played if the game is over.
Example 1:
Input: board = ["O ", " ", " "]
Output: false
Explanation: The first player always plays "X".
Example 2:
Input: board = ["XOX", " X ", " "]
Output: false
Explanation: Players take turns making moves.
Example 3:
Input: board = ["XXX", " ", "OOO"]
Output: false
Example 4:
Input: board = ["XOX", "O O", "XOX"]
Output: true
Note:
-
is a length-3 array of strings, where each stringboard
has length 3.board[i]
- Each
is a character in the setboard[i][j]
.{" ", "X", "O"}
思路:
分为两步进行检查:
1)统计‘X’出现的次数x_count和‘O’出现的次数o_count,合法的棋盘应该是要么‘X’出现的次数等于‘O’出现的次数(最后一步由player2下出),要么‘X’出现的次数等于‘O’出现的次数加1(最后一步由player1下出)。如果都不符合这两种情况,那么就可以直接返回false了。
2)统计形成三个连续‘X’的个数x_lines,以及形成三个连续‘O’的个数o_lines。由于棋盘上只有9个位置,所以o_lines不可能等于2。然后我们对x_lins的出现次数进行分类讨论:a) x_lines == 2,此时只有可能是某一行(列)和某个对角线形成了两个x_liens,例如["XXX", "OXO", "OOX"],所以只需要判断x_count是否为5即可;b) x_lines == 1,此时必须o_lines == 0并且棋盘上‘X’的个数大于‘O’的个数才行,因为只有这样才能说明是player1最后一步下了一个‘X’导致获胜的;c) 如果x_lines == 0,那么要么o_lines == 0表示双方都没有获胜,要么players2获胜了,即o_lines == 1 && (x_count == o_count)。
代码:
class Solution {
public:
bool validTicTacToe(vector<string>& board) {
// step 1: check the number of 'X's and 'O's
int x_count = 0, o_count = 0;
for (int r = 0; r < 3; ++r) {
for (int c = 0; c < 3; ++c) {
x_count += board[r][c] == 'X';
o_count += board[r][c] == 'O';
}
}
if (x_count != o_count && x_count != o_count + 1) {
return false;
}
// step 2: check the number of 'X'lines and 'O' lines
int x_lines = 0, o_lines = 0;
const string &r1 = board[0], &r2 = board[1], &r3 = board[2];
string c1, c2, c3, d1, d2;
c1 += board[0][0], c1 += board[1][0], c1 += board[2][0];
c2 += board[0][1], c2 += board[1][1], c2 += board[2][1];
c3 += board[0][2], c3 += board[1][2], c3 += board[2][2];
d1 += board[0][0], d1 += board[1][1], d1 += board[2][2];
d2 += board[0][2], d2 += board[1][1], d2 += board[2][0];
x_lines += r1 == "XXX", x_lines += r2 == "XXX", x_lines += r3 == "XXX";
x_lines += c1 == "XXX", x_lines += c2 == "XXX", x_lines += c3 == "XXX";
x_lines += d1 == "XXX", x_lines += d2 == "XXX";
o_lines += r1 == "OOO", o_lines += r2 == "OOO", o_lines += r3 == "OOO";
o_lines += c1 == "OOO", o_lines += c2 == "OOO", o_lines += c3 == "OOO";
o_lines += d1 == "OOO", o_lines += d2 == "OOO";
if (x_lines == 2) {
return x_count == 5;
}
else if (x_lines == 1) {
return o_lines == 0 && (x_count > o_count);
}
else {
return o_lines == 0 || (o_lines == 1 && x_count == o_count);
}
}
};