【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
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.
解题思路:把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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- git安装配置相关
Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理.在推出后,Git在其它项目中也取得了很大成功,尤其是在Ruby社区中.目前,包括Rubinius ...
- STM32的堆与栈与编译信息查看
STM32的堆与栈与编译信息查看 因为一个项目中使用malloc函数动态分配内存400多个字节,返回为0,分配失败.查找失败原因,为堆空间不足分配导致.查看堆和栈分别设置了2K,按正常情况看应能满足分 ...
- Codeforces 1238G. Adilbek and the Watering System
传送门 最关键的想法就是每个位置一定用的是当前能用的最便宜的水,因为到后面可能有更便宜的 然后其他还没用上的水我们也留着,假装此时已经买了,但是如果发现后面有更优的再反悔也不迟 每相邻两个朋友之间我们 ...
- 国内有哪些好的JAVA社区
转载自 https://www.zhihu.com/question/29836842#answer-13737722 并发编程网 - ifeve.com 强烈推荐 ImportNew - 专注Jav ...
- List与Set区别
List: 元素有序放入,元素可重复 Set: 元素无序保存,元素不可重复(通过==判断,非基本类型判断的是引用地址),因为set是无序的,故只能通过迭代器循环.ps:说是无序,但是其实set中的元素 ...
- Redis之过期策略
一.设置过期时间 Redis对存储值的过期处理实际上是针对该值的键(key)处理的,即时间的设置也是设置key的有效时间.Expires字典保存了所有键的过期时间,Expires也被称为过期字段. e ...
- HTML5之历史记录(实现的当页面应用路由器的底层)
history hashchange与popstate 一.history history.back():加载history列表中的前一个URL history.forward():加载history ...
- U盘安装 CentOS7图解
参考 https://jingyan.baidu.com/article/e75aca856cca69142fdac673.html
- raise与raise······from
在python中,如果想手动引发一个异常,我们一般都会使用raise # -*- coding:utf-8 -*- # @Author: WanMingZhu # @Date: 2019/10/22 ...
- 十四,K8s集群网络flannel及canal策略
目录 k8s网络CNI之flannel k8s网络通信模型 常见CNI插件(Container,Network,Interface) 插件通信一般的解决方案 网络插件的应用 Flannel插件 fla ...