当前位置:网站首页>Buckle practice - 31 effective tic tac toe games
Buckle practice - 31 effective tic tac toe games
2022-07-24 12:13:00 【qq_ forty-three million four hundred and three thousand six hun】
31 Effective tic tac toe game
1. Problem description
Use string array as the game board of tic tac toe game board. If and only if during tic tac toe , When it is possible for players to place characters in the state shown on the game board , To return to true.
The game board is a 3 x 3 Array , By the character " ",“X” and “O” form . character " " For a vacant seat .
Here are the rules of the tic tac toe game :
Players take turns putting characters in the space (" ") in .
The first player always put characters “X”, And the second player always put characters “O”.
“X” and “O” It is only allowed to be placed in an empty space , It is not allowed to fill the position where the character has been placed .
When there is 3 Same ( It's not empty ) Fill any line with characters from 、 Column or diagonal line , Game over .
When all positions are not empty , It's the end of the game .
If the game is over , Players are not allowed to place any more characters .
Example 1:
Input : board = ["O ", " ", " "]
Output : false
explain : The first player always places “X”.
Example 2:
Input : board = [“XOX”, " X ", " "]
Output : false
explain : Players should be placed in turn .
Example 3:
Input : board = [“XXX”, " ", “OOO”]
Output : false
Example 4:
Input : board = [“XOX”, “O O”, “XOX”]
Output : true
explain :
Game board board It's a length of 3 Array of strings , Each of these strings board[i] The length of is 3.
board[i][j] Is a collection {" ", “X”, “O”} A character in .
The following can be used: main function :
int main()
{
vector<string> board;
string aRow;
for(int i=0; i<3; i++)
{
getline(cin,aRow);
board.push_back(aRow);
}
bool res=Solution().validTicTacToe(board);
cout<<(res?"true":"false")<<endl;
return 0;
}
2. Enter description
Enter three lines , Three characters per line , Every character belongs to a set {" ", “X”, “O”}
3. The output shows that
Output results :true or false
4. Example
Input
XOX
O O
XOX
Output
true
5. Code
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<set>
#include<stack>
using namespace std;
bool win(vector<string>&board, char p)
{
//1. a line / All are listed as X or O situation
for (int i = 0; i < 3; i++)
{
if ((board[i][0] == p && board[i][1] == p && board[i][2] == p) || (board[0][i] == p && board[1][i] == p && board[2][i] == p))
return true;
}
//2. Diagonal situation
return ((board[0][0] == p && board[1][1] == p && board[2][2] == p) || (board[0][2] == p && board[1][1] == p && board[2][0] == p));
}
bool validTicTacToe(vector<string> board)
{
//1. because X It's always better than O First put , therefore X The number must be greater than or equal to O Number If the first player wins , be X The number ratio is O More than one 1 ; If the second player wins , be X and O The number is equal
//2. non-existent 3 individual X In a row / Column , meanwhile 3 individual O In another line / Column
//1. Calculation x and 0 Number
int xcount = 0;
int ocount = 0;
for (string &row : board)// Go through every line
{
for (char c : row)// Traverse row Each element of the row
{
if (c == 'X')
xcount++;
if (c == 'O')
ocount++;
}
}
//2. Judge whether it is normal
// Reduction to absurdity
// As long as one of these conditions exists , Then the return is true , Finally, take the negative output result is false
return !(
(ocount != xcount && ocount != xcount - 1)//O and X When the number of is not equal , Be sure to meet X The number ratio is O The number is bigger
|| (ocount != xcount && win(board, 'O'))//O If you win ,X The number must be equal to O Number
|| (ocount != xcount - 1 && win(board, 'X'))//X If you win ,X The number must be equal to O Number plus 1
);
}
int main()
{
vector<string> board;
string aRow;
for (int i = 0; i < 3; i++)
{
getline(cin, aRow);
board.push_back(aRow);
}
bool res = validTicTacToe(board);
cout << (res ? "true" : "false") << endl;
return 0;
}
边栏推荐
- L1-064 估值一亿的AI核心代码
- Day4: circular structure
- Common formulas and application scenarios of discrete distribution
- CCF 1-2 question answering record (1)
- L1-049 seat allocation of ladder race
- L2-011 玩转二叉树
- leetcode:51. N 皇后
- 微信公众号开发:素材管理(临时、永久)
- How to use a third party without obtaining root permission topic: MIUI chapter
- Overview of MES system equipment management (medium)
猜你喜欢

Convergence rules for 4 * 4 image weights

1184. Distance between bus stops: simple simulation problem

Design of digital oscilloscope based on arm and FPGA -- QMJ

Microsoft SQL Server database language and function usage (XII)

QT notes - qtxml

Record a garbage collection and analysis of gceasy

Easy to use example

如何将Typora中图片上传到csdn

20000 words detailed explanation, thoroughly understand es!

Install MariaDB columnstore (version 10.3)
随机推荐
Experience of redis deepwater area -- Interview reference
如何最快找出复杂代码运行时的函数调用流程
Qt5.12 + vs2019 cannot locate the program input point in the dynamic link library
Import the data in MariaDB into columnstore
Counter attack dark horse: devdbops training, give you the best courses!
Common formulas and application scenarios of discrete distribution
MES系统设备管理概述(中)
leecode-268. 丢失的数字(异或的应用,找没有出现的数字,找只出现一次的数字)
【C和指针第14章】预处理器
gcc -l参数和-L参数的区别
Do you regret learning it?
Microsoft SQL Server database language and function usage (XII)
One of his birds sold for 60million -- the collection of eight mountain people in the Ming and Qing Dynasties
Pushgateway installation and Prometheus configuration
Day5: construct program logic
字符串匹配的KMP
JS image to Base64
SQL multi condition query cannot be implemented
Jackson parsing JSON detailed tutorial
Detailed OSPF configuration of layer 3 switch / router [Huawei ENSP experiment]