forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82c4b70
commit 6c9d6a6
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
class TicTacToe { | ||
int Row[100]; | ||
int Col[100]; | ||
int Diag[2]; | ||
public: | ||
/** Initialize your data structure here. */ | ||
int n; | ||
TicTacToe(int n) { | ||
this->n = n; | ||
memset(Row, 0, sizeof(Row)); | ||
memset(Col, 0, sizeof(Col)); | ||
memset(Diag, 0, sizeof(Diag)); | ||
} | ||
|
||
/** 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 row, int col, int player) | ||
{ | ||
if (player==1) | ||
{ | ||
Row[row]++; | ||
Col[col]++; | ||
if (row==col) | ||
Diag[0]++; | ||
if (row+col==n-1) | ||
Diag[1]++; | ||
|
||
if (Row[row]==n || Col[col]==n || Diag[0]==n || Diag[1]==n) | ||
return 1; | ||
} | ||
if (player==2) | ||
{ | ||
Row[row]--; | ||
Col[col]--; | ||
if (row==col) | ||
Diag[0]--; | ||
if (row+col==n-1) | ||
Diag[1]--; | ||
|
||
if (Row[row]==-n || Col[col]==-n || Diag[0]==-n || Diag[1]==-n) | ||
return 2; | ||
} | ||
// cout<<Row[row]<<" "<<Col[col]<<" "<<Diag[0]<<" "<<Diag[1]<<endl; | ||
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); | ||
*/ |