【codeforces 3C】Tic-tac-toe

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


题意

【题解】


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

【代码】

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值