新闻详情

新闻详情

首页 / 资讯中心 / 详情

LeetCode //C - 1275. Find Winner on a Tic Tac Toe Game

发布时间:2026/10/1 2:32:25来源:尧图网络
LeetCode //C - 1275. Find Winner on a Tic Tac Toe Game
1275. Find Winner on a Tic Tac Toe GameTic-tac-toe is played by two players A and B on a 3 x 3 grid. The rules of Tic-Tac-Toe are:Players take turns placing characters into empty squares ’ .The first player A always places ‘X’ characters, while the second player B always places ‘O’ characters.‘X’ and ‘O’ characters are always placed into empty squares, never on filled ones.The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal.The game also ends if all squares are non-empty.No more moves can be played if the game is over.Given a 2D integer array moves wherem o v e s [ i ] [ r o w i , c o l i ] moves[i] [row_i, col_i]moves[i][rowi​,coli​]indicates that thei t h i^{th}ithmove will be played ong r i d [ r o w i ] [ c o l i ] grid[row_i][col_i]grid[rowi​][coli​]. return the winner of the game if it exists (A or B). In case the game ends in a draw return “Draw”. If there are still movements to play return “Pending”.You can assume that moves is valid (i.e., it follows the rules of Tic-Tac-Toe), the grid is initially empty, and A will play first.Example 1:Input:moves [[0,0],[2,0],[1,1],[2,1],[2,2]]Output:“A”Explanation:A wins, they always play first.Example 2:Input:moves [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]Output:“B”Explanation:TB wins.Example 3:Input:moves [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]Output:“Draw”Explanation:The game ends in a draw since there are no moves to make.Constraints:1 moves.length 9moves[i].length 20 r o w i , c o l i 2 0 row_i, col_i 20rowi​,coli​2There are no repeated elements on moves.moves follow the rules of tic tac toe.From: LeetCodeLink: 1275. Find Winner on a Tic Tac Toe GameSolution:Ideas:put A as 1, B as -1.If any row, column, or diagonal sums to 3, A wins.If it sums to -3, B wins.Code:char*tictactoe(int**moves,intmovesSize,int*movesColSize){intboard[3][3]{0};// A 1, B -1for(inti0;imovesSize;i){intrmoves[i][0];intcmoves[i][1];board[r][c](i%20)?1:-1;}for(inti0;i3;i){introwboard[i][0]board[i][1]board[i][2];intcolboard[0][i]board[1][i]board[2][i];if(row3||col3)returnA;if(row-3||col-3)returnB;}intdiag1board[0][0]board[1][1]board[2][2];intdiag2board[0][2]board[1][1]board[2][0];if(diag13||diag23)returnA;if(diag1-3||diag2-3)returnB;if(movesSize9)returnDraw;returnPending;}
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

cpp-httplib 客户端超时配置完全指南:连接、读取与写入超时(C12) 2026/10/2 0:13:43

cpp-httplib 客户端超时配置完全指南:连接、读取与写入超时(C12)

后端网络 【免费下载链接】cpp-httplib A C header-only HTTP/HTTPS server and client library 项目地址: https://gitcode.com/GitHub_Trending/cp/cpp-httplib 点击查看 免费下载 导读 本指南围绕 cpp-httplib 客户端的三类超时(连接超时、读取超时…

阅读更多 →
devops-exercises 实战:用 Bash 函数与正则校验编写两数求和脚本 2026/10/2 0:13:09

devops-exercises 实战:用 Bash 函数与正则校验编写两数求和脚本

文档教程DevOps运维 【免费下载链接】devops-exercises Linux, Jenkins, AWS, SRE, Prometheus, Docker, Python, Ansible, Git, Kubernetes, Terraform, OpenStack, SQL, NoSQL, Azure, GCP, DNS, Elastic, Network, Virtualization. DevOps Interview Questions 项目地址&…

阅读更多 →
Jupyter Lab密码登录与远程访问安全配置指南 2026/10/2 0:12:55

Jupyter Lab密码登录与远程访问安全配置指南

1. 项目概述:为什么非得让 Jupyter Lab 支持密码登录和远程访问?Jupyter Lab 不是玩具,它是数据科学、机器学习、教学实验和工程验证的真实工作台。但默认安装后,它只在本地http://localhost:8888启动,连本机其他用户都…

阅读更多 →
CentOS 8 安装 GCC 全攻略:在线/离线/源码编译与避坑指南 2026/10/2 0:12:48

CentOS 8 安装 GCC 全攻略:在线/离线/源码编译与避坑指南

CentOS 8 安装 gcc,这话题看着简单,实际操作起来坑不少。尤其 CentOS 8 官方仓库停止维护之后,默认源都迁移到了 vault 地址,你要是直接跑一句yum install gcc -y,十有八九会撞上Failed to download metadata for repo…

阅读更多 →
双渠道闭环供应链跨渠道退货定价:Stackelberg与Nash均衡求解 2026/10/2 0:12:48

双渠道闭环供应链跨渠道退货定价:Stackelberg与Nash均衡求解

简介:一份面向供应链管理研究人员、高校物流相关专业师生及双渠道销售企业管理者的完整PDF资源,聚焦考虑跨渠道退货的双渠道闭环供应链决策优化。内容系统整合Stackelberg博弈与Nash均衡模型,对比集中式、制造商主导、零售商主导及Nash均衡结…

阅读更多 →
ESXi 6.7物理服务器启动盘制作全指南 2026/10/2 0:12:48

ESXi 6.7物理服务器启动盘制作全指南

1. 这不是普通装系统,是给物理服务器“打底”的关键一步你手头有一台闲置的旧服务器、一台二手Dell R720、或者刚淘来的HP ProLiant DL360,想把它变成一个稳定跑虚拟机的私有云平台——这时候,ESXi 6.7 就成了最务实的选择。它轻量、高效、资…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉