在这本很棒的书中,我们被要求设计一种算法来判断某人是否在井字游戏中获胜。给出了以下解决方案,之后作者说:
请注意,通过添加行数和列数数组(以及对角线的两个总和),运行时间可以减少到 O(N)
我很努力,但我无法弄清楚那句话的意思。这些数组和总和是如何相加的?谢谢 !
enum Piece { Empty, Red, Blue };
enum Check { Row, Column, Diagonal, ReverseDiagonal }
Piece getIthColor(Piece[][] board, int index, int var, Check check) {
if (check == Check.Row) return board[index][var];
else if (check == Check.Column) return board[var][index];
else if (check == Check.Diagonal) return board[var][var];
else if (check == Check.ReverseDiagonal)
return board[board.length - 1 - var][var];
return Piece.Empty;
}
Piece getWinner(Piece[][] board, int fixed_index, Check check) {
Piece color = getIthColor(board, fixed_index, 0, check);
if (color == Piece.Empty) return Piece.Empty;
for (int var = 1; var < board.length; var++) {
if (color != getIthColor(board, fixed_index, var, check)) {
return Piece.Empty;
}
}
return color;
}
Piece hasWon(Piece[][] board) {
int N = board.length;
Piece winner = Piece.Empty;
// Check rows and columns
for (int i = 0; i < N; i++) {
winner = getWinner(board, i, Check.Row);
if (winner != Piece.Empty) {
return winner;
}
winner = getWinner(board, i, Check.Column);
if (winner != Piece.Empty) {
return winner;
}
}
winner = getWinner(board, -1, Check.Diagonal);
if (winner != Piece.Empty) {
return winner;
}
// Check diagonal
winner = getWinner(board, -1, Check.ReverseDiagonal);
if (winner != Piece.Empty) {
return winner;
}
return Piece.Empty;
}