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

【题意】

题意

【题解】

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

【代码】

#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. vue学习笔记(1)

    1.检测变化 <ul> <li v-for="item in list">{{item}}</li> </ul> <scrip ...

  2. bzoj 1753: [Usaco2005 qua]Who's in the Middle【排序】

    --这可能是早年Pascal盛行的时候考排序的吧居然还是Glod-- #include<iostream> #include<cstdio> #include<algor ...

  3. Nginx(一) 安装基于centos7

    1.   nginx介绍 1.1. 什么是nginx Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.由俄罗斯的程序设计师Igor Sysoev所开 ...

  4. Golang 入门 : 竞争条件

    笔者在前文<Golang 入门 : 理解并发与并行>和<Golang 入门 : goroutine(协程)>中介绍了 Golang 对并发的原生支持以及 goroutine 的 ...

  5. JS 封装插件

    媒体播放器插件: mediaelement-and-player.js 轮播图插件: swiper.min.js

  6. DHTML_____如何编写事件处理程序

    <html> <head> <meta charset="utf-8"> <title>如何编写事件处理程序</title&g ...

  7. js解析地址栏参数

    /** * 获取地址栏中url后面拼接的参数 * eg: * 浏览器地址栏中的地址:http://1.1.1.1/test.html?owner=2db08226-e2fa-426c-91a1-66e ...

  8. linux下jdk与tomcat的安装与配置

    Linux中jdk与tomcat的安装与配置 1.搭建环境: (1)Linux环境:CentOS6.1 (2)jdk-1.8 (3)tomcat-9.0 2.在Linux系统上创建目录 在usr/lo ...

  9. Android基础TOP6_2:Gallery +Image完成画廊

    Activity: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...

  10. 使用morphia实现对mongodb的聚合查询

    morphia是谷歌的一个针对mongodb的数据化持久框架: 关于mongodb的介绍不在这里展示,直接进入主题:采用morphia实现对mongodb的聚合查询 这里获取所有学生的分数总和 spr ...