用循环 暴力即可
class Solution { public: string tictactoe(vector<string>& board) { int n = board.size(); int flag1 = 0, flag2 = 0, flag3 = 1; for(int i = 0; i < n; i++) { char y = board[i][0]; if(y == ' ') { flag3 = 0; continue; } int j = 1; for(; j < n; j++) { if(board[i][j] == ' ') { flag3 = 0; break; } if(board[i][j] != y) break; } if(j == n) { if(y == 'X') flag1 = 1; else flag2 = 1; } } for(int j = 0; j < n; j++) { char y = board[0][j]; if(y == ' ') { flag3 = 0; continue; } int i = 1; for(; i < n; i++) { if(board[i][j] == ' ') { flag3 = 0; break; } if(board[i][j] != y) break; } if(i == n) { if(y == 'X') flag1 = 1; else flag2 = 1; } } char y = board[0][0]; if(y == ' ') flag3 = 0; if(y != ' ') { int i = 1; for(; i < n; i++) { if(board[i][i] == ' ') { flag3 = 0; break; } if(board[i][i] != y) break; } if(i == n) { if(y == 'X') flag1 = 1; else flag2 = 1; } } y = board[0][n - 1]; if(y == ' ') flag3 = 0; if(y != ' ') { int i = 1; for(; i < n; i++) { if(board[i][n - i - 1] == ' ') { flag3 = 0; break; } if(board[i][n - i - 1] != y) break; } if(i == n) { if(y == 'X') flag1 = 1; else flag2 = 1; } } if(flag1 == 1) return "X"; if(flag2 == 1) return "O"; return flag3 == 1 ? "Draw" : "Pending"; } };