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 导出JSON
cnpm install file-saver --save <template> <div class="hello"> <button @clic ...
- Java基本数据类型及所占字节大小
一.Java基本数据类型 基本数据类型有8种:byte.short.int.long.float.double.boolean.char 分为4类:整数型.浮点型.布尔型.字符型. 整数型:byte. ...
- Ubuntu Visual code安装与使用
1.直接启动软件中心,输入visual studio code,点击install即可,千万千万不要去装逼搞什么linux指令安装,死都不知道怎么死的 2.Visual code是以文件夹为工程目录的 ...
- CF 717A Festival Organization——斯特林数+递推求通项+扩域
题目:http://codeforces.com/contest/717/problem/A 是 BJOI2019 勘破神机 的弱化版. 令 \( g[i] \) 表示长为 i .以 1 结尾的方案数 ...
- js中的$符
js中的$代表什么意思呢? 首先js的作用是什么呢?是修饰网页动态内容的.那么修饰就需要定位主题,比如你把html比喻一个美女,让她唱一首歌.那么首先你要定位出是你想让哪个美女唱歌,通常我们用id来定 ...
- 2018-2019-20175203 实验二 《Java面向对象程序设计》
2018-2019-20175203 实验二 <Java面向对象程序设计>实验报告 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑 ...
- DomainObjectUtility
using System; using System.Collections; using System.Collections.Generic; using System.Collections.S ...
- Asp.Net Core 第04局:依赖注入
总目录 前言 本文介绍Asp.Net Core中默认的依赖注入(DI)模式. 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 第一手:依赖注入说明 1.一个 ...
- [题解]Print a 1337-string...-数学(codeforces 1202D)
题目链接:https://codeforces.com/problemset/problem/1202/D 题意: 构造一串只由 ‘1’,‘3’,‘7’ 组成的字符串,使其 ‘1337’ 子序列数量为 ...
- VS2012修改代码时会把后面的覆盖
vs2012修改代码时会把后面的覆盖,并且鼠标指针变成灰色竖方块 解决:按一下键盘上的Insert键