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 ...
随机推荐
- 第七届蓝桥杯JavaB组——第7题剪邮票
题目: 剪邮票 如[图1.jpg], 有12张连在一起的12生肖的邮票. 现在你要从中剪下5张来,要求必须是连着的. (仅仅连接一个角不算相连) 比如,[图2.jpg],[图3.jpg]中,粉红色所示 ...
- Java RPC 框架 Solon 1.3.7 发布,增强Cloud接口能力范围
Solon 是一个微型的Java RPC开发框架.项目从2018年启动以来,参考过大量前人作品:历时两年,4000多次的commit:内核保持0.1m的身材,超高的跑分,良好的使用体验.支持:RPC. ...
- 剑指 Offer 12. 矩阵中的路径 + 递归 + 深搜 + 字符串问题
剑指 Offer 12. 矩阵中的路径 题目链接 题目类似于迷宫的搜索. 需要注意的是,需要首先判断起始搜索的位置,可能有多个起点,都需要一一尝试. 每轮迭代的时候记得将是否遍历标记数组还原为未遍历的 ...
- JDBC概要
JDBC基础应用 JDBC是Java连接数据库的一套接口,可以让我们方便的在Java中使用数据库.掌握JDBC的使用是Java开发的基本功. 预备工作 导入jar包.根据使用的数据库软件导入相应的ja ...
- MySql数据库列表数据分页查询、全文检索API零代码实现
数据条件查询和分页 前面文档主要介绍了元数据配置,包括表单定义和表关系管理,以及表单数据的录入,本文主要介绍数据查询和分页在crudapi中的实现. 概要 数据查询API 数据查询主要是指按照输入条件 ...
- C语言中指针和多维数组
指针和多维数组 数组名是特殊的指针 数组是一个特殊的指针,多维数组也是更为复杂的数组,它们的关系是什么样的呢? 我们通过一个简单的例子来比较形象的了解指针和多维数组: int a[2][3]; 这是一 ...
- net5 webapi中 SwaggerUI如何进行版本控制
创建项目 net5就自带上了swaggerUI,见红色 // This method gets called by the runtime. Use this method to add servic ...
- MySQL全面瓦解25:构建高性能索引(案例分析篇)
回顾一下上面几篇索引相关的文章: MySQL全面瓦解22:索引的介绍和原理分析 MySQL全面瓦解23:MySQL索引实现和使用 MySQL全面瓦解24:构建高性能索引(策略篇) 索引的十大原则 1. ...
- python基础学习之集合set
.集合:set 特点:无序,不可重复(自动去重),可更改,可以与元组.列表互相转换 格式:s = {'x','y','z'} 转换:(转回用set) s = {'x','y','z'} ...
- HDU_5414 CRB and String 【字符串】
一.题目 CRB and String 二.分析 对于这题,读懂题意非常重要. 题目的意思是在$s$的基础上,按题目中所描述的步骤,即在$s$中任意选择一个字符$c$,在这个字符后面添加一个不等于$c ...