QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#207948#6644. Red Black GridUFRJ#WA 0ms3876kbC++143.9kb2023-10-09 00:46:192023-10-09 00:46:19

Judging History

你现在查看的是最新测评结果

  • [2023-10-09 00:46:19]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3876kb
  • [2023-10-09 00:46:19]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;


bool guloso(vector<vector<int>> &grid, int n, int k){

    auto isColor = [&](int i, int j, int c) -> bool {
        if(i < 0 || i >= n) return false;
        if(j < 0 || j >= n) return false;
        if(grid[i][j] == c) return true;
        return false;
    };
    using T = tuple<int, int, int>; // qtd dif, i, j
    array<int, 5> qtd = {0};
    int tot = 0;
    for(int i = 0; i < n; i++){
        for(int j = 0; j<n; j++){
            if(grid[i][j]){
                tot += isColor(i-1, j, 0);
                tot += isColor(i+1, j, 0);
                tot += isColor(i, j-1, 0);
                tot += isColor(i, j+1, 0);
            } else {
                int cur = 0;
                cur += isColor(i-1, j, 1);
                cur += isColor(i+1, j, 1);
                cur += isColor(i, j-1, 1);
                cur += isColor(i, j+1, 1);
                qtd[cur]++;
            }
        }
    }
    if(k > tot) return false;
    int dif = tot - k;
    vector<bool> dp(dif + 1);
    vector<pair<int, int>> link(dif + 1, {-1, -1});
    dp[0] = 1;
    for(int i = 1; i <= 4; i++){
        for(int j = dif - i; j >=0 ; j--){
            if(!dp[j]) continue;
            int k = qtd[i], s = j + i;
            while(k > 0 && s <= dif && !dp[s]){
                dp[s] = 1, link[s] = {k, i},  --k, s += i;
            }
        }
    }
    if(dp[dif]){
        vector<int> change(5);
        int j = dif;
        while(j > 0){
            auto [cur, i] = link[j];
            //cout<<j<<" "<<cur<<" "<<i<<"\n";
            change[i] += cur;
            j = j - cur * i;
        }

        for(int i = 0; i<n; i++){
            for(int j = 0; j< n; j++){
                if(!grid[i][j]){
                    int cur = 0;
                    cur += isColor(i-1, j, 1);
                    cur += isColor(i+1, j, 1);
                    cur += isColor(i, j-1, 1);
                    cur += isColor(i, j+1, 1);
                    if(change[cur] > 0){
                        grid[i][j] = 1;
                        //cout<<"mudei "<<i<<" "<<j<<" "<<cur<<" "<<change[cur]<<"\n";
                        change[cur]--;
                    }
                }
            }
        }
        return true;
    }


    return false;
}

void solve(){
    int n, k;
    cin>>n>>k;
    if(n == 1){
        cout<<"Possible\n";
        cout<<"R\n";
        return;
    }
    if(k == 1 || k == n * (n-1)/2 - 1){
        cout<<"Impossible\n";
        return;
    }
    else if(n == 3 && k == 5){
        cout<<"Possible\n";
        cout<<"RRR\n";
        cout<<"RBB\n";
        cout<<"RRR\n";
    }
    else {
        vector<vector<int>> grid(n, vector<int>(n));
        for(int i = 0; i<n; i++){
            for(int j = (i%2); j < n; j += 2){
                grid[i][j] = 1;
            }
        }
        if(guloso(grid, n, k)){
            cout<<"Possible\n";
            for(int i = 0; i<n; i++){
                for(int j = 0; j<n; j++){
                    if(grid[i][j]) cout<<"B";
                    else cout<<"R";
                }
                cout<<"\n";
            }
            return;
        }
        grid = vector<vector<int>>(n, vector<int>(n));
        for(int i = 0; i<n; i++){
            for(int j = !(i%2); j < n; j += 2){
                grid[i][j] = 1;
            }
        }
        if(guloso(grid, n, k)){
            cout<<"Possible\n";
            for(int i = 0; i<n; i++){
                for(int j = 0; j<n; j++){
                    if(grid[i][j]) cout<<"B";
                    else cout<<"R";
                }
                cout<<"\n";
            }
            return;
        }
        cout<<"Impossible\n";
     }
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t; cin>>t;
    while(t--){
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3876kb

input:

2
3 6
3 1

output:

Possible
BBB
BBB
BRB
Impossible

result:

wrong answer Condition failed: "getNum(vec) == k" (test case 1)