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"}.

Approach  #1: Simulate. [Java]

class Solution {
public boolean validTicTacToe(String[] board) {
int numX = 0;
int numO = 0;
for (String str : board) {
for (int i = 0; i < str.length(); ++i) {
if (str.charAt(i) == 'X') numX++;
else if (str.charAt(i) == 'O') numO++;
}
}
if (numO > numX) return false; // ["O ", " ", " "]
if (numX > numO+1) return false; // ["XOX", " X ", " "]
if (gameOver(board, 'X') && numX == numO) return false; // ["XXX", " ", "OOO"]
if (gameOver(board, 'O') && numX > numO) return false; // ["OXX","XOX","OXO"] return true;
} private boolean gameOver(String[] board, char c) {
char[][] charArr = new char[3][3];
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
charArr[i][j] = board[i].charAt(j);
}
}
// top
if (charArr[0][1] == c && charArr[0][0] == c && charArr[0][2] == c)
return true; // bottom
if (charArr[1][1] == c && charArr[1][0] == c && charArr[1][2] == c)
return true; // left
if (charArr[1][0] == c && charArr[0][0] == c && charArr[2][0] == c)
return true; // right
if (charArr[1][2] == c && charArr[0][2] == c && charArr[2][2] == c)
return true; // center
if (charArr[1][1] == c && charArr[0][1] == c && charArr[2][1] == c)
return true;
if (charArr[1][1] == c && charArr[1][0] == c && charArr[1][2] == c)
return true;
if (charArr[1][1] == c && charArr[0][2] == c && charArr[2][0] == c)
return true;
if (charArr[1][1] == c && charArr[0][0] == c && charArr[2][2] == c)
return true; return false;
}
}

  

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. 【转+】以C++为核心语言的高频交易系统的讨论

    [前言]高频交易是量化交易的核心.主要分两个方向:计算机技术和交易策略.策略各有不同,一般都是数据分析的专家或者金融,机器学习从业者.在计算机技术方面,一个是交易平台的性能,二者是硬件的性能,延时的多 ...

  2. css常用居中方式

    一.水平居中 1.内联元素 父级元素加 text-align: center 即可 html <div class="container"> <a>内联元素 ...

  3. 不用typsescript也能使用类型增强功能

    由于 JS 的弱类型.宽松的编写规范.以及开发工具的弱鸡支持,我们在维护前人的代码时,经常会出现不知道某一个方法或字段命名来自于哪里,一定要在全局搜索以后慢慢筛查才能找到 同样我们在使用接口返回的对象 ...

  4. 记一次Drone无法触发构建的问题

    问题 好巧不巧,当你晚上准备上线的时候,在下午临近下班的时候CI&CD工具出问题了,提交代码不能触发构建,不能上线了,Drone平台那边也下班了,正好CICD依赖的公司git仓库也出问题了(就 ...

  5. Python中OS对目录的操作以及引用

    路径的获取 对当前目录的获取 1 path = os.getcwd() 2 print("获取到的当前目录是:({})".format(path)) 获取当前文件所在的绝对路径 i ...

  6. 迷宫问题(DFS)

    声明:图片及内容基于https://www.bilibili.com/video/BV1oE41177wk?t=3245 问题及分析 8*8的迷宫,最外周是墙,0表示可以走,1表示不可以走 设置迷宫 ...

  7. centos命令上传

    首先安装 lrzsz # yum -y install lrzsz 运行 rz 命令: 在弹出的窗口选择需要上传的文件,文件会被上传至对应的目录下 运行 sz file.name 在弹出的窗口选择保存 ...

  8. 使用HTML、jquery、DOM创建文本

    <html> <head> <meta charset="utf-8"> <meta charset="utf-8"& ...

  9. 2.pandas常用读取

    一.文本读写 名称 接收 代表(含义) 默认 filepath string 文件路径 无 sep string 分割符 ',' header Int/sequence 某行做列名 infer自动寻找 ...

  10. 安装RPM包或者源码包

    RPM工具 RPM他是以一种数据库记录的方式将我们所需要的套件安装到linux主机的一套管理程序关于RPM各个选项的含义如下-i:表示安装-v:表示可视化-h:表示安装进度在安装RPM包时,常用的附带 ...