【leetcode】1275. Find Winner on a Tic Tac Toe Game
题目如下:
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
moveswhere 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
movesis 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 <= 9moves[i].length == 20 <= moves[i][j] <= 2- There are no repeated elements on
moves.movesfollow the rules of tic tac toe.
解题思路:把moves都下完后,判断当前棋盘上的是否存在三连即可。
代码如下:
class Solution(object):
def tictactoe(self, moves):
"""
:type moves: List[List[int]]
:rtype: str
"""
grid = [[0] * 3 for _ in range(3)]
for i in range(len(moves)):
x, y = moves[i]
grid[x][y] = 1 if i%2 == 0 else -1 if sum(grid[0]) == 3 or sum(grid[1]) == 3 or sum(grid[2]) == 3:
return "A"
elif sum(grid[0]) == -3 or sum(grid[1]) == -3 or sum(grid[2]) == -3:
return "B"
elif (grid[0][0] + grid[1][0] + grid[2][0]) == 3 or (grid[0][1] + grid[1][1] + grid[2][1]) == 3 or \
(grid[0][2] + grid[1][2] + grid[2][2]) == 3:
return "A"
elif (grid[0][0] + grid[1][0] + grid[2][0]) == -3 or (grid[0][1] + grid[1][1] + grid[2][1]) == -3 or \
(grid[0][2] + grid[1][2] + grid[2][2]) == -3:
return "B"
elif (grid[0][0] + grid[1][1] + grid[2][2] == 3) or (grid[0][2] + grid[1][1] + grid[2][0] == 3):
return "A"
elif (grid[0][0] + grid[1][1] + grid[2][2] == -3) or (grid[0][2] + grid[1][1] + grid[2][0] == -3):
return "B"
elif len(moves) == 9:
return "Draw"
return "Pending"
【leetcode】1275. Find Winner on a Tic Tac Toe Game的更多相关文章
- LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...
- 【leetcode】486. Predict the Winner
题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...
- 【LeetCode】486. Predict the Winner 解题报告(Python)
[LeetCode]486. Predict the Winner 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 【LeetCode】一种博弈思路 minimax(共5题)
[292] Nim Game (2019年3月12日,E) 有一堆石头,游戏规则是每次可以从里面拿1-3颗石头,拿到最后的石头的人赢.你和你的对手都 optimal 的玩这个游戏,问先手(也就是你)能 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- 二叉树(Java实现)
一.常见用语 1.逻辑结构:描述数据之间逻辑上的相关关系.分为线性结构(如,字符串),和非线性结构(如,树,图). 2.物理结构:描述数据的存储结构,分为顺序结构(如,数组)和链式结构. 3.结点的度 ...
- 解决MFC实际运行对话框与测试对话框显示效果不一致问题
1.打开#include "stdafx.h"头文件文档 2.在末尾加上#define _UNICODE即可
- 烯烃(olefin) 题解
题面 对于每个点,我们可以用一次dfs求出这个点到以这个点为字树的最远距离和次远距离: 然后用换根法再来一遍dfs求出这个点到除这个点子树之外的最远距离: 显然的,每次的询问我们可以用向上的最大值加向 ...
- 前缀和&二维前缀和
我们知道,数组上的前缀和S[i]=S[i-1]+a[i] 那么,怎样求二维前缀和呢? 二维前缀和: 绿色点的前缀和就是黄色.红色.灰色和绿色的点权和 怎样计算? s[i][j]=s[i-1][j]+s ...
- Python验证数据的抽样分布类型
假如要对一份统计数据进行分析,一般其来源来自于社会调研/普查,所以数据不是总体而是一定程度的抽样.对于抽样数据的分析,就可以结合上篇统计量及其抽样分布的内容,判断数据符合哪种分布.使用已知分布特性,可 ...
- Java EE Servlet相关的两个包
Servlet in Java EE 在Java EE的规范API中(链接),Servlet相关联的最重要的两个Package为: 1.javax.servlet 包含了一系列接口和类,他们在一个Se ...
- Guava -- 集合类 和 Guava Cache
Guava -- 集合类 和 Guava Caches 1. 什么是 Guava Guava 是 google 推出的一个第三方 java 库,用来代替 jdk 的一些公共操作,给我印象特别深的就是 ...
- CentOS/RHEL 安装EPEL第三方软件源
EPEL源简介 EPEL(Extra Packages for Enterprise Linux) 是由 FedORA 社区打造,为 RHEL 及衍生发行版如 CentOS等提供高质量软件包的项目.装 ...
- JS基础_对象的简介、对象的基本操作
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- QT编译Mysql驱动问题及解决方案
默认情况下,qt 并没有自带mysql的数据库插件,需要自己编译先安装mysql server ,运行setup.exe时选择自定义安装,安装目录设为"D:\mysqldev"不要 ...