Title Description:
Use string arrays as game boards for jigsaw games. true is returned only if and only if it is possible for the player to place the characters in the state shown on the board during the Tic-Tac game.
The game board is a 3 x 3 array consisting of characters "," "X" and "O". The character "" represents a space.
The following are the rules of the Jingzi game:
Players take turns putting characters into spaces (").
The first player always puts the character "X" and the second player always puts the character "O".
"X" and "O" are only allowed to be placed in empty spaces, but not filled in the positions where the characters have been placed.
When three identical (and non-empty) characters fill any row, column or diagonal line, the game ends.
When all positions are not empty, the end of the game is also counted.
If the game is over, the player is not allowed to place any more characters.
Example 1:
Input: board = ["O", "," "O"]
Output: false
Explanation: The first player always puts "X".
Example 2:
Input: board = ["XOX", "X", "X"]
Output: false
Explanation: Players should take turns.
Example 3:
Input: board = ["XXX", ""OOO"]
Output: false
Example 4:
Input: board = ["XOX", "O", "XOX"]
Output: true
Explain:
The board is an array of strings with a length of 3, with each board[i] having a length of 3.
board[i][j] is a character in the set {"", "X", "O"}.
The first is to calculate the number of X and O in a given chessboard. If the number of X is greater than the number of O + 1, then it must return false. If the number of X and O is the same, then we can judge whether the chessboard itself satisfies the condition (whether the crosswise oblique is repeated, if not, return true directly), and if it is so heavy. Complex can only be O, because if it is X then the last piece put in X must repeat. Next, we find the last place put in O. If we can find it, we return true and false. If the number of X is one more than the number of O and the condition is not satisfied, then we try to replace an X to see if it is true. Satisfaction. If Satisfaction returns to true and can't be found in a circle, then we return to false.
class Solution { // 09:03:09 on July 31, 2019 public boolean validTicTacToe(String[] board) { int num1 = get('X', board); int num2 = get('O', board); if (num2 > num1) { return false; } if (num1 > num2 + 1) { return false; } if(!isvalid(board)){ // Try to change a letter to see if it works. if(num1 == num2){ // What can only be replaced is O. for (int i = 0; i < board.length; i++) { for (int j = 0; j < 3; j++) { if(board[i].charAt(j) == 'O'){ String tem = board[i].substring(0,j) + " " +board[i].substring(j + 1); board[i] = tem; if(isvalid(board)){ return true; } board[i] = board[i].substring(0,j) + "O" +board[i].substring(j + 1);; } } } return false; }else { // What can only be replaced is X. for (int i = 0; i < board.length; i++) { for (int j = 0; j < 3; j++) { if(board[i].charAt(j) == 'X'){ String tem = board[i].substring(0,j) + " " +board[i].substring(j + 1); board[i] = tem; if(isvalid(board)){ return true; } board[i] = board[i].substring(0,j) + "X" +board[i].substring(j + 1);; } } } return false; } }else { return true; } } public boolean isvalid(String board[]) { // Judgment Horizontal for (String string : board) { for (int i = 0; i < 3; i++) { if (string.charAt(i) != ' ') { if (string.charAt(1) == string.charAt(2) && string.charAt(1) == string.charAt(0)) { return false; } } } } // Judgment column for (int i = 0; i < 3; i++) { if (board[0].charAt(i) != ' ') { if (board[1].charAt(i) == board[2].charAt(i) && board[1].charAt(i) == board[0].charAt(i)) { return false; } } } // Judging diagonal line if (board[1].charAt(1) != ' ') { if ((board[0].charAt(0) == board[2].charAt(2) && board[0].charAt(0) == board[1].charAt(1)) || board[0].charAt(2) == board[2].charAt(0) && board[0].charAt(2) == board[1].charAt(1)) { return false; } } return true; } public int get(char tem, String board[]) { int result = 0; for (String string : board) { for (int i = 0; i < string.length(); i++) { if (string.charAt(i) == tem) { result++; } } } return result; } }