获胜者(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)
发布评论