zoukankan      html  css  js  c++  java
  • 【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;
    }
    
    
  • 相关阅读:
    ORA-65114
    Mariadb 10.14 mysqldump error: 1049
    nginx:403 forbidden
    ORA-01017
    oracle 12C 之 Clone 数据库
    Selinux的基本使用
    This system is not registered to Red Hat Subscription Management
    Emacs: too long for unix domain socket
    hive 之 元数据表结构(Mysql)
    hive之SerDe
  • 原文地址:https://www.cnblogs.com/AWCXV/p/10642990.html
Copyright © 2011-2022 走看看