HDU 1426 Sudoku Killer(搜索)

时间:2022-04-17 02:39:11
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1426

题意很明确,让你解一个9*9的数独。

DFS即可。

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define REP(i,n)        for(int i(0); i <  (n); ++i)
#define rep(i,a,b)        for(int i(a); i <= (b); ++i)
#define dec(i,a,b)        for(int i(a); i >= (b); --i)
#define for_edge(i,x)        for(int i = H[x]; i; i = X[i])

#define LL    long long
#define ULL    unsigned long long
#define MP    make_pair
#define PB    push_back
#define FI    first
#define SE    second
#define INF    1 << 30

const int N    =    100000        +    10;
const int M    =    10000        +    10;
const int Q    =    1000        +    10;
const int A    =    30        +    1;


struct node{
    int x, y;
} d[Q];

char ch;

bool a[A][A];
int f[A][A], r[A][A], c[A][A], v[A][A];
bool rr[A][A], cc[A][A], vv[A][A];
int num;


inline void print(){
    rep(i, 1, 9){rep(j, 1, 8) printf("%d ", f[i][j]); printf("%d", f[i][9]); putchar(10);}
}

void dfs(int now){
    if (now > num){ print(); return ;}
    int x = d[now].x, y = d[now].y;

    rep(i, 1, 9){
        if (vv[v[x][y]][i]) continue;
        if (cc[c[x][y]][i]) continue;
        if (rr[r[x][y]][i]) continue;
        f[x][y] = i;
        vv[v[x][y]][i] = true;
        cc[c[x][y]][i] = true;
        rr[r[x][y]][i] = true;
        dfs(now + 1);
        f[x][y] = 0;
        vv[v[x][y]][i] = false;
        cc[c[x][y]][i] = false;
        rr[r[x][y]][i] = false;
    }

}

            

int main(){
#ifndef ONLINE_JUDGE
    freopen("test.txt", "r", stdin);
    freopen("test.out", "w", stdout);
#endif


    rep(i, 1, 3) rep(j, 1, 3) v[i][j] = 1;
    rep(i, 1, 3) rep(j, 4, 6) v[i][j] = 2;
    rep(i, 1, 3) rep(j, 7, 9) v[i][j] = 3;
    rep(i, 4, 6) rep(j, 1, 3) v[i][j] = 4;
    rep(i, 4, 6) rep(j, 4, 6) v[i][j] = 5;
    rep(i, 4, 6) rep(j, 7, 9) v[i][j] = 6;
    rep(i, 7, 9) rep(j, 1, 3) v[i][j] = 7;
    rep(i, 7, 9) rep(j, 4, 6) v[i][j] = 8;
    rep(i, 7, 9) rep(j, 7, 9) v[i][j] = 9;
    rep(i, 1, 9) rep(j, 1, 9) r[i][j] = i, c[i][j] = j;
    int Case = 0;

    //rep(i, 1, 9){rep(j, 1, 9) printf("%d ", c[i][j]); putchar(10); }
    while (~scanf("%c ", &ch)){
        if (Case) puts(""); else Case = 1;
    //    memset(a, false, sizeof a);
        memset(f, 0, sizeof f);
        memset(cc, false, sizeof cc);
        memset(vv, false, sizeof vv);
        memset(rr, false, sizeof vv);
        memset(d, 0, sizeof d); 
        num = 0;
        if (ch == '?'){
               f[1][1] = 0;
               d[++num].x = 1, d[num].y = 1;
        }     
        else{
                   int np = (int)ch - 48;
                f[1][1] = np;
                cc[c[1][1]][np] = true;
                vv[v[1][1]][np] = true;
                rr[r[1][1]][np] = true;
        }
        
        rep(cnt, 2, 81){
            scanf("%c ", &ch);
            int x = (cnt - 1) / 9 + 1, y = (cnt - 1) % 9 + 1;
            if (ch == '?'){
                   f[x][y] = 0;
                d[++num].x = x, d[num].y = y;
            }
            else{
                int np = (int)ch - 48;
                f[x][y] = np;
                cc[c[x][y]][np] = true;
                vv[v[x][y]][np] = true;
                rr[r[x][y]][np] = true;


            }    
        //rep(i, 1, 9){rep(j, 1, 9) printf("%d ", f[i][j]); putchar(10);}
        }
        dfs(1);
    }
            
    
    
    return 0;
    
}