HDU 1426 Sudoku Killer(搜索)

题目链接: 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;
    
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值