Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using uint = unsigned int;
- using ll = long long;
- using pii = pair<int, int>;
- #define dbg(x) cerr<<#x": "<<(x)<<'\n'
- #define dbg_p(x) cerr<<#x": "<<(x).first<<' '<<(x).second<<'\n'
- #define dbg_v(x, n) {cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<(x)[_]<<' ';cerr<<'\n';}
- #define all(v) v.begin(), v.end()
- #define fi first
- #define se second
- #define INF 2000000010
- #define MOD 1000000007
- #define ST_SIZE 1048600
- #define QMAX
- #define NMAX 20
- char s[NMAX][NMAX];
- int a[NMAX][NMAX];
- int col[NMAX], row[NMAX], ptr[NMAX][NMAX];
- bool ok(int x, int y, int val) {
- if(col[y] & (1 << val)) return 0;
- if(col[x] & (1 << val)) return 0;
- if(ptr[x / 4 * 4][y / 4 * 4] & (1 << val)) return 0;
- return 1;
- for(int j = 0; j < 16; ++j) if(a[x][j] == val) return false;
- for(int i = 0; i < 16; ++i) if(a[i][y] == val) return false;
- int l = x / 4 * 4;
- int c = y / 4 * 4;
- for(int i = l; i < l + 4; ++i)
- for(int j = c; j < c + 4; ++j)
- if(a[i][j] == val) return false;
- return true;
- }
- bool full() {
- for(int i = 0; i < 16; ++i)
- for(int j = 0; j < 16; ++j)
- if(a[i][j] == -1) return false;
- return true;
- }
- bool bkt2(int toComplete) {
- if(toComplete == 0) {
- for(int i = 0; i < 16; ++i, cout << '\n')
- for(int j = 0; j < 16; ++j) cout << char('A' + a[i][j]);
- return true;
- }
- int x, y, nrsol = 100;
- for(int i = 0; i < 16; ++i)
- for(int j = 0; j < 16; ++j) {
- if(a[i][j] != -1) continue;
- int nr = 0;
- for(int val = 0; val < 16; ++val) if(ok(i, j, val)) ++nr;
- if(nr < nrsol) nrsol = nr, x = i, y = j;
- }
- for(int val = 0; val < 16; ++val)
- if(ok(x, y, val)) {
- a[x][y] = val; col[y] ^= (1 << val); row[x] ^= (1 << val); ptr[x / 4 * 4][y / 4 * 4] ^= (1 << val);
- if(bkt2(toComplete - 1)) return true;
- a[x][y] = -1; col[y] ^= (1 << val); row[x] ^= (1 << val); ptr[x / 4 * 4][y / 4 * 4] ^= (1 << val);
- }
- return false;
- }
- bool bkt(int used) {
- cerr << used << '\n';
- if(!used) {
- for(int i = 0; i < 16; ++i, cout << '\n')
- for(int j = 0; j < 16; ++j) cout << char('A' + a[i][j]);
- return true;
- }
- int mn = 100;
- for(int i = 0; i < 16; ++i)
- for(int j = 0; j < 16; ++j)
- if(a[i][j] == -1) {
- int cnt = 0;
- for(int val = 0; val < 16; ++val) {
- if(!ok(i, j, val)) continue;
- ++cnt;
- }
- mn = min(mn, cnt);
- }
- for(int i = 0; i < 16; ++i)
- for(int j = 0; j < 16; ++j)
- if(a[i][j] == -1) {
- int cnt = 0;
- for(int val = 0; val < 16; ++val) {
- if(!ok(i, j, val)) continue;
- ++cnt;
- }
- if(cnt != mn || cnt > 2) continue;
- for(int val = 0; val < 16; ++val) {
- if(!ok(i, j, val)) continue;
- a[i][j] = val; col[j] ^= (1 << val); row[i] ^= (1 << val); ptr[i / 4 * 4][j / 4 * 4] ^= (1 << val);
- if(bkt(used - 1)) return 1;
- a[i][j] = -1; col[j] ^= (1 << val); row[i] ^= (1 << val); ptr[i / 4 * 4][j / 4 * 4] ^= (1 << val);
- }
- }
- return false;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- int i, j, toComplete;
- while(cin >> s[0]) {
- for(i = 0; i < 16; ++i) col[i] = 0, row[i] = 0;
- for(i = 0; i < 4; ++i)
- for(j = 0; j < 4; ++j)
- ptr[i][j] = 0;
- for(i = 1; i < 16; ++i) cin >> s[i];
- for(toComplete = 0, i = 0; i < 16; ++i)
- for(j = 0; j < 16; ++j)
- if(s[i][j] == '-') a[i][j] = -1, ++toComplete;
- else a[i][j] = (s[i][j] - 'A'), col[j] |= (1 << a[i][j]), row[i] |= (1 << a[i][j]), ptr[i / 4 * 4][j / 4 * 4] |= (1 << a[i][j]);
- cout << bkt(toComplete) << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement