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

【题意】

题意

【题解】

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

【代码】

#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. mybatis 注解写法 多层嵌套foreach,调用存储过程,批量插入数据

    @Select("<script>" + "DECLARE @edi_Invoice_Details edi_Invoice_Details;" + ...

  2. 服务器出现 nginx 502 Bad Gateway

    发生原因1.PHP FastCGI进程数不够用 当网站并发访问巨大时,php fastcgi的进程数不有一定的保障,因为cgi是单线程多进程工作的,也就是说cgi需要处理完一个页面后再继续下一个页面. ...

  3. 数据结构之链式队列(C实现)

    1.1  linkqueue.h #ifndef LINKQUEUE_H #define LINKQUEUE_H #include <stdio.h> #include <mallo ...

  4. Codefoces 828C

    C. String Reconstruction time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. 贪心+优先队列 HDOJ 5360 Hiking

    题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...

  6. BFS HDOJ 1728 逃离迷宫

    题目传送门 /* BFS:三维BFS,加上方向.用dp[x][y][d]记录当前需要的最少转向数 */ #include <cstdio> #include <algorithm&g ...

  7. leetcode650 2 Keys Keyboard

    思路: 把给定的数分解质因子之后,对于每一个质因子x,都需要x次操作(一次copy all操作和x-1次paste),所以答案就是对分解后的所有质因子求和. 实现: class Solution { ...

  8. 2017-12-01HTML块及引用

    HTML块1.HTML块元素 快元素在显示时,通常会以新行开始 例如:<h1>.<p>.<ul>2.HTML内联元素 内联元素通常不会以新行开始 例如:<b& ...

  9. SugarCRM安装过程——PHP文件上传限制问题

    找到D:\xampp\php目录下,php文件中的php.ini文件,用写字板打开: 1.查找post_max_size,指通过表单POST给PHP的所能接收的最大值,包括表单里的所有值,默认为8M, ...

  10. js文件中引用其他js文件

    这一个功能的作用是做自己的js包时,可以通过引入一个整体的js文件而引入其他js. 只需要在总体的js加上这一句话 document.write("<script type='text ...