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 ...
随机推荐
- 让Django支持数据库长连接(可以提高不少性能哦)
书接上回 上回我们说到:<在生产系统使用Tornado WebServer来代替FastCGI加速你的Django应用> 那么现在很流行用一些高性能的nonblock的app server ...
- 通过Jquery异步获取股票实时数据
最近朋友让我帮他做个异步获取数据的程序,暂时服务器什么都没有,所以我就想先拿股票数据打个框架,方便后续开发和移植等事情 代码如下: <!-- 说明:股票看盘 作者:黑桃A 时间:2014-04- ...
- 【转】 Pro Android学习笔记(六八):HTTP服务(2):HTTP POST
目录(?)[-] 找一个测试网站 HTTP POST小例子 上次学习了HTTP GET请求,这次学习一下HTTP POST. 找一个测试网站 小例子好写,但要找个测试网站就有些麻烦,一下子无从入手,都 ...
- 一个有关Golang变量作用域的坑
转自:http://tonybai.com/2015/01/13/a-hole-about-variable-scope-in-golang/ 临近下班前编写和调试一段Golang代码,但运行结果始终 ...
- JS---星星评分
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- ng2 样式控制之style绑定和class绑定
- electron将网站打包成桌面应用
需求同 NW.js将网站打包成桌面应用 1. 从github上克隆electron示例项目 git clone https://github.com/electron/electron-quick-s ...
- IIS及时回收
在打开的列表中更改以下设置:回收——固定时间间隔(分钟) 改为 0进程模型——闲置超时(分钟) 改为 0
- PopupWindow --- 弹出底部窗体
第一步 : 布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- SQL标量值函数:返回汉字拼音首拼
今天遇到一个需求,客户要求在系统客户端选择客户的时候,可以用拼音首拼去快速过滤选择,此时我们在客户表里面加多一个拼音首拼字段CustPY来记录,字段加好了,我们要把所有客户名称的拼音简拼都更新到Cus ...