[数组]leetcode1275:找出井字棋的获胜者(easy)

编程入门 行业动态 更新时间:2024-10-23 17:26:57

[数组]leetcode1275:找出井字棋的<a href=https://www.elefans.com/category/jswz/34/1193711.html style=获胜者(easy)"/>

[数组]leetcode1275:找出井字棋的获胜者(easy)

题目:

1275. 找出井字棋的获胜者

题解:

  • 思路:暴力枚举(说实话我是笨蛋,再打比赛的时候这种小数据的枚举题都没做好,我是笨蛋)
  • 获胜的方式也就总共8种:三行、三列、两条对角线。
  • 然后把moves当成一维数组使用,注意二维数组与一维数组的转换(每行的个数*i+j)

代码如下:

class Solution {
public:string tictactoe(vector<vector<int>>& moves) {vector<int> playerA(9,0),playerB(9,0);for(int i=0;i<moves.size();++i){if(i%2==0){playerA[moves[i][0]*3+moves[i][1]]=1;if(check(playerA))return "A";}else{playerB[moves[i][0]*3+moves[i][1]]=1;if(check(playerB))return "B";}}if(moves.size()>=9)return "Draw";//平局else return "Pending";//游戏还没结束}bool check(vector<int>& grid){//判断行for(int i=0;i<3;++i){if(grid[i*3]&&grid[i*3+1]&&grid[i*3+2])return true;}//判断列for(int i=0;i<3;++i){if(grid[i]&&grid[i+3]&&grid[i+6])return true;}//判断对角线if(grid[0]&&grid[4]&&grid[8])return true;if(grid[2]&&grid[4]&&grid[6])return true;return false;}
};

更多推荐

[数组]leetcode1275:找出井字棋的获胜者(easy)

本文发布于:2023-07-28 18:53:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279908.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:获胜者   数组   井字棋   easy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!