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;
}

猜你喜欢

转载自www.cnblogs.com/SilverChen/p/9567519.html