题解:CF71D Solitaire

· · 题解

思路

按题意进行模拟即可。

分 3 类讨论:原始矩形中有两个王、一个王、没有王的情况。

基于分类讨论,枚举用于替换王的牌,再枚举矩形中所有 3 \times 3 的正方形,判断是否满足,再按题目要求输出就可以了。

AC Code

#include <bits/stdc++.h>
#define i64 long long
#define u64 unsigned long long
#define i128 __int128
#define u128 unsigned __int128
#define db double
#define pq priority_queue
#define mod 998244353
#define pb1 push_back
#define pb2 pop_back
#define inf 1073741823
#define INF 4611686018427387903
#define all(x) x.begin(), x.end()
using namespace std;

int n, m, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
set <string> c;
string s[20][20];

inline bool check (int x, int y) {
    map <char, bool> mp;
    for (int i = x; i < x + 3; i++)
        for (int j = y; j < y + 3; j++)
            mp[s[i][j][0]] = true;
    if ((int)(mp.size()) == 9)
        return true;
    mp.clear();
    for (int i = x; i < x + 3; i++)
        for (int j = y; j < y + 3; j++)
            mp[s[i][j][1]] = true;
    if ((int)(mp.size()) == 1)
        return true;
    return false;
} // 判断以 (x, y) 为左上角的 3 * 3 正方形满不满足条件

inline void calc1 (bool &flag, const string &str1, const string &str2) {
    for (int i1 = 1; i1 <= n - 2; i1++)
        for (int j1 = 1; j1 <= m - 2; j1++)
            if (check(i1, j1)) // 枚举并判断第一个 3 * 3 正方形
                for (int i2 = i1; i2 <= n - 2; i2++)
                    for (int j2 = (i2 == i1 ? j1 + 3 : 1); j2 <= m - 2; j2++)
                        if ((j2 > j1 + 2 || i2 > i1 + 2) && check(i2, j2)) {  // 枚举并判断第二个 3 * 3 正方形,并判断第一个和第二个正方形是否重叠
                            cout << "Solution exists.\nReplace J1 with " << str1 << " and J2 with " << str2 << ".\nPut the first square to (" << i1 << ", " << j1 << ").\nPut the second square to (" << i2 << ", " << j2 << ").";
                            flag = true;
                            return;
                        }
} // 其他 calc 类函数类似于 calc1,只是不同于输出

inline void calc2 (bool &flag, const string &str) {
    for (int i1 = 1; i1 <= n - 2; i1++)
        for (int j1 = 1; j1 <= m - 2; j1++)
            if (check(i1, j1))
                for (int i2 = i1; i2 <= n - 2; i2++)
                    for (int j2 = (i2 == i1 ? j1 + 3 : 1); j2 <= m - 2; j2++)
                        if ((j2 > j1 + 2 || i2 > i1 + 2) && check(i2, j2)) {
                            cout << "Solution exists.\nReplace J1 with " << str << ".\nPut the first square to (" << i1 << ", " << j1 << ").\nPut the second square to (" << i2 << ", " << j2 << ").";
                            flag = true;
                            return;
                        }
}

inline void calc3 (bool &flag, const string &str) {
    for (int i1 = 1; i1 <= n - 2; i1++)
        for (int j1 = 1; j1 <= m - 2; j1++)
            if (check(i1, j1))
                for (int i2 = i1; i2 <= n - 2; i2++)
                    for (int j2 = (i2 == i1 ? j1 + 3 : 1); j2 <= m - 2; j2++)
                        if ((j2 > j1 + 2 || i2 > i1 + 2) && check(i2, j2)) {
                            cout << "Solution exists.\nReplace J2 with " << str << ".\nPut the first square to (" << i1 << ", " << j1 << ").\nPut the second square to (" << i2 << ", " << j2 << ").";
                            flag = true;
                            return;
                        }
}

inline void calc4 (bool &flag) {
    for (int i1 = 1; i1 <= n - 2; i1++)
        for (int j1 = 1; j1 <= m - 2; j1++)
            if (check(i1, j1))
                for (int i2 = i1; i2 <= n - 2; i2++)
                    for (int j2 = (i2 == i1 ? j1 + 3 : 1); j2 <= m - 2; j2++)
                        if ((j2 > j1 + 2 || i2 > i1 + 2) && check(i2, j2)) {
                            cout << "Solution exists.\nThere are no jokers.\nPut the first square to (" << i1 << ", " << j1 << ").\nPut the second square to (" << i2 << ", " << j2 << ").";
                            flag = true;
                            return;
                        }
}

int main () {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    cin >> n >> m;
    c.insert("AC");
    c.insert("AD");
    c.insert("AH");
    c.insert("AS");
    c.insert("2C");
    c.insert("2D");
    c.insert("2H");
    c.insert("2S");
    c.insert("3C");
    c.insert("3D");
    c.insert("3H");
    c.insert("3S");
    c.insert("4C");
    c.insert("4D");
    c.insert("4H");
    c.insert("4S");
    c.insert("5C");
    c.insert("5D");
    c.insert("5H");
    c.insert("5S");
    c.insert("6C");
    c.insert("6D");
    c.insert("6H");
    c.insert("6S");
    c.insert("7C");
    c.insert("7D");
    c.insert("7H");
    c.insert("7S");
    c.insert("8C");
    c.insert("8D");
    c.insert("8H");
    c.insert("8S");
    c.insert("9C");
    c.insert("9D");
    c.insert("9H");
    c.insert("9S");
    c.insert("TC");
    c.insert("TD");
    c.insert("TH");
    c.insert("TS");
    c.insert("JC");
    c.insert("JD");
    c.insert("JH");
    c.insert("JS");
    c.insert("QC");
    c.insert("QD");
    c.insert("QH");
    c.insert("QS");
    c.insert("KC");
    c.insert("KD");
    c.insert("KH");
    c.insert("KS");
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            cin >> s[i][j];
            if (s[i][j][0] == 'J' && abs(s[i][j][1] - '1') <= 1)
                if (s[i][j][1] == '1')
                    x1 = i, y1 = j;
                else
                    x2 = i, y2 = j;
            else
                c.erase(s[i][j]);
        } // 获取大小王的位置
    if ((x1 > 0 && y1 > 0) && (x2 > 0 && y2 > 0)) {
        bool flag = false;
        for (auto str1 : c)
            if (!flag)
                for (auto str2 : c)
                    if (!flag && str1 != str2)
                        s[x1][y1] = str1, s[x2][y2] = str2, calc1(flag, str1, str2);
        if (!flag)
            cout << "No solution.";
    } // 两个王的情况
    else
        if (x1 > 0 && y1 > 0) {
            bool flag = false;
            for (auto str : c)
                if (!flag)
                    s[x1][y1] = str, calc2(flag, str);
            if (!flag)
                cout << "No solution.";
        } // 一个王的情况J1
        else
            if (x2 > 0 && y2 > 0) {
                bool flag = false;
                for (auto str : c)
                    if (!flag)
                        s[x2][y2] = str, calc3(flag, str);
                if (!flag)
                    cout << "No solution.";
            } // 一个王的情况J2
            else {
                bool flag = false;
                calc4(flag);
                if (!flag)
                    cout << "No solution.";
            } // 没有王的情况
    return 0;
}