当前位置:网站首页>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~~~~

etcode
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 "";
    }
};
原网站

版权声明
本文为[I'm not Xiao Haiwa~~~~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207172215177435.html