【題解】TIOJ 1235 . 富翁種花

【題目敘述】https://tioj.ck.tp.edu.tw/problems/1235

#include <bits/stdc++.h>
using namespace std;

int g[9][9], r[9][9], c[9][9], b[3][3][9];
map <char, int> ci;
map <int, char> ic;
string s, str = "ROYGBIPLW";
vector <int> ans[9];

void f(int x, int y){
    if (x == 9){
        for (int i = 0; i < 9; i++){
            for (int j:ans[i]){
                cout << ic[j];
            }
            cout << "\n";
        }
        return;
    }
    if (g[x][y] != -1){
        if (y == 8) f(x+1, 0);
        else f(x, y+1);
    }
    else{
        for (int i = 0; i < 9; i++){
            if (!r[x][i] && !c[y][i] && !b[x/3][y/3][i]){
                r[x][i] = 1;
                c[y][i] = 1;
                b[x/3][y/3][i] = 1;
                ans[x].push_back(i);
                if (y == 8) f(x+1, 0);
                else f(x, y+1);
                r[x][i] = 0;
                c[y][i] = 0;
                b[x/3][y/3][i] = 0;
                ans[x].pop_back();
            }
        }
    }
}

int main(){
    for (int i = 0; i < 9; i++){
        ci[str[i]] = i;
        ic[i] = str[i];
    }
    for (int i = 0; i < 9; i++){
        cin >> s;
        for (int j = 0; j < 9; j++){
            if (s[j] != '*'){
                int tmp = ci[s[j]];
                g[i][j] = tmp;
                r[i][tmp] = 1;
                c[j][tmp] = 1;
                b[i/3][j/3][tmp] = 1;
            }
            else g[i][j] = -1;
        }
    }
    f(0, 0);
}

分享本文 Share with friends