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开发的第二款小游戏.此游戏是大家想必大家小时候都玩过,因为玩它很简单,只需要一张草稿纸和一只笔就能开始游戏,所以广受儿童欢迎. ...
随机推荐
- leetcode-167周赛-1293-网格中的最短路径
题目描述: 自己的提交:广度优先 O(mn*min(k,m+n)) class Solution: def shortestPath(self, grid, k: int) -> int: vi ...
- 透明的UITableView
// // ViewController.m // 透明table // // Created by LiuWei on 2018/4/23. // Copyright © 2018年 xxx. Al ...
- spring boot jar的支持
- PWN入门的入门——工具安装
安装pwntool: 命令行运行: pip install pwntools python import pwn pwn.asm("xor eax,eax") 出现'1\xc0' ...
- 从xxxx检测到有潜在危险的 Request.Form 提示黄页
相信很多朋友都遇到过"从客户端xxxxxx"检测到有潜在危险的 Request.Form 然后给一个黄页提示.然后检测代码又找不到错误的代码提示. 原因:是因为在页面里边使用了富文 ...
- jenkins-参数化构建插件:Choice Parameter
参考: 谢谢大佬的总结: https://www.cnblogs.com/zhaojingyu/p/9862371.html 使用方式 step1: 添加参数,选择Choice Parameter,并 ...
- cnn模型
https://blog.csdn.net/qq_26591517/article/details/79805884
- iview+vue 表格中添加图片
开门见山,话不多说,要在表格中添加图片,可以使用td: <table " width="100%"> <tr class="tr-style ...
- 简单DP入门(二) 最长上升子序列及其优化
最长上升子序列解决问题: 有N个数,求出它最长的上升子序列并输出长度. 在题里不会讲的这么直白,这个算法往往会与其他的算法混在一起使用. 在这篇文章中不会出现其他的例题,为了让大家更好的理解,我只会对 ...
- md5加密 和拉钩网的登录
#使用requests模块 #1.登录lagou #2.登录人人,保存个人首页 import requests from urllib import parse #hashlib是MD5加密的一个py ...