QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#397363#6644. Red Black GridUSP_USP_USP#TL 0ms3684kbC++142.6kb2024-04-23 23:46:462024-04-23 23:46:47

Judging History

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

  • [2024-04-23 23:46:47]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3684kb
  • [2024-04-23 23:46:46]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define int long long
void dbg_out() { cerr << endl; }
template <typename H, typename... T>
void dbg_out(H h, T... t) { cerr << ' ' << h; dbg_out(t...); }
#define dbg(...) { cerr << #__VA_ARGS__ << ':'; dbg_out(__VA_ARGS__); }
using ll = long long;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 2e5 + 5;
const int MOD = 1e9+7; //998244353;
const int INF = 0x3f3f3f3f;
const ll INF64 = ll(4e18) + 5;

vector<pair<int,int>> d4 = { {1,0}, {0,1}, {-1,0}, {0,-1}};

void solve(){
	int n,k;
	cin >> n >> k;
	for(int op = 0; op < 2; op++){
		vector<vector<int>> ans(n, vector<int>(n));
		auto calc = [&](int i, int j){
			int at = 0;
			for(int w = 0; w < 4; w++){
				int ni = i + d4[w].fi;
				int nj = j + d4[w].se;
				if(ni >= 0 && nj >= 0 && ni < n && nj < n){
					at += ans[ni][nj] != ans[i][j];
				}
			}
			return at;
		};

		auto print = [&](){
			cout <<"Possible\n";
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					cout << (ans[i][j] == 0 ? 'B' : 'R');
				}
				cout << '\n';
			}
		};
		int sum = 0;
		auto flip = [&](int i, int j){
			sum -= calc(i,j);
			ans[i][j] ^= 1;
			sum += calc(i,j);
		};
		int fi = -1;
		int fj = -1;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				if( (i+j+op)%2 == 0){
					flip(i,j);
					if(sum > k){
						fi = i;
						fj = j;
						flip(i,j);
					}
					if(sum == k){
						break;
					}
				}
			}
			if(sum == k || fi != -1) break;
		}
		if(fi == -1 && sum != k){
			continue;
		}
		if(sum == k){
			print();
			return;
		}
		vector<pair<int,int>> vals;
		for(int dx = -1; dx <= 2; dx++){
			for(int dy = -2; dy <= 2; dy++){
				int ni = fi+dx;
				int nj = fj+dy;
				if(ni >= 0 && nj >= 0 && ni < n && nj < n){
					vals.pb({ni,nj});
				}
			}
		}
		int sz = vals.size();
		for(int mask = 0; mask < (1<<n); mask++){
			for(int w = 0; w < sz; w++) if( (mask>>w)&1){
				auto [i,j] = vals[w];
				flip(i,j);
			}
			if(sum == k){
				print();
				return;
			}
			for(int w = 0; w < sz; w++) if( (mask>>w)&1){
				auto [i,j] = vals[w];
				flip(i,j);
			}

		}
	}
	cout << "Impossible\n";
}

signed main(){

	ios::sync_with_stdio(false); cin.tie(NULL);

	int t = 1;
	cin >> t;
	while(t--){
		solve();
	}

	return 0;
}

/*
Makefile:
CXXFLAGS=-Wall -Wextra -Wshadow -g -pedantic -fsanitize=address,undefined -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUGPEDANTIC -std=gnu++17
 */

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3684kb

input:

2
3 6
3 1

output:

Possible
BRB
RBB
BBB
Impossible

result:

ok correct! (2 test cases)

Test #2:

score: -100
Time Limit Exceeded

input:

4424
1 0
2 4
2 3
2 2
2 1
2 0
3 12
3 11
3 10
3 9
3 8
3 7
3 6
3 5
3 4
3 3
3 2
3 1
3 0
4 24
4 23
4 22
4 21
4 20
4 19
4 18
4 17
4 16
4 15
4 14
4 13
4 12
4 11
4 10
4 9
4 8
4 7
4 6
4 5
4 4
4 3
4 2
4 1
4 0
5 40
5 39
5 38
5 37
5 36
5 35
5 34
5 33
5 32
5 31
5 30
5 29
5 28
5 27
5 26
5 25
5 24
5 23
5 22
5 21
5...

output:

Possible
R
Possible
RB
BR
Impossible
Possible
RB
BB
Impossible
Possible
BB
BB
Possible
RBR
BRB
RBR
Impossible
Possible
RBR
BRB
RBB
Possible
BRB
RBR
BBB
Possible
RBR
BRB
BBB
Impossible
Possible
BRB
RBB
BBB
Impossible
Possible
RBR
BBB
BBB
Possible
RRB
BBB
BBB
Possible
RBB
BBB
BBB
Impossible
Possible
B...

result: