【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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- oracle报:ORA-01034和ORA-27101的解决办法
出现ORA-01034和ORA-27101的原因是多方面的:主要是oracle当前的服务不可用,shared memory realm does not exist,是因为oracle没有启动或没有正 ...
- linux kprobe rootkit学习
介绍 <linux二进制分析>中提到了使用kprobe来写内核rootkit,还给出了一个简单的源码实现,这里看一下他的源码 kprobe kprobe的介绍可以看下面这几篇文章 介绍:h ...
- less的引用及公共变量的抽离
一.什么是less? less是什么自然不用多言,乃一个css预编译器,可以扩展css语言,添加功能如如允许变量(variables),混合(mixins),函数(functions) 和许多其他的技 ...
- 怎样使用 v-if 实现 html 元素的显示 / 隐藏?
1. 首先, 指令后的引号内是可以写 js 表达式的, 这点很重要. v-if 的用法很简单, 只需要给 v-if = " " 的引号内放一个 布尔值 即可. 注意: v-if 的 ...
- 15-Perl 格式化输出
1.Perl 格式化输出Perl 是一个非常强大的文本数据处理语言.Perl 中可以使用 format 来定义一个模板,然后使用 write 按指定模板输出数据.Perl 格式化定义语法格式如下:fo ...
- Leaflet个人封装笔记
<!DOCTYPE html> <html> <head> <link href="style/leaflet.css" type=&qu ...
- linux系统TCP协议之Send(转)
tcp协议本身是可靠的,并不等于应用程序用tcp发送数据就一定是可靠的.不管是否阻塞,send发送的大小,并不代表对端recv到多少的数据. 在阻塞模式下, send函数的过程是将应用程序请求发送的数 ...
- Nginx作为代理服务之正反代理
Nginx作为代理服务之正反代理 首先什么是代理,就跟明星的经纪人类似,比如作为苍老师经纪人的我,如果你们需要和苍老师拍小电影,可以跟我这个经纪人来商量比如价格啊,时间等相关信息,那么我就作为一个代理 ...
- php--常见算法3
<?php function leijia($number){ $arr=[]; for($i=1;$i<=$number;$i++) { for($j=1;$j<=$number; ...
- windows 下Nginx 入门
验证配置是否正确: nginx -t 查看Nginx的版本号:nginx -V 启动Nginx:start nginx 快速停止或关闭Nginx:nginx -s stop 正常停止或关闭Nginx: ...