LC 794. Valid Tic-Tac-Toe State

A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.

The board is a 3 x 3 array, and consists of characters " ", "X", and "O".  The " " character represents an empty square.

Here are the rules of Tic-Tac-Toe:

  • Players take turns placing characters into empty squares (" ").
  • The first player always places "X" characters, while the second player always places "O" characters.
  • "X" and "O" characters are always placed into empty squares, never filled ones.
  • The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal.
  • The game also ends if all squares are non-empty.
  • No more moves can be played if the game is over.
Example 1:
Input: board = ["O  ", "   ", "   "]
Output: false
Explanation: The first player always plays "X".

Example 2:
Input: board = ["XOX", " X ", "   "]
Output: false
Explanation: Players take turns making moves.

Example 3:
Input: board = ["XXX", "   ", "OOO"]
Output: false

Example 4:
Input: board = ["XOX", "O O", "XOX"]
Output: true

Note:

  • board is a length-3 array of strings, where each string board[i] has length 3.
  • Each board[i][j] is a character in the set {" ", "X", "O"}.

Runtime: 8 ms, faster than 21.69% of Java online submissions for Valid Tic-Tac-Toe State.

class Solution {
  private int[][][] state = new int[9][2][8];
  private int[][] dirs = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
  public boolean validTicTacToe(String[] board) {
    int xnum = 0, onum = 0;
    for (int i = 0; i < board.length; i++) {
      for (int j = 0; j < board[i].length(); j++) {
        if (board[i].charAt(j) == 'O') onum++;
        else if (board[i].charAt(j) == 'X') xnum++;
      }
    }
    if (!(onum == xnum || onum == xnum - 1)) return false;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 8; k++) {
          if(onum == xnum && board[i].charAt(j) == 'X' && state[i*3+j][0][k] != 0) continue;
          else if (onum == xnum && board[i].charAt(j) == 'X' && state[i*3+j][0][k] == 0) {
            int cnt = 0;
            state[i*3+j][0][k] = 1;
            int tmpx = i + dirs[k][0], tmpy = j + dirs[k][1];
            while(tmpx >= 0 && tmpx < board.length && tmpy >= 0 && tmpy < board[0].length() && board[tmpx].charAt(tmpy) == 'X') {
              state[tmpx*3+tmpy][0][k] = 1;
              tmpx += dirs[k][0]; tmpy += dirs[k][1];
              cnt++;
            }
            if(cnt == 2) return false;
          }
          else if(onum == xnum-1 && board[i].charAt(j) == 'O' && state[i*3+j][1][k] != 0) continue;
          else if(onum == xnum-1 && board[i].charAt(j) == 'O' && state[i*3+j][1][k] == 0){
            int cnt = 0;
            state[i*3+j][1][k] = 1;
            int tmpx = i + dirs[k][0], tmpy = j + dirs[k][1];
            while(tmpx >= 0 && tmpx < board.length && tmpy >= 0 && tmpy < board[0].length() && board[tmpx].charAt(tmpy) == 'O') {
              state[tmpx*3+tmpy][1][k] = 1;
              tmpx += dirs[k][0]; tmpy += dirs[k][1];
              cnt++;
            }
            if(cnt == 2) return false;

          }
        }
      }
    }
    return true;
  }
}

猜你喜欢

转载自www.cnblogs.com/ethanhong/p/10281319.html
今日推荐