当前位置:网站首页>Leetcode 1275. Find out the winner of tic tac toe
Leetcode 1275. Find out the winner of tic tac toe
2022-07-19 15:01:00 【I'm not Xiao Haiwa~~~~】

A and B In a 3 x 3 Play tic tac toe on the grid of .
The rules of the tic tac toe game are as follows :
- Players take turns putting pieces in empty squares (" ") On .
- The first player A Always use “X” As a chess piece , And the second player B Always use “O” As a chess piece .
- “X” and “O” Can only be placed in an empty square , It can't be placed on the square that has been occupied .
- As long as there is 3 A the same ( Non empty ) The pieces are lined up in a straight line ( That's ok 、 Column 、 Diagonals ) when , Game over .
- If all the squares are full of pieces ( Not empty ), The game will end .
- After the game , The chess piece cannot move any more .
Give you an array moves, Each of these elements is of size 2 Another array of ( Elements correspond to the rows and columns of the grid ), It follows A and B The order of action ( First A after B) The positions of their pieces are recorded .
If there is a winner in the game (A or B), Return to the winner of the game ; If the game ends in a draw , Then return to “Draw”; If there's still going to be action ( The game is not over ), Then return to “Pending”.
You can assume moves all It works ( Follow the rules of tic tac toe ), The grid is initially empty ,A Will act first .
Example 1:
Input :moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
Output :"A"
explain :"A" win victory , He always goes first .
"X " "X " "X " "X " "X "
" " -> " " -> " X " -> " X " -> " X "
" " "O " "O " "OO " "OOX"
Example 2:
Input :moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
Output :"B"
explain :"B" win victory .
"X " "X " "XX " "XXO" "XXO" "XXO"
" " -> " O " -> " O " -> " O " -> "XO " -> "XO "
" " " " " " " " " " "O "
Example 3:
Input :moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
Output :"Draw"
Output : Because there is no way to act again , The game ended in a draw .
"XXO"
"OOX"
"XOX"
Example 4:
Input :moves = [[0,0],[1,1]]
Output :"Pending"
explain : The game is not over yet .
"X "
" O "
" "
Tips :
- 1 <= moves.length <= 9
- moves[i].length == 2
- 0 <= moves[i][j] <= 2
- moves There are no duplicate elements .
- moves Follow the rules of tic tac toe .
Code:
class Solution {
public:
string tictactoe(vector<vector<int>>& moves) {
vector<vector<int>> newOne(3, vector<int>(3, -1));
for(int i=0;i<moves.size();i++)
{
vector<int>sub=moves[i];
if(i%2)
{
newOne[sub[0]][sub[1]]=0;
}
else
{
newOne[sub[0]][sub[1]]=1;
}
}
// Line judgment
int cnt=0;
for(int i=0;i<3;i++)
{
vector<int>sub=newOne[i];
cnt=count(sub.begin(),sub.end(),1);
if(cnt==3)
return "A";
cnt=count(sub.begin(),sub.end(),0);
if(cnt==3)
return "B";
}
// Column judgment
bool left=false;
for(int i=0;i<3;i++)
{
int cnt1=0;
int cnt2=0;
for(int j=0;j<3;j++)
{
if(newOne[j][i]==1)
{
cnt1++;
}
else if(newOne[j][i]==0)
{
cnt2++;
}
else
left=true;
}
if(cnt1==3)
return "A";
if(cnt2==3)
return "B";
}
if(newOne[0][0]==1 && newOne[1][1]==1 && newOne[2][2]==1)
{
return "A";
}
if(newOne[0][2]==1 && newOne[1][1]==1 && newOne[2][0]==1)
return "A";
if(newOne[0][0]==0 && newOne[1][1]==0 && newOne[2][2]==0)
{
return "B";
}
if(newOne[0][2]==0 && newOne[1][1]==0 && newOne[2][0]==0)
return "B";
if(left)
return "Pending";
else
return "Draw";
return "";
}
};
边栏推荐
- [cute new problem solving] sum of four numbers
- Leetcode 1275. 找出井字棋的获胜者
- 论文阅读 TEMPORAL GRAPH NETWORKS FOR DEEP LEARNING ON DYNAMIC GRAPHS
- Principle and simple implementation of custom MVC
- Cross domain and CORS
- Natural language processing model of bigscience open source bloom
- The first step of agile: turn "iteration" into "sprint" and start!
- Practice of tDesign in vitest
- Is it safe to buy funds in a securities account? I want to make a fixed investment in the fund
- JVM common tuning configuration parameters
猜你喜欢

Labview32-bit and 64 bit compatibility

MySQL CPU usage is soaring. How to locate who is occupying it

Icml2022 | géométrie multimodale Contrastive Representation Learning

kube-proxy & Service & Endpoint

Notes on random nodes of force buckle 382 linked list

OSPF appendix anti ring Reissue

Sliding window maximum problem

Google Earth engine - Classification and processing of UAV images

dba

SQL wrong questions set of Niuke brush questions
随机推荐
Force deduction 912 sorting array notes
Unveil the mystery of service grid istio service mesh
Is it safe to open a fund account online now?
1. Basic concepts of DBMS
Alibaba microservice component Nacos registry
ORA-08103
1、DBMS基本概念
Deployment principle
Chang'an chain learning research - storage analysis wal mechanism
中断的分类
Explain C language dynamic memory management in detail
MySQL read / write separation
Explain the operation of C language file in detail
【MQTT从入门到提高系列 | 06】MQTT3.1.1之SUBSCRIBE订阅工作流
2. MySQL introduction
状态机练习
两种虚拟机的比较
模块1 作业
Sliding window maximum problem
MySQL index (III)