【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-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- centos7安装oracle1201c
root身份安装依赖包: yum -y install binutils compat-libcap1 compat-libstdc++-33 compat-libstdc++-33*.i686 el ...
- 基于Docker安装 GitLab
⒈下载镜像 本文使用GitLab 中文社区版 Docker 镜像 Docker Hub地址:https://hub.docker.com/r/beginor/gitlab-ce 如果要体验最新版的Gi ...
- Yii2.0 组件
框架之所以是框架,是因为其强大,其封装了很多实用的功能,开发者可以开箱即用. 下边列举Yii2.0的部分组件: var_dump(Yii::$app->session->getId()); ...
- Open-falcon监控
https://book.open-falcon.org/zh_0_2/ 本文档记录了CentOS7.4下open-falcon-v2监控系统的部署流程,以及一些需要注意的地方. 环境准备 安装Red ...
- Asp.net core Enum as string + ef core value convertor
更新 : 2019-06-08 build in convertor https://docs.microsoft.com/en-us/ef/core/modeling/value-conversio ...
- lua与c的交互(运用)
(1)lua程序 (2)C++程序(头文件) extern "C" { #include "lua.h" #include "lual ...
- django 支持跨域请求配置
参考:https://www.jianshu.com/p/63fb55bee142 核心注意点:
- java——ArrayList中contains()方法中的疑问
问题引子: ist<Student> students=new ArrayList<Student>(); students.add(new Student("201 ...
- 异常-Throwable的几个常见方法
package cn.itcast_04; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...
- js中with的作用
js中with的作用当一个对象有多个需要操作的属性或方法时,可以使用如<体>试验<script type=“text/javascript”>var o=文件.创建元素(“DI ...