1317. Sudoku

Source: Internet
Author: User

Problem-solving skills: 1. The number of Sudoku per grid record itself can fill the number, each search can fill the number of the minimum number of lattice fill numbers.

2. Use "Disable Count" to record the number of times a number is disabled for each lattice so that it can be resumed after disabling.

#include <cstdio> #include <cstring> #include <set> #include <vector>using std::set;using std::    vector;struct Elem {int num; Set<int> lefts;}; set<int> lefts;const int maxn = 9;char charmap[maxn][maxn+1];    Elem map[maxn][maxn];int ans[maxn][maxn];int count;int f[maxn][maxn][maxn + 1];struct Cor {int R, C; Cor (int r_ = 0, int c_ = 0): R (R_), C (C_) {} bool operator< (const Cor &cor) Const {return (R < COR.R ) ||    (r = = COR.R && c < cor.c);            }};set<cor> unfills;void Init () {for (int i = 0, i < MAXN; ++i) {for (int j = 0; j < Maxn; ++j) {            Map[i][j].num = 0;        Map[i][j].lefts.insert (Lefts.begin (), Lefts.end ());  }} for (int i = 0, i < MAXN; ++i) {for (int j = 0; j < Maxn; ++j) {for (int k = 0; K < MAXN;            ++k) {f[i][j][k + 1] = 0;    }}} count = 0; Unfills.clear ();} void input () {for (int i = 0; i < MAXN;    ++i) {scanf ("%s", Charmap[i]);            }}void transform () {for (int i = 0; i < MAXN, ++i) {for (int j = 0; Charmap[i][j]! = '; ++j ') {            if (charmap[i][j]! = ' _ ') {Map[i][j].num = charmap[i][j]-' 0 ';            } else {Unfills.insert (Cor (i, j));    }}}}void setting (int r, int c, int num) {map[r][c].num = num;        for (int i = 0; i < MAXN; ++i) {map[r][i].lefts.erase (num);        ++f[r][i][num];        Map[i][c].lefts.erase (num);    ++f[i][c][num];    } int beginr = R/3 * 3;    int beginc = C/3 * 3; for (int i = 0, i < 3; ++i) {for (int j = 0; j < 3; ++j) {map[i + beginr][j + Beginc].lefts.eras            E (NUM);        ++f[i + beginr][j + beginc][num];    }}}void Resume (int r, int c, int num) {map[r][c].num = 0;        for (int i = 0; i < MAXN; ++i) {if (--f[r][i][num] = = 0) map[r][i].lefts.insert (num); if (--f[i][c][num] = = 0) map[i][c].lefts.insert (num);    } int beginr = R/3 * 3;    int beginc = C/3 * 3;  for (int i = 0, i < 3; ++i) {for (int j = 0; j < 3; ++j) {if (--f[i+beginr][j+beginc][num] = = 0)        Map[i + beginr][j + beginc].lefts.insert (num); }}}void Predeal () {for (int i = 0, i < MAXN; ++i) {for (int j = 0; j < Maxn; ++j) {if (M            Ap[i][j].num! = 0) {Setting (I, J, Map[i][j].num);        }}}}void Dfs () {if (Unfills.empty ()) {///To achieve a destination state ++count;        if (Count > 1) return; for (int i = 0, i < MAXN; ++i) {for (int j = 0; j < Maxn; ++j) {Ans[i][j] = map[i][j].nu            M }}} else {//traverse unfills, look for the smallest set<cor>::iterator iter = Unfills.begin (), target = Unfills.        Begin ();            while (iter! = Unfills.end ()) {const Cor &cor1 = *iter;   Const Cor &AMP;COR2 = *target;         if (Map[cor1.r][cor1.c].lefts.size () < Map[cor2.r][cor2.c].lefts.size ()) {target = iter;        } ++iter;        } Cor cor = *target;        Unfills.erase (target);   Set<int> _lefts (map[cor.r][cor.c].lefts);        Set<int>::iterator iter1 = _lefts.begin ();            while (Iter1! = _lefts.end ()) {int num = *iter1;            Setting (COR.R, COR.C, num);            DFS ();            Resume (COR.R, cor.c, num);        ++iter1;    } unfills.insert (COR);    }}int Main () {for (int i = 1; I <= 9; ++i) Lefts.insert (i);    int t;    scanf ("%d", &t);        for (int tt = 1; TT <= t; ++tt) {//Initialize init ();        Enter input ();        Conversion transform ();        Pretreatment predeal ();        Deep Search DFS ();        if (count = = 0) {printf ("Puzzle%d has no solution\n", TT); } else if (count = = 1) {printf ("Puzzle%d solution is\n", TT); for (int i = 0; i < MAXN; ++i) {for (int j = 0; j < Maxn; ++j) {printf ("%d", ans I                [j]);            } printf ("\ n");        }} else if (Count > 1) {printf ("Puzzle%d has%d solutions\n", TT, Count);    } if (tt! = t) printf ("\ n"); } return 0;}

  

1317. Sudoku

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.