HUD 1426 Sudoku Killer (DFS)



链接 : Here!

思路 : 记录下所有 "?" , 出现的位置, 然后 $DFS$ 一下, 对于每个位置来说都可以填充 $9$ 种数值, 然后对于判断填充是否合法需要三个标记数组来辅助记录. $visR[i][num] = 1, 代表第 i 行num值已经出现过, visC[i][num] = 1, 代表第 i 列num值已经出现过, visB[i][num] = 1, 代表第 i 小块 num值已经出现过$. 计算小块的标号只需要 $x / 3 * 3 + y / 3 $ 即可.

类比 : 与之相似的题目是 hihocoder 1321, 但是这样做会 T, 所以 hihocoder 需要用舞蹈链

注意 : 此题读入十分恶心, 最好在写完读入操作后检查一下. 还需要注意, 如果是已经给出的值 (即非‘?‘), 需要先标记一下.


/*************************************************************************
    > File Name: 1426-Sudoku-Killer.cpp
    > Author:
    > Mail:
    > Created Time: 2017年11月29日 星期三 17时16分39秒
 ************************************************************************/

#include <bits/stdc++.h>
using namespace std;

#define MAX_N 20
struct Point {
    int x, y;
} pt[110];
int node_cnt = 0;
int G[MAX_N][MAX_N];
int visR[MAX_N][MAX_N] = {0};
int visC[MAX_N][MAX_N] = {0};
int visB[MAX_N][MAX_N] = {0};
int ok = 0;

void clear() {
    node_cnt = 0;
    ok = 0;
    memset(G, 0, sizeof(G));
    memset(visR, 0, sizeof(visR));
    memset(visC, 0, sizeof(visC));
    memset(visB, 0, sizeof(visB));
}
int cal_block(int x, int y) {
    return (x / 3 * 3 + y / 3);
}
bool check(int x, int y, int block, int num) {
    return !(visR[x][num] || visC[y][num] || visB[block][num]);
}
void set_pt(int x, int y, int block, int num) {
    visR[x][num] = visC[y][num] = visB[block][num] = 1;
}
void move_pt(int x, int y, int block, int num) {
    visR[x][num] = visC[y][num] = visB[block][num] = 0;
}
void dfs(int step) {
    if (ok) return;
    if (step == node_cnt) {
        for (int i = 0 ; i < 9 ; ++i) {
            for (int j = 0 ; j < 8 ; ++j) {
                printf("%d ", G[i][j]);
            }
            printf("%d\n", G[i][8]);
        }
        ok = 1;
        return;
    }
    for (int i = 1 ; i <= 9 ; ++i) {
        int x = pt[step].x;
        int y = pt[step].y;
        int block = cal_block(x, y);
        if (!check(x, y, block, i)) continue;
        set_pt(x, y, block, i);
        int t_val = G[x][y];
        G[x][y] = i;
        dfs(step + 1);
        G[x][y] = t_val;
        move_pt(x, y, block, i);
    }
    return;
}
int main() {
    int kase = 0;
    char ch;
    while (scanf("%c", &ch) != EOF) {
        if (ch == '\n') continue;
        if (kase) printf("\n");
        ++kase;

        G[0][0] = (ch == '?' ? 0 : (ch - '0'));
        if (G[0][0] == 0) {
            pt[node_cnt].x = 0;
            pt[node_cnt].y = 0;
            ++node_cnt;
        } else {
            set_pt(0, 0, cal_block(0, 0), G[0][0]);
        }
        for (int i = 1 ; i < 9 ; ++i) {
            scanf("%c", &ch);
            if (ch == ' ') {
                --i; continue;
            }
            G[0][i] = (ch == '?' ? 0 : (ch - '0'));
            if (G[0][i] == 0) {
                pt[node_cnt].x = 0;
                pt[node_cnt].y = i;
                ++node_cnt;
            } else {
                set_pt(0, i, cal_block(0, i), G[0][i]);
            }
        }
        for (int i = 1 ; i < 9 ; ++i) {
            getchar();
            for (int j = 0 ; j < 9 ; ++j) {
                scanf("%c", &ch);
                if (ch == ' ') {
                    --j; continue;
                }
                G[i][j] = (ch == '?' ? 0 : (ch - '0'));
                if (G[i][j] == 0) {
                    pt[node_cnt].x = i;
                    pt[node_cnt].y = j;
                    ++node_cnt;
                } else {
                    set_pt(i, j, cal_block(i, j), G[i][j]);
                }
            }
        }
        dfs(0);
        clear();
    }
    return 0;
}
时间: 2024-08-06 02:53:11

HUD 1426 Sudoku Killer (DFS)的相关文章

hdu 1426 Sudoku Killer(DFS)

1 #include <iostream> 2 #include <memory.h> 3 #include <vector> 4 #include <string> 5 #include <cstdio> 6 using namespace std; 7 8 int row[11][11],col[11][11],blo[11][11],mp[11][11]; 9 struct node{ 10 int x,y; 11 }; 12 node u

HDU 1426 Sudoku Killer DFS 简单题

给出一个数独的一部分,然后然后要我们填完整这个数独. Input 本题包含多组测试,每组之间由一个空行隔开.每组测试会给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开.其中1-9代表该位置的已经填好的数,问号(?)表示需要你填的数. Output 对于每组测试,请输出它的解,同一行相邻的两个数用一个空格分开.两组解之间要一个空行.对于每组测试数据保证它有且只有一个解. Sample Input 7 1 2 ? 6 ? 3 5 8 ? 6 5 2 ? 7 1 ? 4 ? ? 8 5 1

HDU 1426 Sudoku Killer(dfs 解数独)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1426 Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9804    Accepted Submission(s): 2944 Problem Description 自从2006年3月10日至11日的首届数独世界

HDU 1426 Sudoku Killer(数独,划分区域是关键)

Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6283    Accepted Submission(s): 1981 Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单

HDU 1426 Sudoku Killer【DFS 数独】

自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品———HDU免费七日游外加lcy亲笔签名以及同hdu acm team合影留念的机会. 所以全球人民前仆后继,为了奖品日夜训练茶饭不思.当然也包括初学者linle,不过他太笨了又没有多少耐性,只能做做最最基本的数独题,不过他还是想得到那些奖品,你能帮帮他吗?你只要把答案告诉他就可以,不用教他是怎么做的

HDOJ Sudoku Killer(dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1426 思路分析:该问题为数独问题,明显解是唯一的,所有采用dfs搜索效果更好: 在搜索时,可以通过3个数组来判断对于某个特定的数是否能够满足要求,即在每一行.每一列和每一个3X3的方块中只有唯一的1~9之间的数: vis_r数组:如果vis_r[i][j] == 1表示在第i行中数字j已经存在,否则表示不存在: vis_c数组:如果vis_c[i][j] == 1表示在第j列数字j已经存在,否则表示

hdu 1426 Sudoku Killer

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1426 1 #include<stdio.h> 2 #include<math.h> 3 #include<string.h> 4 #include<stdlib.h> 5 #include<iostream> 6 using namespace std; 7 char map[12][12]; 8 bool row[12][12],list[12][12

hdu 1426 Sudoku Killer ( Dancing Link 精确覆盖 )

利用 Dancing Link 来解数独 具体的可以看    lrj 的训练指南 和 < Dancing Links 在搜索中的应用 >这篇论文 Dancing Link 来求解数独 , 是通过求解精确覆盖 精确覆盖就是给出一个 01 矩阵 , 要求我们选择一些行 , 使得每一列有且仅有一个 1 对于数独问题 , 行就是我们的选择 , 即在第 i 行 第 j 列 放上 数字 k , 所以我们最多有 i * j * k 中选择 如果某些位置( x , y  )已经放了数字 a , 那么我们的选择

hdu1426 Sudoku Killer dfs

Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视.  据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品―――HDU免费七日游外加lcy亲笔签名以及同hdu acm team合影留念的机会.  所以全球人民前仆后继,为了奖品日夜训练茶饭不思.当然也包括初学者linle,不过他太笨了又没有多少耐性,只能做做最最基本的数独题,不过他还是想得到那些奖品,你能帮帮他吗?你只要把答案告诉