1 题目
Design a Tic-tac-toe game that is played between two players on a n x n grid.
You may assume the following rules:
- A move is guaranteed to be valid and is placed on an empty block.
- Once a winning condition is reached, no more moves is allowed.
- A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game.
Example:
Given n = 3, assume that player 1 is "X" and player 2 is "O" in the board.
TicTacToe toe = new TicTacToe(3);
toe.move(0, 0, 1); -> Returns 0 (no one wins)
|X| | |
| | | | // Player 1 makes a move at (0, 0).
| | | |
toe.move(0, 2, 2); -> Returns 0 (no one wins)
|X| |O|
| | | | // Player 2 makes a move at (0, 2).
| | | |
toe.move(2, 2, 1); -> Returns 0 (no one wins)
|X| |O|
| | | | // Player 1 makes a move at (2, 2).
| | |X|
toe.move(1, 1, 2); -> Returns 0 (no one wins)
|X| |O|
| |O| | // Player 2 makes a move at (1, 1).
| | |X|
toe.move(2, 0, 1); -> Returns 0 (no one wins)
|X| |O|
| |O| | // Player 1 makes a move at (2, 0).
|X| |X|
toe.move(1, 0, 2); -> Returns 0 (no one wins)
|X| |O|
|O|O| | // Player 2 makes a move at (1, 0).
|X| |X|
toe.move(2, 1, 1); -> Returns 1 (player 1 wins)
|X| |O|
|O|O| | // Player 1 makes a move at (2, 1).
|X|X|X|
2 尝试解
2.1 分析
下井字棋,要求设置棋盘类,能够记录棋盘情况并且在每下一步后,判断当前胜负状态。要求复杂度做到O(N²)以下。棋盘上两位玩家的棋子分别用1和2表示,如果为空则用0表示。如果玩家1赢,返回1,如果玩家2赢,返回2,如果未定,返回0。
可以用column,row记录每一列每一行两个玩家的棋子数,用left,right记录左对角线和右对角线两个玩家的棋子数。每当玩家在位置[r,c]添加棋子player时,
column[player-1][c]+=1
row[player-1][c]+=1
if r==c left[player-1]+=1
if r+c == size - 1 right[player-1]+=1
然后判断column[player-1],row[player-1],left[player-1],right[player-1]中是否有等于size的元素。
2.2 代码
class TicTacToe {
public:
/** Initialize your data structure here. */
vector<vector<int>> board;
vector<vector<int>> column;
vector<vector<int>> row;
vector<int> left{0,0};
vector<int> right{0,0};
int size = 0;
TicTacToe(int n) {
size = n;
board = vector<vector<int>> (n,vector<int>(n,0));
column = vector<vector<int>> (2,vector<int>(n,0));
row = vector<vector<int>> (2,vector<int>(n,0));
}
/** Player {player} makes a move at ({row}, {col}).
@param row The row of the board.
@param col The column of the board.
@param player The player, can be either 1 or 2.
@return The current winning condition, can be either:
0: No one wins.
1: Player 1 wins.
2: Player 2 wins. */
int move(int r, int c, int player) {
board[r][c] = player;
row[player-1][r] += 1;
column[player-1][c] += 1;
if(r == c) right[player-1] += 1;
if(r+c == size-1) left[player-1] += 1;
return check(player);
}
int check(int player){
if(left[player-1]==size||right[player-1]==size)
return player;
for(auto state : column[player-1]){
if(state==size) return player;
}
for(auto state : row[player-1]){
if(state==size) return player;
}
return 0;
}
};
/**
* Your TicTacToe object will be instantiated and called as such:
* TicTacToe* obj = new TicTacToe(n);
* int param_1 = obj->move(row,col,player);
*/
3 标准解
3.1 分析
在下完棋检查时,可以只检查该棋子所在的行列,如果在对角线的话,再检查对角线即可。
3.2 代码
class TicTacToe {
public:
TicTacToe(int n) : sz(n) {
rows.resize(n, 0), cols.resize(n, 0);
diagonal = anti_diagonal = 0;
}
int move(int row, int col, int player) {
if (player == 1) {
++rows[row], ++cols[col];
if (row == col)
++diagonal;
if (row == sz - 1 - col)
++anti_diagonal;
if (rows[row] == sz || cols[col] == sz || diagonal == sz || anti_diagonal == sz)
return 1;
}
else {
--rows[row], --cols[col];
if (row == col)
--diagonal;
if (row == sz - 1 - col)
--anti_diagonal;
if (rows[row] == -sz || cols[col] == -sz || diagonal == -sz || anti_diagonal == -sz)
return 2;
}
return 0;
}
private:
vector<int> rows, cols;
int diagonal, anti_diagonal;
int sz;
};