[leetcode] 1275. Find Winner on a Tic Tac Toe Game

Description

Tic-tac-toe is played by two players A and B on a 3 x 3 grid.

Here are the rules of Tic-Tac-Toe:

  • Players take turns placing characters into empty squares (" ").
  • The first player A always places “X” characters, while the second player B always places “O” characters.
  • “X” and “O” characters are always placed into empty squares, never on 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.

Given an array moves where each element is another array of size 2 corresponding to the row and column of the grid where they mark their respective character in the order in which A and B play.

Return the winner of the game if it exists (A or B), in case the game ends in a draw return “Draw”, if there are still movements to play return “Pending”.

You can assume that moves is valid (It follows the rules of Tic-Tac-Toe), the grid is initially empty and A will play first.

Example 1:

Input: moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
Output: "A"
Explanation: "A" wins, he always plays 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"
Explanation: "B" wins.
"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"
Explanation: The game ends in a draw since there are no moves to make.
"XXO"
"OOX"
"XOX"

Example 4:

Input: moves = [[0,0],[1,1]]
Output: "Pending"
Explanation: The game has not finished yet.
"X  "
" O "
"   "

Constraints:

  • 1 <= moves.length <= 9
  • moves[i].length == 2
  • 0 <= moves[i][j] <= 2
  • There are no repeated elements on moves.
  • moves follow the rules of tic tac toe.

分析

题目的意思是:给定一个数组,对应A和B的坐标,求最终游戏的状态,有四个,A赢,B赢,Draw和Pending。这道题是easy类型的题目,坑你Tic Tac Toe总共才9个格子吧,这道题我也做不出来,因为它里面包含了好多规律,大概就是如果9个格子都填满了就要返回Draw,如果A和B还未分出胜负就要返回Pending。其他的情况比如对角线的情况,是最终占据对角线的player获胜;还有占据整行整列的情况,这时候也是占据的player获胜。

代码

class Solution:
    def tictactoe(self, moves: List[List[int]]) -> str:
        fill=0
        m=[[0 for i in range(3)] for j in range(3)]
        j=0
        for move in moves:
            x=move[0]
            y=move[1]
            if(j==0):
                m[x][y]='A'
                j+=1
            else:
                m[x][y]='B'
                j=0
            fill+=1
        for i in range(3):
            if(m[i][0]!=0 and m[i][1]==m[i][0] and m[i][2]==m[i][1]):
                return m[i][0]
            elif(m[0][i]!=0 and m[1][i]==m[0][i] and m[2][i]==m[1][i]):
                return m[0][i]
        if(m[0][0]!=0 and m[1][1]==m[0][0] and m[2][2]==m[1][1]):
            return m[0][0]
        elif(m[0][2]!=0 and m[1][1]==m[0][2] and m[2][0]==m[1][1]):
            return m[0][2]
        elif(fill==9):
            return 'Draw'
        else:
            return 'Pending'

参考文献

[LeetCode] Easy to understand in py3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值