当前位置:网站首页>Leetcode 1275. 找出井字棋的獲勝者
Leetcode 1275. 找出井字棋的獲勝者
2022-07-19 15:01:00 【我不是蕭海哇~~~~】

A 和 B 在一個 3 x 3 的網格上玩井字棋。
井字棋遊戲的規則如下:
- 玩家輪流將棋子放在空方格 (" ") 上。
- 第一個玩家 A 總是用 “X” 作為棋子,而第二個玩家 B 總是用 “O” 作為棋子。
- “X” 和 “O” 只能放在空方格中,而不能放在已經被占用的方格上。
- 只要有 3 個相同的(非空)棋子排成一條直線(行、列、對角線)時,遊戲結束。
- 如果所有方塊都放滿棋子(不為空),遊戲也會結束。
- 遊戲結束後,棋子無法再進行任何移動。
給你一個數組 moves,其中每個元素是大小為 2 的另一個數組(元素分別對應網格的行和列),它按照 A 和 B 的行動順序(先 A 後 B)記錄了兩人各自的棋子比特置。
如果遊戲存在獲勝者(A 或 B),就返回該遊戲的獲勝者;如果遊戲以平局結束,則返回 “Draw”;如果仍會有行動(遊戲未結束),則返回 “Pending”。
你可以假設 moves 都 有效(遵循井字棋規則),網格最初是空的,A 將先行動。
示例 1:
輸入:moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
輸出:"A"
解釋:"A" 獲勝,他總是先走。
"X " "X " "X " "X " "X "
" " -> " " -> " X " -> " X " -> " X "
" " "O " "O " "OO " "OOX"
示例 2:
輸入:moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
輸出:"B"
解釋:"B" 獲勝。
"X " "X " "XX " "XXO" "XXO" "XXO"
" " -> " O " -> " O " -> " O " -> "XO " -> "XO "
" " " " " " " " " " "O "
示例 3:
輸入:moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
輸出:"Draw"
輸出:由於沒有辦法再行動,遊戲以平局結束。
"XXO"
"OOX"
"XOX"
示例 4:
輸入:moves = [[0,0],[1,1]]
輸出:"Pending"
解釋:遊戲還沒有結束。
"X "
" O "
" "
提示:
- 1 <= moves.length <= 9
- moves[i].length == 2
- 0 <= moves[i][j] <= 2
- moves 裏沒有重複的元素。
- moves 遵循井字棋的規則。
Code:
class Solution {
public:
string tictactoe(vector<vector<int>>& moves) {
vector<vector<int>> newOne(3, vector<int>(3, -1));
for(int i=0;i<moves.size();i++)
{
vector<int>sub=moves[i];
if(i%2)
{
newOne[sub[0]][sub[1]]=0;
}
else
{
newOne[sub[0]][sub[1]]=1;
}
}
//行判斷
int cnt=0;
for(int i=0;i<3;i++)
{
vector<int>sub=newOne[i];
cnt=count(sub.begin(),sub.end(),1);
if(cnt==3)
return "A";
cnt=count(sub.begin(),sub.end(),0);
if(cnt==3)
return "B";
}
//列判斷
bool left=false;
for(int i=0;i<3;i++)
{
int cnt1=0;
int cnt2=0;
for(int j=0;j<3;j++)
{
if(newOne[j][i]==1)
{
cnt1++;
}
else if(newOne[j][i]==0)
{
cnt2++;
}
else
left=true;
}
if(cnt1==3)
return "A";
if(cnt2==3)
return "B";
}
if(newOne[0][0]==1 && newOne[1][1]==1 && newOne[2][2]==1)
{
return "A";
}
if(newOne[0][2]==1 && newOne[1][1]==1 && newOne[2][0]==1)
return "A";
if(newOne[0][0]==0 && newOne[1][1]==0 && newOne[2][2]==0)
{
return "B";
}
if(newOne[0][2]==0 && newOne[1][1]==0 && newOne[2][0]==0)
return "B";
if(left)
return "Pending";
else
return "Draw";
return "";
}
};
边栏推荐
猜你喜欢
![[flask introduction series] exception handling](/img/88/7891be60098ab9b41a3c7e0f8cf9fc.png)
[flask introduction series] exception handling

ICML2022 | 几何多模态对比表示学习

ORA-00054

The bill module of freeswitch

国科大.深度学习.期末复习知识点总结笔记

How to quickly realize Zadig single sign on on authoring?

Sliding window maximum problem

Zabbix实现对Redis的监控

Leetcode 1296. 划分数组为连续数字的集合(已解决)

Deep understanding of transaction isolation levels
随机推荐
Can [C language - user defined type] be adjusted like this?
程序员 运维那些高薪的背后
Data consistency between redis and MySQL
2、MYSQL介绍
Is it safe to buy funds in a securities account? I want to make a fixed investment in the fund
Pyside2 drawing embedded in Matplotlib
Single channel 6Gsps 12 bit AD acquisition and single channel 6Gsps 16 bit Da (ad9176) output sub card based on vita57.1 standard
分布式事务总结
1、DBMS基本概念
session管理
Damn it, why is there less space on the USB flash drive? It's the EFI partition. Delete it
UCAS. Deep learning Final examination questions and brief thinking analysis
Which company is better in data filling and report presentation? Yixin ABI gives you the answer
国科大. 深度学习. 期末试题与简要思路分析
Cilium & Hubble
Classification of blocks
Gradle introduction notes
Alibaba microservice component Nacos registry
【微服务】 微服务学习笔记三:利用Feign替换RestTemplate完成远程调用
[mqtt from getting started to improving series | 06] subscribe subscription workflow of mqtt3.1.1