地址  https://leetcode-cn.com/problems/find-winner-on-a-tic-tac-toe-game/submissions/

题目描述
A 和 B 在一个 3 x 3 的网格上玩井字棋。

井字棋游戏的规则如下:

玩家轮流将棋子放在空方格 (” “) 上。
第一个玩家 A 总是用 “X” 作为棋子,而第二个玩家 B 总是用 “O” 作为棋子。
“X” 和 “O” 只能放在空方格中,而不能放在已经被占用的方格上。
只要有 3 个相同的(非空)棋子排成一条直线(行、列、对角线)时,游戏结束。
如果所有方块都放满棋子(不为空),游戏也会结束。
游戏结束后,棋子无法再进行任何移动。
给你一个数组 moves,其中每个元素是大小为 2 的另一个数组(元素分别对应网格的行和列),它按照 A 和 B 的行动顺序(先 A 后 B)记录了两人各自的棋子位置。

如果游戏存在获胜者(A 或 B),就返回该游戏的获胜者;如果游戏以平局结束,则返回 “Draw”;如果仍会有行动(游戏未结束),则返回 “Pending”。

你可以假设 moves 都 有效(遵循井字棋规则),网格最初是空的,A 将先行动

  1. 示例
  2.  
  3. 输入:moves = [[,],[,],[,],[,],[,]]
  4. 输出:"A"
  5. 解释:"A" 获胜,他总是先走。
  6. "X " "X " "X " "X " "X "
  7. " " -> " " -> " X " -> " X " -> " X "
  8. " " "O " "O " "OO " "OOX"
  9. 示例
  10.  
  11. 输入:moves = [[,],[,],[,],[,],[,],[,]]
  12. 输出:"B"
  13. 解释:"B" 获胜。
  14. "X " "X " "XX " "XXO" "XXO" "XXO"
  15. " " -> " O " -> " O " -> " O " -> "XO " -> "XO "
  16. " " " " " " " " " " "O "
  17. 示例
  18.  
  19. 输入:moves = [[,],[,],[,],[,],[,],[,],[,],[,],[,]]
  20. 输出:"Draw"
  21. 输出:由于没有办法再行动,游戏以平局结束。
  22. "XXO"
  23. "OOX"
  24. "XOX"
  25. 示例
  26.  
  27. 输入:moves = [[,],[,]]
  28. 输出:"Pending"
  29. 解释:游戏还没有结束。
  30. "X "
  31. " O "
  32. " "
  33.  
  34.  
  35. 提示:
  36.  
  37. <= moves.length <=
  38. moves[i].length ==
  39. <= moves[i][j] <=
  40. moves 里没有重复的元素。
  41. moves 遵循井字棋的规则。

算法1
主要是模拟 横竖左斜右斜 四种检测是否连成一线判断胜负

额外做了剪枝 如果没走到5步 也就是任何一方都没有下三次 那么就没连线可能

C++ 代码

  1. class Solution {
  2. public:
  3.  
  4. vector<vector<char>> board;
  5. string tictactoe(vector<vector<int>>& moves) {
  6. board = vector<vector<char>>(, vector<char>());
  7. if (moves.size() < ) return "Pending";
  8. for (int i = ; i < moves.size(); i++) {
  9. int x = moves[i][];int y = moves[i][];
  10. if (i % == ) {board[x][y] = 'A'; }
  11. else {board[x][y] = 'B';}
  12. int xcount = ; int ycount = ; int xycount = ;
  13. for (int idx = -; idx <= ; idx++) {
  14. if (x + idx >= && x + idx < && board[x + idx][y] == board[x][y]) {
  15. xcount++;
  16. }
  17. if (y + idx >= && y + idx < && board[x][y + idx] == board[x][y]) {
  18. ycount++;
  19. }
  20. if(x+y == || x==y){
  21. if (board[][] == board[][] && board[][] == board[][] && board[][] != ) {
  22. string s = "T";
  23. s[] = board[x][y];
  24. return s;
  25. }
  26. if (board[][] == board[][] && board[][] == board[][] && board[][] != ) {
  27. string s = "T";
  28. s[] = board[x][y];
  29. return s;
  30. }
  31. }
  32.  
  33. if (xcount == || ycount == ) {
  34. string s = "T";
  35. s[] = board[x][y];
  36. return s;
  37. }
  38. }
  39. }
  40.  
  41. if (moves.size() == ) {
  42. return "Draw";
  43. }
  44. else {
  45. return "Pending";
  46. }
  47. }
  48.  
  49. };
  50.  
  51. 作者:defddr
  52. 链接:https://www.acwing.com/solution/LeetCode/content/6670/
  53. 来源:AcWing
  54. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game的更多相关文章

  1. leetcode.1275找出井字棋的获胜者

    A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: 玩家轮流将棋子放在空方格 (" ") 上.第一个玩家 A 总是用 "X" 作为棋子, ...

  2. leetcode-165周赛-1275-找出井字棋的获胜者

    题目描述: 自己的提交: class Solution: def tictactoe(self, moves: List[List[int]]) -> str: p = [[0] * 3 for ...

  3. [LeetCode] Design Tic-Tac-Toe 设计井字棋游戏

    Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...

  4. 【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-To ...

  5. [LeetCode] Valid Tic-Tac-Toe State 验证井字棋状态

    A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to r ...

  6. [LeetCode] 794. Valid Tic-Tac-Toe State 验证井字棋状态

    A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to r ...

  7. [LeetCode] 348. Design Tic-Tac-Toe 设计井字棋游戏

    Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...

  8. [HTML5实现人工智能]小游戏《井字棋》发布,据说IQ上200才能赢

    一,什么是TicTacToe(井字棋)   本 游戏 为在下用lufylegend开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿 ...

  9. Java实现简单井字棋

    Java第一次实验,老师让做一个井字棋,电脑随机下棋. 然后就想能不能聪明一点,可以判断出走哪一步棋:然后只能做到不会输,还是不够聪明,只能呆板地堵住用户,smartRobot的第三个判断逻辑找不到最 ...

随机推荐

  1. doPost()和doGet()方法的区别?

    GET和POST请求都是http的请求方式,用户通过不同的http的请求方式完成对资源(url)的不同操作.GET,POST,PUT,DELETE就对应着对这个资源的查 ,改 ,增 ,删 4个操作,具 ...

  2. Highlight List View Objects 突出显示列表视图对象

    In this lesson, you will learn how to format data that satisfies the specified criteria. For this pu ...

  3. linux用户身份与文件权限

    用户 useradd [ 参数 ] 用户名 添加用户 sudo useradd -d /home/test -u 1001 -s /bin/bash name usermod [选项] 用户名 更改用 ...

  4. DomDom: 1 Vulnhub Walkthrough

    主机层面扫描: ╰─ nmap -p1-65535 -A -sV 10.10.202.140 You name 存在XSS 漏洞 右键源码有隐藏form表单 修改其type属性为:text 尝试了SQ ...

  5. 请求*.html后缀无法返回json数据的问题

    在springmvc中请求*.html不可以返回json数据. 修改web.xml,添加url拦截格式.

  6. Linux系统学习 十六、VSFTP服务—本地用户访问—基本用户基础配置

    缺点,ftp密码是和系统密码是一致的,并不安全 先设置两个测试用户 test1      123123 test2      123123 基本用户基础配置 1.本地用户基本配置 local_enab ...

  7. 每日JAVA面试

  8. 计算机网络知识之TCP/IP协议簇

    OSI参考模型 OSI的来源         OSI(Open System Interconnect),即开放式系统互联. 一般都叫OSI参考模型,是ISO(国际标准化组织)组织在1985年研究的网 ...

  9. C# 对 Excel 的相关操作

    C# 对Excel的操作 学习自: 教练辅导 C# 对Excel的读取操作 我们需要额外添加引用: References 搜索Excel 这样我们的基础就添加完成了. 并且在using 中添加: us ...

  10. java8-CompleableFuture的使用1

    背景 硬件的极速发展,多核心CPU司空见惯:分布式的软件架构司空见惯: 功能API大多采用混聚的方式把基础服务的内容链接在一起,方便用户生活. 抛出了两个问题: 如何发挥多核能力: 切分大型任务,让每 ...