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

判断井字棋是不是合法状态,想怎么判断就怎么判断吧

 class Solution {
public:
char Board[][];
bool check(int x,int y,int dx,int dy,char c){
for(int i=;i<;i++){
if(Board[x][y]!=c){
return ;
}
x+=dx;
y+=dy;
}
return ;
}
bool win(char a){
for(int i=;i<;i++){
if(check(,i,,,a)){
return ;
}
if(check(i,,,,a)){
return ;
}
}
if(check(,,,,a)){
return ;
}
if(check(,,,-,a)){
return ;
}
return ;
}
bool validTicTacToe(vector<string>& board) { int X=;
int O=;
int len=board.size();
for(int i=;i<len;i++){ for(int j=;j<;j++){ Board[i][j]=board[i][j]; if(Board[i][j]=='X'){
X++;
}
if(Board[i][j]=='O'){
O++;
}
}
// Str="";
}
if(X!=O&&O+!=X){
return false;
}
int x=win('X');
int o=win('O');
if(o+x>=){
return false;
}
// for(int i=0;i<3;i++){
// for(int j=0;j<3;j++){
// cout<<Board[i][j];
// }
// cout<<endl;
// }
if(x){
return X==(O+);
}
if(o){
//cout<<o<<endl;
return X==O;
}
return true;
}
};
 

74th LeetCode Weekly Contest Valid Tic-Tac-Toe State的更多相关文章

  1. 74th LeetCode Weekly Contest Valid Number of Matching Subsequences

    Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...

  2. 74th LeetCode Weekly Contest Preimage Size of Factorial Zeroes Function

    Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...

  3. 74th LeetCode Weekly Contest Number of Subarrays with Bounded Maximum

    We are given an array A of positive integers, and two positive integers L and R (L <= R). Return ...

  4. POJ 2361 Tic Tac Toe

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

  5. 【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 ...

  6. 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 ...

  7. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  8. 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 ...

  9. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

随机推荐

  1. 使用Visual Studio进行单元测试-Part1

    写在开头:Coding ain't done until all the tests run. No unit test no BB. -------------------------------- ...

  2. bzoj 4815 小Q的表格 —— 反演+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4815 思路就和这里一样:https://blog.csdn.net/leolyun/arti ...

  3. MySQL复制--最快的从库搭建方法(tar包) -转

    最快的从库搭建方法0,准备从库第一次数据备份,临时锁所有表开启窗口1 mysql> flush tables with read lock; Query OK, 0 rows affected  ...

  4. ORACLE数据库增加表空间大小或给表空间增加数据文件

    转载 2017年11月24日 11:00:28 ----查询表空间使用情况--- SELECT UPPER(F.TABLESPACE_NAME) "表空间名", D.TOT_GRO ...

  5. Spring boot 学习二:入门

    1: 需要的环境: JDK:至少JDK7才支持Spring boot maven:至少3.2 spring-boot:1.2.5.RELEASE(在pom.xml中指定) 2: 创建一个maven工程 ...

  6. Python-连接Redis并操作

    首先开启redis的外连 sch01ar@ubuntu:~$ sudo vim /etc/redis/redis.conf 把bind 127.0.0.1这行注释掉 然后重启redis sudo /e ...

  7. Umbraco安装过程中出现的问题以及调试

    在VS2015中使用NuGet安装完UmbracoCms后,按Ctrl+F5运行程序来完成安装UmbracoCms的过程中,发现一直在安装但是没有反应 估计是出现了错误.所以我到项目所在的文件夹中查找 ...

  8. hdu1050

    #include <cstdio> #include <algorithm> using namespace std; #define SIZE 205 struct Data ...

  9. C# 5.0中新增特性

    C# 5.0随着VisualStudio 2012一起正式发布了,让我们来看看C#5.0中增加了哪些功能. 1. 异步编程 在.Net 4.5中,通过async和await两个关键字,引入了一种新的基 ...

  10. vim 设置TAB宽度、显示行号、自动缩进、自动换行宽度

    一.vim  ~/.vimrc 二.添加如下几行:(括号中的不是,是我添加的) set shiftwidth=4          (表示每一级缩进的长度)set softtabstop=4     ...