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开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿童欢迎. ...
随机推荐
- HTTP协议缓存
缓存的概念 缓存这个东西真的是无处不在, 有浏览器端的缓存, 有服务器端的缓存,有代理服务器的缓存, 有ASP.NET页面缓存,对象缓存. 数据库也有缓存, 等等. http中具有缓存功能的是浏览器缓 ...
- leetcode-15双周赛-1287-有序数组中出现次数超过25%的元素
题目描述: 方法一:二分法 class Solution: def findSpecialInteger(self, arr: List[int]) -> int: span = len(arr ...
- PHP array_change_key_case() 函数
实例 将数组的所有的键转换为大写字母: <?php $age=array("Peter"=>"35","Ben"=>&qu ...
- BZOJ 4883 棋盘上的守卫 解题报告
BZOJ4883 棋盘上的守卫 考虑费用流,但是数据范围太大 考虑 \(i\) 行 \(j\) 列如果被选择,那么要么给 \(i\) 行,要么给 \(j\) 列 把选择 \(i\) 行 \(j\) 列 ...
- linux 正则表达式与实践
正则表达式基础 准备 (1)alias grep='grep --color=auto' 易于显示 (2)LC_ALL=C,字符集,设置环境变量,字符顺序 基础正则 1)^word 匹配以Word开 ...
- python中的encode()和decode()函数
前言: 我们知道,计算机是以二进制为单位的,也就是说计算机只识别0和1,也就是我们平时在电脑上看到的文字,只有先变成0和1,计算机才会识别它的意思.这种数据和二进制的转换规则就是编码.计算机的发展中, ...
- 建站手册-网站建设:Web 安全
ylbtech-建站手册-网站建设:Web 安全 1.返回顶部 1. http://www.w3school.com.cn/site/site_security.asp 2. 2.返回顶部 1. 此刻 ...
- Bash Shell中命令行选项/参数处理
0.引言 写程序的时候经常要处理命令行参数,本文描述在Bash下的命令行处理方式. 选项与参数: 如下一个命令行: ./test.sh -f config.conf -v --prefix=/home ...
- 我的WordPress站点
读取VDI文件 SSL和TLS Windows下使用vim的最佳方案:Sublime gdb用法 VMware的Guest与Host进行通信的三种方式 加密与解密 漫谈保护模式 processing学 ...
- shell cp拷贝的用法
个人觉得这个记录的比较全 自己查阅: cp [options] <source file or directory> <target file or directory> 或 ...