74th LeetCode Weekly Contest 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"}
.
判断井字棋是不是合法状态,想怎么判断就怎么判断吧
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 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 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
随机推荐
- Python 转义字符中没有这个 「\e」 !
问题来源于技术交流群里: 常见的转义字符 \n.\t 之类的我们都知道什么意思,但是这个 \e 是什么意思呢? 抱着一股钻研的精神,我搜了一把. 结果,所有的页面里都是只有一句简单的 \e 代表转义. ...
- 【JVM】jvm垃圾回收器相关垃圾回收算法
引用计数法[原理]--->引用计数器是经典的也是最古老的垃圾收集防范.--->实现原理:对于对象A,只要有任何一个对象引用A,则计数器加1.当引用失效时,计数器减1.只要对象A的计数器值为 ...
- ACM学习历程—HDU4675 GCD of Sequence(莫比乌斯)
Description Alice is playing a game with Bob. Alice shows N integers a 1, a 2, …, a N, and M, K. She ...
- 【LeetCode】012. Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- WSGI服务与django的关系
WSGI接口 wsgi是将python服务器程序连接到web服务器的通用协议.uwsgi是独立的实现了wsgi协议的服务器. web服务器 服务端程序 简化版的WSGI架构 服务端程序(类似dja ...
- POJ2449:K短路
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 26355 Accepted: 7170 ...
- loadrunner手动生成脚本函数
1.点击insert
- IIS及时回收
在打开的列表中更改以下设置:回收——固定时间间隔(分钟) 改为 0进程模型——闲置超时(分钟) 改为 0
- python 基础 字典 增删改查
content = {"name":"wd","pc":{"phone":111111,"age": ...
- ViewPage+Fragment(仿微信切换带通知)
第一步 : 布局文件 activity_main.xml <?xml version="1.0" encoding="utf-8"?> <Li ...