CCF 201803-4

CCF 201803-4

考虑到题中说明,AB均采用最优策略,通过最大最小搜索完成。

#include<iostream>
using namespace std;
int pad[9];
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
int status() {
    for (int i = 0; i<3; i++) {
        if (pad[i * 3] != 0 && pad[i * 3] == pad[i * 3 + 1] && pad[i*3] == pad[i * 3 + 2]) {
            return pad[i * 3];
        }
        if (pad[i] != 0 && pad[i] == pad[i + 3] && pad[i] == pad[i + 6]) {
            return pad[i];
        }
    }
    if (pad[4] != 0 && pad[0] == pad[4] && pad[4] == pad[8]) {
        return pad[4];
    }
    if (pad[4] != 0 && pad[2] == pad[4] && pad[4] == pad[6]) {
        return pad[4];
    }
    return 0;
}
int minmax(int depth) {
    if (depth >9) {
        return 0;
    }
    int a = status();//current
    if (a != 0) {
        return (9 - depth + 1)*(a == 1 ? 1 : -1);
    }
    if (depth == 9) {
        return 0;
    }
    if (depth % 2 == 0) {//a.Going to choose the maxScore;
        int maxScore = -10;
        for (int i = 0; i<9; i++) {
            if (pad[i] == 0) {
                pad[i] = 1;
                maxScore = max(maxScore, minmax(depth + 1));
                pad[i] = 0;
            }
        }
        return maxScore;
    }
    else {//B . Going to get min;
        int minScore = 10;
        for (int i = 0; i<9; i++) {
            if (pad[i] == 0) {
                pad[i] = 2;
                minScore = min(minScore, minmax(depth + 1));
                pad[i] = 0;
            }
        }
        return minScore;
    }
}
int main() {
    int T;
    cin >> T;
    int depth = 0;
    while (T-->0) {
        depth = 0;
        for (int i = 0; i<9; i++) {
            cin >> pad[i];
            if (pad[i]) {
                depth++;
            }
        }
        if (depth == 0) {
            cout << 0 << endl;
        }
        else
        {
            cout << minmax(depth) << endl;
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/SilverChen/p/9567519.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值