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 stringboard[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的更多相关文章
- POJ 2361 Tic Tac Toe
题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...
- 【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 ...
- 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 ...
- 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 ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- Epic - Tic Tac Toe
N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...
- python 井字棋(Tic Tac Toe)
说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...
- ACM-Team Tic Tac Toe
我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...
- LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...
- [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 ...
随机推荐
- spring boot的 yml和properties的对比
Spring Boot 虽然做了大量的工作来简化配置,但其配置依然是相当的复杂!支持的外部配置方式就有很多种,笔者没有去统计,也许是为了灵活使用吧. application.yml 和 appli ...
- 使用 xunit 编写测试代码
使用 xunit 编写测试代码 Intro xunit 是 .NET 里使用非常广泛的一个测试框架,有很多测试项目都是在使用 xunit 作为测试框架,不仅仅有很多开源项目在使用,很多微软的项目也在使 ...
- Qt update刷新之源码分析(二)
大家好,我是IT文艺男,来自一线大厂的一线程序员 上次视频给大家从源码层面剖析了Qt update刷新机制的异步事件投递过程,这次视频主要从源码层面剖析Qt刷新事件(QEvent::UpdateReq ...
- 更改EFI分区位置
我是win10 + arch 双系统,并且efi分区用的是win10自动创建的(大小100m),所以这些空间很快就不够用了(内核和initramfs都放在了ESP分区当中) 我原本是直接把win的ef ...
- 剑指 Offer 33. 二叉搜索树的后序遍历序列 + 根据二叉树的后序遍历序列判断对应的二叉树是否存在
剑指 Offer 33. 二叉搜索树的后序遍历序列 Offer_33 题目详情 题解分析 本题需要注意的是,这是基于一颗二叉排序树的题目,根据排序二叉树的定义,中序遍历序列就是数据从小到大的排序序列. ...
- 在ASP.NET Core中用HttpClient(一)——获取数据和内容
在本文中,我们将学习如何在ASP.NET Core中集成和使用HttpClient.在学习不同HttpClient功能的同时使用Web API的资源.如何从Web API获取数据,以及如何直接使用Ht ...
- C#中的字段与属性的区别及属性的作用
C#中的字段与属性的区别及属性的作用 先上代码 public class Employee { //字段 private string name; //属性 public string Name { ...
- FreeBSD jail 折腾记(二)
FreeBSD jail 折腾记(二) 创建jail目录 创建4个 分别是模板 骨架 数据 项目 创建模板目录 mkdir -p /jail/j1 # 然后放入基本目录,上篇说过不再写 创建骨架目录 ...
- javascript 之对象-13
对象 无序属性的集合,属性可以包含基本值.对象或者函数,简单理解为对象是若干属性的集合:我们常说的面向对象(oop)编程其实是指的一种编码的思想,简单理解为用对象来封装数据,利用封装.继承.多态对代码 ...
- ch2_8_1求解n阶螺旋矩阵问题
思路:循环输出,注意边界控制 import java.util.Scanner; public class ch2_8_1求解n阶螺旋矩阵问题 { public static void main(St ...