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:
boardis 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 ...
随机推荐
- Gym 101142 I.Integral Polygons(计算几何)
题意:给定一个凸包,现在让你连接凸包上两点,把凸包变为两个多边形,满足两个多边形的面积都是整数. 思路:我们知道整点的三角形面积S=叉积/2,则S要么是整数,要么是整数+0.5.那么多边形有多个三角形 ...
- 2017-2018-1 20179203 《Linux内核原理与分析》第四周作业
攥写人:李鹏举 学号:20179203 ( 原创作品转载请注明出处) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/US ...
- vue 打包去掉console.log
在webpack.prod.conf.js 文件中将设置修改为 先全局找到 UglifyJsPlugin 然后修改为: new UglifyJsPlugin({ uglifyOptions: { ...
- 自定义Panel中添加依赖属性需要注意的问题
今天帮忙同事调试一个自定义Panel的问题, 很奇怪, 利用Binding可以通过ItemSource来添加控件,但是在Listbox的xaml里添加几个ListboxItem却报异常: Visual ...
- canvas实现平铺
代码: /** * Created by Administrator on 2016/1/30. */ function draw(id){ var canvas = document.getElem ...
- 进程vs线程
内存中的内容不同 进程->{ 进程是系统分配资源的最基本单位,线程是进程的一部分, 进程中存储文件和网络句柄 } 线程->{ 栈(每个线程都有一个栈空间) pc(当前或下一条指令的地址,指 ...
- javaScript之事件处理程序
事件就是用户或浏览器自身执行的某个动作,JavaScript与HTML的交互也是通过事件实现的.而相应某个事件的函数就叫做事件处理函数.包括以下几种: 1.HTML事件处理程序 某个元素支持的每 ...
- ueditor1.4.3jsp版成功上传图片后却回显不出来与在线管理显示不出图片的解决方案
这是因为路径问题,可以在jsp/config.json这个文件去改路径 通过“imageUrlPrefix”与“imagePathFormat”这两个属性去拼凑路径. “imageUrlPrefix” ...
- Web Form要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptResourceMapping。”的解决办法。
1.先将aspnet.scriptmanager.jquery.dl 复制到bin (网站根目录下的bin文件夹找不到,看看下面的图片中点击[显示所有文档]) 文件夹下. 2.在网站根目录下s ...
- SQL查询语句大全集锦
SQL查询语句大全集锦 一. 简单查询 简单的Transact-SQL查询只包括选择列表.FROM子句和WHERE子句.它们分别说明所查询列.查询的 表或视图.以及搜索条件等. 例如,下面的语句查询t ...