题目链接:https://leetcode-cn.com/problems/tic-tac-toe-lcci/ 题目如下:
class Solution {
public:
string tictactoe(vector<string>& board) {
const int n=board.size();
int ax=0,ao=0;
int bx=0,bo=0;
bool isempty=true;
for(int i=0;i<n;i++){
if(board[i][i]=='X') ax++;
else if(board[i][i]=='O') ao++;
else isempty=false;
if(board[i][n-i-1]=='X') bx++;
else if(board[i][n-i-1]=='O') bo++;
else isempty=false;
}
if(ax==n||bx==n) return "X";
else if(ao==n||bo==n) return "O";
ax=0,ao=0;
bx=0,bo=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(board[i][j]=='X') ax++;
else if(board[i][j]=='O') ao++;
if(board[j][i]=='X') bx++;
else if(board[j][i]=='O') bo++;
if(board[i][j]==' ') isempty=false;
}
if(ax==n||bx==n) return "X";
else if(ao==n||bo==n) return "O";
ax=0,ao=0;
bx=0,bo=0;
}
if(isempty==false) return "Pending";
else return "Draw";
}
};
|