题目如下:

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:

  1. Input: moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
  2. Output: "A"
  3. Explanation: "A" wins, he always plays first.
  4. "X " "X " "X " "X " "X "
  5. " " -> " " -> " X " -> " X " -> " X "
  6. " " "O " "O " "OO " "OOX"

Example 2:

  1. Input: moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
  2. Output: "B"
  3. Explanation: "B" wins.
  4. "X " "X " "XX " "XXO" "XXO" "XXO"
  5. " " -> " O " -> " O " -> " O " -> "XO " -> "XO "
  6. " " " " " " " " " " "O "

Example 3:

  1. Input: moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
  2. Output: "Draw"
  3. Explanation: The game ends in a draw since there are no moves to make.
  4. "XXO"
  5. "OOX"
  6. "XOX"

Example 4:

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

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都下完后,判断当前棋盘上的是否存在三连即可。

代码如下:

  1. class Solution(object):
  2. def tictactoe(self, moves):
  3. """
  4. :type moves: List[List[int]]
  5. :rtype: str
  6. """
  7. grid = [[0] * 3 for _ in range(3)]
  8. for i in range(len(moves)):
  9. x, y = moves[i]
  10. grid[x][y] = 1 if i%2 == 0 else -1
  11.  
  12. if sum(grid[0]) == 3 or sum(grid[1]) == 3 or sum(grid[2]) == 3:
  13. return "A"
  14. elif sum(grid[0]) == -3 or sum(grid[1]) == -3 or sum(grid[2]) == -3:
  15. return "B"
  16. elif (grid[0][0] + grid[1][0] + grid[2][0]) == 3 or (grid[0][1] + grid[1][1] + grid[2][1]) == 3 or \
  17. (grid[0][2] + grid[1][2] + grid[2][2]) == 3:
  18. return "A"
  19. elif (grid[0][0] + grid[1][0] + grid[2][0]) == -3 or (grid[0][1] + grid[1][1] + grid[2][1]) == -3 or \
  20. (grid[0][2] + grid[1][2] + grid[2][2]) == -3:
  21. return "B"
  22. elif (grid[0][0] + grid[1][1] + grid[2][2] == 3) or (grid[0][2] + grid[1][1] + grid[2][0] == 3):
  23. return "A"
  24. elif (grid[0][0] + grid[1][1] + grid[2][2] == -3) or (grid[0][2] + grid[1][1] + grid[2][0] == -3):
  25. return "B"
  26. elif len(moves) == 9:
  27. return "Draw"
  28. return "Pending"

【leetcode】1275. Find Winner on a Tic Tac Toe Game的更多相关文章

  1. LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game

    地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...

  2. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  3. 【LeetCode】486. Predict the Winner 解题报告(Python)

    [LeetCode]486. Predict the Winner 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  4. 【LeetCode】一种博弈思路 minimax(共5题)

    [292] Nim Game (2019年3月12日,E) 有一堆石头,游戏规则是每次可以从里面拿1-3颗石头,拿到最后的石头的人赢.你和你的对手都 optimal 的玩这个游戏,问先手(也就是你)能 ...

  5. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  6. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  7. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  8. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  9. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

随机推荐

  1. oracle报:ORA-01034和ORA-27101的解决办法

    出现ORA-01034和ORA-27101的原因是多方面的:主要是oracle当前的服务不可用,shared memory realm does not exist,是因为oracle没有启动或没有正 ...

  2. linux kprobe rootkit学习

    介绍 <linux二进制分析>中提到了使用kprobe来写内核rootkit,还给出了一个简单的源码实现,这里看一下他的源码 kprobe kprobe的介绍可以看下面这几篇文章 介绍:h ...

  3. less的引用及公共变量的抽离

    一.什么是less? less是什么自然不用多言,乃一个css预编译器,可以扩展css语言,添加功能如如允许变量(variables),混合(mixins),函数(functions) 和许多其他的技 ...

  4. 怎样使用 v-if 实现 html 元素的显示 / 隐藏?

    1. 首先, 指令后的引号内是可以写 js 表达式的, 这点很重要. v-if 的用法很简单, 只需要给 v-if = " " 的引号内放一个 布尔值 即可. 注意: v-if 的 ...

  5. 15-Perl 格式化输出

    1.Perl 格式化输出Perl 是一个非常强大的文本数据处理语言.Perl 中可以使用 format 来定义一个模板,然后使用 write 按指定模板输出数据.Perl 格式化定义语法格式如下:fo ...

  6. Leaflet个人封装笔记

    <!DOCTYPE html> <html> <head> <link href="style/leaflet.css" type=&qu ...

  7. linux系统TCP协议之Send(转)

    tcp协议本身是可靠的,并不等于应用程序用tcp发送数据就一定是可靠的.不管是否阻塞,send发送的大小,并不代表对端recv到多少的数据. 在阻塞模式下, send函数的过程是将应用程序请求发送的数 ...

  8. Nginx作为代理服务之正反代理

    Nginx作为代理服务之正反代理 首先什么是代理,就跟明星的经纪人类似,比如作为苍老师经纪人的我,如果你们需要和苍老师拍小电影,可以跟我这个经纪人来商量比如价格啊,时间等相关信息,那么我就作为一个代理 ...

  9. php--常见算法3

    <?php function leijia($number){ $arr=[]; for($i=1;$i<=$number;$i++) { for($j=1;$j<=$number; ...

  10. windows 下Nginx 入门

    验证配置是否正确: nginx -t 查看Nginx的版本号:nginx -V 启动Nginx:start nginx 快速停止或关闭Nginx:nginx -s stop 正常停止或关闭Nginx: ...