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开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿童欢迎. ...
随机推荐
- Linux中的touch命令总结(一)
touch命令有两个主要功能: 改变 timestamps 新建_空白_文件 例如,不带任何参数地输入: touch file1 file2 file3 将在当前目录下新建三个空白文件:file1, ...
- 英语单词Uninterrupted
Uninterrupted 来源——不间断电源供应 UPS(Uninterrupted Power Supply) 翻译 adj. 不间断的:连续的 GRE 词根 un- + interrupt ...
- BLUEHOST香港主机FTP连接不上解决办法
昨天购买了BLUEHOST的香港主机后,以为一切顺风顺水,结果FTP却连接不上,用了多种FTP工具都不行,按官方博客要求开启了TSL仍然不行.经过一晚上的测试后终于成功,现分享出来. 方法一 ...
- Security基础(三):OpenSSL及证书服务、邮件TLS/SSL加密通信
一.OpenSSL及证书服务 目标: 本案例要求熟悉OpenSSL工具的基本使用,完成以下任务操作: 使用OpenSSL加密/解密文件 搭建企业自有的CA服务器,为颁发数字证书提供基础环境 方案: 使 ...
- LOJ 6436 「PKUSC2018」神仙的游戏——思路+卷积
题目:https://loj.ac/problem/6436 看题解才会. 有长为 i 的 border ,就是有长为 n-i 的循环节. 考虑如果 x 位置上是 0 . y 位置上是 1 ,那么长度 ...
- codeforces 111A/112C Petya and Inequiations
题目:Petya and Inequiations传送门: http://codeforces.com/problemset/problem/111/A http://codeforces.com/p ...
- vue-cli2.X之simple项目搭建过程
1.vue init webpack-simple vuedemo02 2.按提示操作 3. 项目目录: ps:可能遇到的问题
- _new__和__init__的区别
__new__是Python面向对象语言中一个很少用的函数,更多使用的是__init__这个函数.例如: class Book(object): def __init__(self, title): ...
- Intel processor brand names-Xeon,Core,Pentium,Celeron----Xeon
http://en.wikipedia.org/wiki/Comparison_of_Intel_processors Processor Series Nomenclature Code Name ...
- 深入浅出HashMap
/** *@ author ViVi *@date 2014-6-11 */ Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.希望通过仪器讨论.共同提高~ 1 ...