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.
  1. Example 1:
  2. Input: board = ["O  ", "   ", "   "]
  3. Output: false
  4. Explanation: The first player always plays "X".
  5.  
  6. Example 2:
  7. Input: board = ["XOX", " X ", " "]
  8. Output: false
  9. Explanation: Players take turns making moves.
  10.  
  11. Example 3:
  12. Input: board = ["XXX", " ", "OOO"]
  13. Output: false
  14.  
  15. Example 4:
  16. Input: board = ["XOX", "O O", "XOX"]
  17. 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"}.

Approach  #1: Simulate. [Java]

  1. class Solution {
  2. public boolean validTicTacToe(String[] board) {
  3. int numX = 0;
  4. int numO = 0;
  5. for (String str : board) {
  6. for (int i = 0; i < str.length(); ++i) {
  7. if (str.charAt(i) == 'X') numX++;
  8. else if (str.charAt(i) == 'O') numO++;
  9. }
  10. }
  11. if (numO > numX) return false; // ["O ", " ", " "]
  12. if (numX > numO+1) return false; // ["XOX", " X ", " "]
  13. if (gameOver(board, 'X') && numX == numO) return false; // ["XXX", " ", "OOO"]
  14. if (gameOver(board, 'O') && numX > numO) return false; // ["OXX","XOX","OXO"]
  15.  
  16. return true;
  17. }
  18.  
  19. private boolean gameOver(String[] board, char c) {
  20. char[][] charArr = new char[3][3];
  21. for (int i = 0; i < 3; ++i) {
  22. for (int j = 0; j < 3; ++j) {
  23. charArr[i][j] = board[i].charAt(j);
  24. }
  25. }
  26. // top
  27. if (charArr[0][1] == c && charArr[0][0] == c && charArr[0][2] == c)
  28. return true;
  29.  
  30. // bottom
  31. if (charArr[1][1] == c && charArr[1][0] == c && charArr[1][2] == c)
  32. return true;
  33.  
  34. // left
  35. if (charArr[1][0] == c && charArr[0][0] == c && charArr[2][0] == c)
  36. return true;
  37.  
  38. // right
  39. if (charArr[1][2] == c && charArr[0][2] == c && charArr[2][2] == c)
  40. return true;
  41.  
  42. // center
  43. if (charArr[1][1] == c && charArr[0][1] == c && charArr[2][1] == c)
  44. return true;
  45. if (charArr[1][1] == c && charArr[1][0] == c && charArr[1][2] == c)
  46. return true;
  47. if (charArr[1][1] == c && charArr[0][2] == c && charArr[2][0] == c)
  48. return true;
  49. if (charArr[1][1] == c && charArr[0][0] == c && charArr[2][2] == c)
  50. return true;
  51.  
  52. return false;
  53. }
  54. }

  

794. Valid Tic-Tac-Toe State的更多相关文章

  1. POJ 2361 Tic Tac Toe

    题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...

  2. 【leetcode】1275. Find Winner on a Tic Tac Toe Game

    题目如下: Tic-tac-toe is played by two players A and B on a 3 x 3 grid. Here are the rules of Tic-Tac-To ...

  3. Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy

    1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...

  4. 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe

    题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...

  5. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  6. Epic - Tic Tac Toe

    N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...

  7. python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

  8. ACM-Team Tic Tac Toe

    我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...

  9. LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game

    地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...

  10. [LeetCode] 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 r ...

随机推荐

  1. Django批量插入数据和分页器

    目录 一.ajax结合sweetalert实现删除按钮动态效果 二.bulk_create批量插入数据 1. 一条一条插入 2. 批量插入 三.自定义分页器 一.ajax结合sweetalert实现删 ...

  2. 【死磕JVM】五年 整整五年了 该知道JVM加载机制了!

    类加载 Java虚拟机类加载过程是把Class类文件加载到内存,并对Class文件中的数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的java类型的过程 和那些编译时需要连接工作的语言不 ...

  3. 一些 html+css 细节

    一. input 光标(插入符)颜色 input: { caret-color: #c0c0ff; } 二. 修改 placeholder 颜色 input::placeholder { color: ...

  4. CentOS7 下 MySQL 5.7.23 & XtraBackup 24 做数据备份(1)——安装软件

    在两台机子上同时操作下面的步骤 首先安装MySQL,从官网下载相对应版本的RPM包 mysql-community-client-5.7.23-1.el7.x86_64.rpm mysql-commu ...

  5. pytorch(08)数据模型的读取(2)

    import numpy as np import torch import os import random from PIL import Image from torch.utils.data ...

  6. JPress企业站主题-zbout

    JPress企业站主题-zbout 经典的黑白灰颜色搭配风格,首页配置有轮播图.案例展示.公司简介.新闻中心.联系方式以及合作伙伴模块,全站使用了响应式结构,可以自适应电脑端和手机端浏览器访问.主题整 ...

  7. Python工程师学习之旅

    1.Python软件开发基础 1.Linux操作系统2.Docker基础3.Python基础语法4.Python字符串解析5.Python正则表达式6.Python文件操作7.Python 模块8.P ...

  8. C# 应用 - 多线程 1) 多线程的知识图谱

  9. LNMP配置——Nginx配置 ——访问控制

    #vi /usr/local/nginx/conf/vhost/test.com.conf 写入: server { listen 80; server_name test.com test1.com ...

  10. 以“有匪”为实战案例,用python爬取视频弹幕

    最近腾讯独播热剧"有匪"特别火,我也一直在追剧,每次看剧的时候都是把弹幕开启的,这样子看剧才有灵魂呀.借助手中的技术,想爬取弹幕分析下这部电视剧的具体情况和网友们的评论!对于弹幕的 ...