leetcode-165周赛-1275-找出井字棋的获胜者
题目描述:



自己的提交:
class Solution:
def tictactoe(self, moves: List[List[int]]) -> str:
p = [[0] * 3 for _ in range(3)]
if len(moves) < 5:
return "Pending"
for i in moves[::2]:
p[i[0]][i[1]] = "X"
for i in moves[1::2]:
p[i[0]][i[1]] = "O"
for i in p:
if i == ["X","X","X"]:
return "A"
if i == ["O","O","O"]:
return "B"
for i in zip(p[0],p[1],p[2]):
if i == ("X","X","X"):
return "A"
if i == ("O","O","O"):
return "B"
if p[0][0] == p[1][1] == p[2][2] == "X" or p[0][2] == p[1][1] == p[2][0] == "X":
return "A"
if p[0][0] == p[1][1] == p[2][2] == "O" or p[0][2] == p[1][1] == p[2][0] == "O":
return "B"
for i in p:
if 0 in i:
return "Pending"
return "Draw"
优化:
class Solution(object):
def tictactoe(self, moves):
board = [[" "," "," "],[" "," "," "],[" "," "," "]] def solved():
diags = [[board[0][0], board[1][1], board[2][2]], [board[0][2], board[1][1], board[2][0]]]
for row in board + zip(*board) + diags:
s = set(row)
if len(s) == 1:
a, =s
if a in "XO":
return "A" if a == "X" else "B"
return None p = 0
for x, y in moves:
board[x][y] = "XO"[p]
p^=1
if solved():
return solved()
return "Pending" if len(moves) < 9 else "Draw"
另:
class Solution:
def tictactoe(self, moves: List[List[int]]) -> str:
allpath = [
[[0,0],[0,1],[0,2]],
[[1,0],[1,1],[1,2]],
[[2,0],[2,1],[2,2]],
[[0,0],[1,0],[2,0]],
[[0,1],[1,1],[2,1]],
[[0,2],[1,2],[2,2]],
[[0,0],[1,1],[2,2]],
[[2,0],[1,1],[0,2]]
]
def test(a):
for line in allpath:
if len(list(filter(lambda path: path in a, line))) == 3:
return True
return False if test(moves[0::2]):
return "A"
elif test(moves[1::2]):
return "B"
elif len(moves) == 9:
return "Draw"
else:
return "Pending"
leetcode-165周赛-1275-找出井字棋的获胜者的更多相关文章
- leetcode.1275找出井字棋的获胜者
A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: 玩家轮流将棋子放在空方格 (" ") 上.第一个玩家 A 总是用 "X" 作为棋子, ...
- LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...
- [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 ...
- leetcode 双周赛9 找出所有行中最小公共元素
给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1. 示例: 输入:mat = [[,,,,] ...
- [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 ...
- [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 ...
- [HTML5实现人工智能]小游戏《井字棋》发布,据说IQ上200才能赢
一,什么是TicTacToe(井字棋) 本 游戏 为在下用lufylegend开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿 ...
- Java实现简单井字棋
Java第一次实验,老师让做一个井字棋,电脑随机下棋. 然后就想能不能聪明一点,可以判断出走哪一步棋:然后只能做到不会输,还是不够聪明,只能呆板地堵住用户,smartRobot的第三个判断逻辑找不到最 ...
- 『HTML5实现人工智能』小游戏《井字棋》发布,据说IQ上200才能赢【算法&代码讲解+资源打包下载】
一,什么是TicTacToe(井字棋) 本游戏为在下用lufylegend开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿童欢迎. ...
随机推荐
- Vue.js----router(路由)
什么是路由? 路由可看做是一个键值对,K-V. K => path()路径地址 V=> 处理请求的回调函数 前台路由 K => path()路径地址 V=> 路由组件 编写使用 ...
- boost multi array
Boost MultiArray is a library that simplifies using arrays with multiple dimensions. 1. #include < ...
- mui is not defined
vue项目中引用mui.js,我是在main.js中这样引入的, 结果报错 查找资料,最后在mui.js的最后添加了这样一句 这是因为mui并不能像jquery那样作为全局对象存在,加上wi ...
- 【SPOJ1811】Longest Common Substring(后缀自动机)
题意:给定两个仅含小写字母的字符串,求他们最长公共子串的长度 n<=250000 思路: #include<bits/stdc++.h> using namespace std; t ...
- spring+cxf
里面有http://127.0.0.1:8081/dcs/soap/cls http://127.0.0.1:8081/dcs/soap/cms http://127.0.0. ...
- window安装nginx
下载安装 到nginx官网上下载相应的安装包,http://nginx.org/en/download.html: 下载进行解压,将解压后的文件放到自己心仪的目录下,我的解压文件放在了d盘根目录下,如 ...
- 【ngx-ueditor】百度编辑器按下Shift键不触发contentChange事件
背景:基于Angular 6,引入ngx-ueditor 发现现象:如果以Shift键+任意键结尾,则ngModel会丢失包含shift键的字符 例如:输入“ABC+AB++++”,则ngModel中 ...
- linux超级块和inode 详解 和 df 、du 命令详解与环境变量
一.inode块,Unix文件的核心. 首先需要明白的是,在Unix操作系统中的任何资源都被当作文件来管理.如目录.光驱.终端设备等等,都被当作是一种文件.从这方面来说,Unix操作系统中的所有的目录 ...
- Javascript对checkbox勾选判断,错误提示和按钮变色操作
同意相关条款未打钩时,登录按钮为灰色且无法提交,点击灰色的登录按钮提示同意相关条款,打钩后变成亮色且可以提交信息. 勾选框及文字: <div class="check-rule&quo ...
- trizip haskell implementation
1 trizip :: [a] -> [b] -> [c] -> [(a,b,c)] 2 trizip a b c 3 | null a = [] 4 | null b = [] 5 ...