Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define debug(s) cout<< #s <<" = "<< s <<endl
- #define all(v) (v).begin(), (v).end()
- #define mem(a,val) memset(a,val,sizeof a)
- #define ll long long
- #define ff first
- #define ss second
- #define pb push_back
- #define endl '\n'
- bool flag = 0;
- int n = 9,tot,g[11][11];
- bitset<9> row[11],col[11],blk[11];
- vector<pair<int,int> > v;
- int block[][9] = {
- { 0, 0, 0, 1, 1, 1, 2, 2, 2 },
- { 0, 0, 0, 1, 1, 1, 2, 2, 2 },
- { 0, 0, 0, 1, 1, 1, 2, 2, 2 },
- { 3, 3, 3, 4, 4, 4, 5, 5, 5 },
- { 3, 3, 3, 4, 4, 4, 5, 5, 5 },
- { 3, 3, 3, 4, 4, 4, 5, 5, 5 },
- { 6, 6, 6, 7, 7, 7, 8, 8, 8 },
- { 6, 6, 6, 7, 7, 7, 8, 8, 8 },
- { 6, 6, 6, 7, 7, 7, 8, 8, 8 } };
- bool check(int x, int y, int num)
- {
- int b = block[x][y],l = num-1;
- if(row[x][l] or col[y][l] or blk[b][l]) return false;
- g[x][y] = num;
- row[x][l] = col[y][l] = blk[b][l] = true;
- return true;
- }
- void backtrack(int idx)
- {
- if(flag) return;
- if(idx == tot){
- flag = 1;
- for(int i = 0; i < n; ++i){
- for(int j = 0; j < n; ++j){
- cout << g[i][j];
- }
- cout << endl;
- }
- return;
- }
- int x = v[idx].ff;
- int y = v[idx].ss;
- for(int i = 1; i <= 9; ++i){
- if(check(x,y,i)){
- backtrack(idx+1);
- if(flag) return;
- g[x][y] = 0;
- int l = i-1;
- int b = block[x][y];
- row[x][l] = col[y][l] = blk[b][l] = false;
- }
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);cin.tie(NULL);
- #ifndef ONLINE_JUDGE
- freopen("in", "r", stdin);
- freopen("out","w",stdout);
- #endif
- int t;
- cin >> t;
- for(int cs = 1; cs <= t; ++cs){
- flag = 0;
- mem(g,0);
- mem(row,0);
- mem(col,0);
- mem(blk,0);
- for(int i = 0; i < n; ++i){
- for(int j = 0; j < n; ++j){
- char c;
- cin >> c;
- if(c == '.') v.pb({i,j});
- else{
- g[i][j] = (c-'0');
- row[i][g[i][j]-1] = col[j][g[i][j]-1] = blk[block[i][j]][g[i][j]-1] = true;
- }
- }
- }
- tot = v.size();
- cout << "Case " << cs << ":" << endl;
- backtrack(0);
- v.clear();
- }
- }
Add Comment
Please, Sign In to add comment