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

【题意】

题意

【题解】

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

【代码】

#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. mysql status关键字 数据表设计中慎重使用

    mysql status关键字  数据表设计中慎重使用

  2. hdu3018 Ant Trip (并查集+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题意:给你一个图,每条路只能走一次.问至少要多少个人才能遍历所有的点和所有的边. 这是之前没有接 ...

  3. Python基础 — NumPy

    NumPy--简介  Numpy(Numerical Python的简称)是一个由多维数组对象和用于处理数组的例程集合组成的库.  Numpy内部解除了Python的PIL(全局解释器锁),运算效率极 ...

  4. 思维题 UVA 10881 Piotr's Ants

    题目传送门 /* 题意:在坐标轴上一群蚂蚁向左或向右爬,问经过ts后,蚂蚁的位置和状态 思维题:本题的关键1:蚂蚁相撞看作是对穿过去,那么只要判断谁是谁就可以了 关键2:蚂蚁的相对位置不变 关键3:o ...

  5. 三分 HDOJ 3714 Error Curves

    题目传送门 /* 三分:凹(凸)函数求极值 */ #include <cstdio> #include <algorithm> #include <cstring> ...

  6. mybatis使用中类属性名和数据库表字段名问题

    起初我以为上述二者必须一致,后来发现也是可以像Hibernate那样在xml文件中进行映射的. <mapper namespace="com.tenghu.mybatis.model. ...

  7. Python :用两个栈实现队列

    转自:http://blog.csdn.net/Lynette_bb/article/details/75092745 牛客网上的剑指 offer的在线编程: 题目描述 用两个栈来实现一个队列,完成队 ...

  8. Sqoop hive 和mysql 交互 完整案例

    本文完成:在hive里建管理表:注入部分数据:利用sqoop导入mysql中 期间:解决中文乱码问题   飞行报告故障表 建表命令 查看表 人工灌入少量数据 Windows系统向Linux系统数据传输 ...

  9. centos如何离线安装部署node&pm2?

    最近我们项目要上即时通讯,因为项目对安全要求比较高,所以选择了即时通讯云服务器yun2win,他们提供了数据服务器让我们自己安装部署.那么问题来了,我们服务器是放在内网,完全无法访问外网,而yun2w ...

  10. Farseer.net轻量级开源框架 入门篇:修改数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 添加数据详解 下一篇:Farseer.net轻量级开源框架 入门篇: 删除数据详解 ...