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;
}
};
 

最新文章

  1. 微信小程序火车票查询 直取12306数据
  2. WCF服务编程
  3. oracle后台进程详解
  4. windows 下部署kafka 日记 转
  5. (整理)RPC
  6. MVC 小常识
  7. (未解决)android studio:com.android.support:appcompat-v7:22+ Could not found
  8. Qt浅译:JSON Support in Qt(JSON只有六种数据类型)
  9. 九度 和为S的连续正数序列
  10. 转载-windows下MySql5.6.17没有setup.exe时的安装方法
  11. Visual Studio 2017十五项新功能体验
  12. AngularJS高级程序设计读书笔记 -- 控制器篇
  13. 201521123103 《Java学习笔记》 第六周学习总结
  14. vmstat &amp; mpstat &amp; w
  15. 04_Python Data Structures
  16. haproxy实现会话保持(1):cookie
  17. mysql 遍历所有的库并根据表和sql语句备份
  18. Maven的下载,安装,配置,测试,初识以及Maven私服
  19. AtCoder Beginner Contest 044 C - 高橋君とカード / Tak and Cards
  20. 慕容小匹夫 Unity3D移动平台动态读取外部文件全解析

热门文章

  1. 「LOJ#10050」「一本通 2.3 例 2」The XOR Largest Pair (Trie
  2. python中文件打开的各个标识含义
  3. CentOS6.5中的vsftpd安装配置
  4. SYS/BIOS实例分析
  5. netty中的Channel、ChannelPipeline
  6. C#对SQL数据库操作类简介:Connection、Command、DataReader、DataSet、DataAdapter
  7. nop前端分页实现思路及步骤
  8. MySql获取记录的名次
  9. 具体问题:3、hibernate跟Mybatis/ ibatis 的区别,为什么选择?
  10. Entity Framework Code-First(10.1):EntityTypeConfiguration