### 藍橋杯 數獨 深度優先搜索

005300000
800000020
070010500
400005300
010070006
003200080
060500009
004000030
000009700

145327698
839654127
672918543
496185372
218473956
753296481
367542819
984761235
521839764

800000000
003600000
070090200
050007000
000045700
000100030
001000068
008500010
090000400

812753649
943682175
675491283
154237896
369845721
287169534
521974368
438526917
796318452

CPU消耗  < 2000ms

`#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define M 11struct point{    int x, y;} p[100];//row[i][j]表示第i行是否有數字j，col[i][j]表示第i列是否有數字j，mat[i][j][k]表示第（i，j）位置的小九宮格里是否有數字kint Map[M][M];bool flag, row[M][M], col[M][M], mat[3][3][M];int np;void DFS(int num){    if (flag) return;    int i, j;    if (num == -1)    {        for (i = 0; i < 9 && !flag; i++)        {            for (j = 0; j < 9; j++)                printf ("%d", Map[i][j]);            printf ("\n");        }        flag = true;        return;    }    for (i = 1; i <= 9 && !flag; i++)    {        int x = p[num].x;        int y = p[num].y;        if (!row[x][i] && !col[y][i] && !mat[x/3][y/3][i])        {            row[x][i] = col[y][i] = mat[x/3][y/3][i] = true;            Map[x][y] = i;            DFS(num-1);            row[x][i] = col[y][i] = mat[x/3][y/3][i] = false;            Map[x][y] = 0;        }    }}int main(){    int t, i, j;    //scanf ("%d", &t);    //while (t--)    {        memset (row, false, sizeof (row));        memset (col, false, sizeof (col));        memset (mat, false, sizeof (mat));        np = 0;        for (i = 0; i < 9; i++)        {            for (j = 0; j < 9; j++)            {                scanf ("%1d", &Map[i][j]);                      // 新技能                int num = Map[i][j];                if (num)                    row[i][num] = col[j][num] = mat[i/3][j/3][num] = true;                else                {                    p[np].x = i;                    p[np++].y = j;                }            }        }        flag = false;        DFS(np-1);    }    return 0;}`