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 ...
随机推荐
- Django批量插入数据和分页器
目录 一.ajax结合sweetalert实现删除按钮动态效果 二.bulk_create批量插入数据 1. 一条一条插入 2. 批量插入 三.自定义分页器 一.ajax结合sweetalert实现删 ...
- 【死磕JVM】五年 整整五年了 该知道JVM加载机制了!
类加载 Java虚拟机类加载过程是把Class类文件加载到内存,并对Class文件中的数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的java类型的过程 和那些编译时需要连接工作的语言不 ...
- 一些 html+css 细节
一. input 光标(插入符)颜色 input: { caret-color: #c0c0ff; } 二. 修改 placeholder 颜色 input::placeholder { color: ...
- CentOS7 下 MySQL 5.7.23 & XtraBackup 24 做数据备份(1)——安装软件
在两台机子上同时操作下面的步骤 首先安装MySQL,从官网下载相对应版本的RPM包 mysql-community-client-5.7.23-1.el7.x86_64.rpm mysql-commu ...
- pytorch(08)数据模型的读取(2)
import numpy as np import torch import os import random from PIL import Image from torch.utils.data ...
- JPress企业站主题-zbout
JPress企业站主题-zbout 经典的黑白灰颜色搭配风格,首页配置有轮播图.案例展示.公司简介.新闻中心.联系方式以及合作伙伴模块,全站使用了响应式结构,可以自适应电脑端和手机端浏览器访问.主题整 ...
- Python工程师学习之旅
1.Python软件开发基础 1.Linux操作系统2.Docker基础3.Python基础语法4.Python字符串解析5.Python正则表达式6.Python文件操作7.Python 模块8.P ...
- C# 应用 - 多线程 1) 多线程的知识图谱
- LNMP配置——Nginx配置 ——访问控制
#vi /usr/local/nginx/conf/vhost/test.com.conf 写入: server { listen 80; server_name test.com test1.com ...
- 以“有匪”为实战案例,用python爬取视频弹幕
最近腾讯独播热剧"有匪"特别火,我也一直在追剧,每次看剧的时候都是把弹幕开启的,这样子看剧才有灵魂呀.借助手中的技术,想爬取弹幕分析下这部电视剧的具体情况和网友们的评论!对于弹幕的 ...