面试题 16.04. 井字游戏
一开始以为让我往里面放,结果是直接判断该局面谁赢了。。。
class Solution { public: string tictactoe(vector<string>& board) { int n=board.size(); bool flag=false; for(int i=0;i<n;i++) { int xx=0,oo=0; for(int j=0;j<n;j++) { if(board[i][j]=='X') xx++; else if(board[i][j]=='O') oo++; else if(board[i][j]==' ') flag=true; } if(xx==n) return "X"; if(oo==n) return "O"; } for(int i=0;i<n;i++) { int xx=0,oo=0; for(int j=0;j<n;j++) { if(board[j][i]=='X') xx++; else if(board[j][i]=='O') oo++; } if(xx==n) return "X"; if(oo==n) return "O"; } int xiez=1; for(int i=1;i<n;i++) { if(board[0][0]==board[i][i]) xiez++; } if(xiez==n&&board[0][0]=='O') return "O"; if(xiez==n&&board[0][0]=='X') return "X"; int xiey=1; for(int i=1;i<n;i++) { if(board[0][n-1]==board[i][n-i-1]) xiey++; } if(xiey==n&&board[0][n-1]=='O') return "O"; if(xiey==n&&board[0][n-1]=='X') return "X"; if(flag) return "Pending"; else return "Draw"; } };