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

【题意】

题意

【题解】

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

【代码】

#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 启动服务和登陆参数

    启动MySQL服务:net start mysql; 停止MySQL服务:net stop mysql; 参数 描述 -D,--database=name 打开指定数据库 --delimiter=na ...

  2. bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声【单调栈】

    先考虑只能往一边传播,最后正反两边就行 一向右传播为例,一头牛能听到的嚎叫是他左边的牛中与高度严格小于他并且和他之间没有更高的牛,用单调递减的栈维护即可 #include<iostream> ...

  3. 151. [USACO Dec07] 建造路径

    ★★   输入文件:roads.in   输出文件:roads.out   简单对比 时间限制:1 s   内存限制:128 MB 译 by CmYkRgB123 描述 Farmer John 刚刚得 ...

  4. Python+selenium测试环境成功搭建,简单控制浏览器(firefox)接下来,继续学习其他浏览器上的测试环境搭建;学习Python语言,利用Python语言来写测试用例。加油!!!

    Python+selenium测试环境成功搭建,简单控制浏览器(firefox)接下来,继续学习其他浏览器上的测试环境搭建:学习Python语言,利用Python语言来写测试用例.加油!!!

  5. 联想 Z5S(L78071)免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖 ZUI 10.5.370

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  6. 【PostgreSQL-9.6.3】LOG: unrecognized configuration parameter "dynamic_shared_memory_type"

    报错如下: 输入如下命令启动PG数据库时,报错: [postgres@drz ~]$ pg_ctl -D /opt/postgresql/data/ start server starting FAT ...

  7. eclipse中添加maven

    收藏一下,一篇很好的例子 maven相关插件:链接:http://pan.baidu.com/s/1i3Ks95j 密码:7pgh eclipse:链接:http://pan.baidu.com/s/ ...

  8. Windows提高_1.2遍历进程、遍历模块

    进程 什么是进程? 通俗的来讲,进程就是一个运行中的程序,最少包含一个虚拟空间,通常是 4 GB大小,一组提供数据和代码的模块,通产是 dll 和 exe 文件,一个进程内核对象和最少一个线程. 进程 ...

  9. Ceres

    sudo apt-get install liblapack-dev libsuitesparse-dev libcxspares3.1.2 libgflags-dev libggoogle-glog ...

  10. ansible结合playbook批量部署war包项目上线

    批量部署jenkins.war包实现上线 用于测试war包上线 [root~localhost]~#vim /etc/ansible/test.yml - hosts: test vars:     ...