【链接】 我是链接,点我呀:)

【题意】

题意

【题解】

写一个函数判断当前局面是否有人赢。
然后枚举上一个人的棋子下在哪个地方。
然后把他撤回
看看撤回前是不是没人赢然后没撤回之前是不是有人赢了。
如果是的话
那么就是满足要求的啦吸吸吸

【代码】

#include <bits/stdc++.h>
#define ll long long
using namespace std; const ll MOD = 998244353; string s[10];
string ans[]={"zero","first","second"};
int a[5][5];
int cnt[5],nex; //判断正对角线
//如果是true,返回第一个人赢还是第二个人赢
pair<bool,int> zheng(){
if (a[1][1]==a[2][2] && a[2][2]==a[3][3]){
if (a[1][1]==3){
return make_pair(false,3);
}else return make_pair(true,a[1][1]);
}else return make_pair(false,3);
} //反对角线
//如果是true,返回是谁
pair<bool,int> fan(){
if (a[1][3]==a[2][2] && a[2][2]==a[3][1]){
if (a[1][3]==3){
return make_pair(false,3);
}else return make_pair(true,a[1][3]);
}else return make_pair(false,3);
} //判断每一行,返回相等的行
vector<int> hang(){
vector<int> v;v.clear();
for (int i = 1;i <= 3;i++){
if (a[i][1]==a[i][2] && a[i][2]==a[i][3]){
if (a[i][1]==3) continue;
v.push_back(a[i][1]);
}
}
return v;
} //判断每一列,返回相等的列
vector<int> lie(){
vector<int> v;v.clear();
for (int i = 1;i <= 3;i++){
if (a[1][i]==a[2][i] && a[2][i]==a[3][i]){
if (a[1][i]==3) continue;
v.push_back(a[1][i]);
}
}
return v;
} //判断是否有人赢
bool somebodywin(){
if ((int)hang().size()>0) return true;
if ((int)lie().size()>0) return true;
if (zheng().first==true) return true;
if (fan().first==true) return true;
return false;
} //让x对应数字的标记变成空白,然后看看是不是没有人赢
int stepbackisok(int x){
for (int i = 1;i <= 3;i++)
for (int j = 1;j <= 3;j++)
if (a[i][j]==x){
a[i][j] = 3;
if (!somebodywin()) return true;
a[i][j] = x;
}
return false;
} int main(){
ios::sync_with_stdio(0),cin.tie(0);
for (int i = 1;i <= 3;i++){
cin >> s[i];
for (int j = 1;j <= 3;j++){
if (s[i][j-1]=='X'){
a[i][j] = 1;
}else if (s[i][j-1]=='0'){
a[i][j] = 2;
}else a[i][j] = 3;
cnt[a[i][j]]++;
}
}
if (cnt[1]==cnt[2]+1){
nex = 2;
}else if (cnt[1]==cnt[2]){
nex = 1;
}else{
cout<<"illegal"<<endl;
return 0;
}
if (cnt[1]+cnt[2]==9 && !somebodywin()){
cout<<"draw"<<endl;
return 0;
}
int last = 3-nex;
//把上一步去掉,没人赢,加上这一步上个人赢
if (somebodywin()){
//如果有人赢了
if (stepbackisok(last)){
cout<<"the "<<ans[last]<<" player won"<<endl;
}else{
cout<<"illegal"<<endl;
}
}else{
cout<<ans[nex]<<endl;
} return 0;
}

【codeforces 3C】Tic-tac-toe的更多相关文章

  1. 【leetcode】1275. Find Winner on a Tic Tac Toe Game

    题目如下: Tic-tac-toe is played by two players A and B on a 3 x 3 grid. Here are the rules of Tic-Tac-To ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy

    1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...

  4. POJ 2361 Tic Tac Toe

    题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...

  5. 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe

    题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...

  6. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  7. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  8. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  9. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

随机推荐

  1. 最短路径(Dijkstra算法)

    当用图结构来表示通信.交通等网络,权重代表距离或者成本,寻找最短路径就成为了一个重要的任务. 给定带权网络G=(V;E),源点s,对于其他所有顶点v,寻找s到v的最短路径,连接成一颗最短路径树.可以证 ...

  2. J20180116

    用度 总务 用度係 总务科(管理办公用品等) 型紙 设计图 プラント 工厂

  3. 【题解】TES-Intelligence Test

    [题解]\(TES-Intelligence\) \(Test\) 逼自己每天一道模拟题 传送:\(TES-Intelligence\) \(Test\) \([POI2010]\) \([P3500 ...

  4. Oracle随机选择一条记录SQL

    Oracle随机选择一条记录SQL:

  5. 使用_CRTDBG_LEAK_CHECK_DF检查VC程序的内存泄漏(转)

    我们知道,MFC程序如果检测到存在内存泄漏,退出程序的时候会在调试窗口提醒内存泄漏.例如: class CMyApp : public CWinApp{public:BOOL InitApplicat ...

  6. oracle性能优化培训总结

  7. 用Martini、websocket实现单机版聊天室

    ChatRoom A stand-alone ChatRoom in Martini Please Star https://github.com/renleimlj/ChatRoom Interfa ...

  8. [ HAOI 2008 ] 圆上的整点

    \(\\\) Description 给出一个整数 \(r\) ,求圆 \(x^2+y^2=r^2\) 上的整点数. \(r\le 2\times 10^9\) \(\\\) Solution 神题. ...

  9. leetcode126 Word Ladder II

    思路: 宽搜过程中分层记录路径,递归还原.实现: class Solution { public: void getPath(string now, string beginWord, string ...

  10. opencv 检测图片中圆形物体(解决乱线问题)

    2018-03-0418:03:12 整体代码如下: def detect_circle_demo (image): # 降噪处理 dst = cv.pyrMeanShiftFiltering(ima ...