zoukankan      html  css  js  c++  java
  • 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;
      }
    }
  • 相关阅读:
    三星t5拆解
    一条 SQL 引发的事故,同事直接被开除!!
    Git 不能提交空目录?我也是醉了!
    Redis 6.0.8 紧急发布,请尽快升级!
    String.format() 图文详解,写得非常好!
    为什么 Redis 要比 Memcached 更火?
    Lambda 表达式入门,这篇够了!
    天啊,为什么我的 Redis 变慢了。。
    写出一手烂代码的 19 条准则!
    Redis 面试一定要知道的 3 个 问题!
  • 原文地址:https://www.cnblogs.com/ethanhong/p/10281319.html
Copyright © 2011-2022 走看看