HUD 1426 Sudoku Killer (DFS)


Link: Here!

Ideas: Record all "?", Where they appear, and then $ DFS $ bit, for each position can be filled for $ 9 $ kinds of values, then determine whether to fill a legitimate need for the three marker array to assist record. $ VisR [i ] [num] = 1, i-th row num value has occurred, visC [i] [num] = 1, i-th column of the num value has appeared, visB [i] [num] = 1, represents the i tile has appeared $ num value calculation requires only small numbers of $ x / 3 * 3 + y / 3 $ to.

Analogy: Similarly entitled hihocoder 1321, But doing so will T, so hihocoder needDancing links

Note: This question reads very sick, it is best to check at the finish after the read operation. Also note that if the value has been given (that is, non '?'), You need to mark it.


/*************************************************************************
    > File Name: 1426-Sudoku-Killer.cpp
    > Author: 
    > Mail: 
    > Created Time: 2017 Nian 11 Wednesday, 29 October 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;
}

Intelligent Recommendation

Sudoku Killer(HDU-1426)

Problem Description Since the first Sudoku World Championships from March 10 to 11, 2006, the game of Sudoku has been loved and valued more and more by people. It is said that in the 2008 Beijing Olym...

HDU 1426(Sudoku Killer)

Deep search questions, for each'?' box, determine in turn whether filling in 1~9 satisfies the same number in the row, column, and small nine squares. Keep going....

Sudoku Killer (hdu 1426 Sudoku)

Sudoku rule is this: in a grid of 9x9, you need to fill in the spaces among the numbers 1-9, and the squares of each row and each column contains nine figures 1-9. But also to ensure, with a thick lin...

Sudoku Killer HDU-1426 Sudoku

Title description Since the first Sudoku World Championships from March 10 to 11, 2006, the game of Sudoku has become more and more popular. It is said that in the 2008 Beijing Olympic Games, Sudoku w...

[Retrospective] [HDOJ] 1426 Sudoku Killer

1. The original question link:http://acm.hdu.edu.cn/showproblem.php?pid=1426 Problem Description since2006year3month10Day to11After the first Sudoku World Championship on the day, the game of Sudoku h...

More Recommendation

Luogu 1784 Sudoku (hdu 1426 Sudoku Killer)

Luogu 1784 Sudoku: The first question is to give you a 9x9 unfilled Sudoku (not filled with 0), asking you to fill in the numbers of these 0s to meet the number. Unique characteristics. parsing: In fa...

Solving Sudoku Problem---HDU--1426---Sudoku Killer

Topic connection:http://acm.hdu.edu.cn/showproblem.php?pid=1426 Problem Description Since the first Sudoku World Championships from March 10 to 11, 2006, the game of Sudoku has become more and more po...

HDU1426 Sudoku Killer DFS

Click to open the link Sudoku Killer   Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9634    Acc...

Sudoku Killer dfs

Sudoku Killer Link to the original question https://vjudge.net/contest/345248#problem/P is to write a program to calculate Sudoku Sudoku requires that the ranks and the numbers in each small nine-squa...

Sudoku Killer (Simple DFS)

Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1289 Accepted Submission(s): 422 Problem Description Since the first several Sino-Wo...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top