POJ 2676 Sudoku

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20113   Accepted: 9628   Special Judge

Description

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task. 

Input

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

Output

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

Sample Input

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN  10
#define INF 1000000009
/*
数独游戏,9*9的方格
要求 每行,每一列,还有九个3*3的方格(如图) 中都出现1—9的全部数字
DFS回溯法搜索 难点在于如何保证九个 3 3的方格中出现1到9的全部数字
对每个方格找到对应的区域然后检查
*/
struct node
{
    int x, y;
    node(int _x,int _y):x(_x),y(_y){}
};
vector<node> v;
char g[MAXN][MAXN],ans[MAXN][MAXN];
bool judge(int x, int y,char c)
{
    int tx = x / 3 * 3, ty = y / 3 * 3;
    for (int i = 0; i < 9; i++)
    {
        if (g[i][y] == c) return false;
        if (g[x][i] == c) return false;
        if (g[tx + i/3][ty + i-i/3*3] == c)
            return false;
    }
    return true;
}
bool dfs(int x)
{
    if (x == v.size())
        return true;
    for (int i = 1; i <= 9; i++)
    {
        if (!judge(v[x].x, v[x].y, i+'0'))
            continue;
        //cout << v[x].x << ' ' << v[x].y << ' ' << char(i + '0') << endl;
        g[v[x].x][v[x].y] = i + '0';
        if (dfs(x + 1))
            return true;
    }
    g[v[x].x][v[x].y] = '0';
    return false;
}
int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        v.clear();
        for (int i = 0; i < 9; i++)
        {
            scanf("%s", g[i]);
            for (int j = 0; j < 9; j++)
            {
                if(g[i][j]=='0')
                    v.push_back(node(i, j));
            }
        }
        dfs(0);
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                printf("%c", g[i][j]);
            }
            printf("
");
        }
    }
    return 0;
}

 改进版 :空间换时间

把judge改成inline能节省300ms

680K 860MS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN  10
#define INF 1000000009
/*
数独游戏,9*9的方格
要求 每行,每一列,还有九个3*3的方格(如图) 中都出现1—9的全部数字
DFS回溯法搜索 难点在于如何保证九个 3 3的方格中出现1到9的全部数字
对每个方格找到对应的区域然后检查
*/
struct node
{
    int x, y;
    node(int _x,int _y):x(_x),y(_y){}
};
vector<node> v;
char g[MAXN][MAXN],ans[MAXN][MAXN];
bool col[MAXN][MAXN], row[MAXN][MAXN], grid[MAXN][MAXN][MAXN];
 bool judge(int x, int y,int i)
{
    int tx = x / 3 * 3, ty = y / 3 * 3;
    if (!row[x][i] && !col[y][i] && !grid[tx][ty][i])
        return true;
    return false;
}
bool dfs(int x)
{
    if (x == v.size())
        return true;
    for (int i = 1; i <= 9; i++)
    {
        if (!judge(v[x].x, v[x].y, i))
            continue;
        col[v[x].y][i] = row[v[x].x][i] = grid[v[x].x / 3 * 3][v[x].y / 3 * 3][i] = true;
        g[v[x].x][v[x].y] = i + '0';
        if (dfs(x + 1))
            return true;
        col[v[x].y][i] = row[v[x].x][i] = grid[v[x].x / 3 * 3][v[x].y / 3 * 3][i] = false;
    }
    g[v[x].x][v[x].y] = '0';
    return false;
}
int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        v.clear();
        memset(grid, false, sizeof(grid));
        memset(row, false, sizeof(row));
        memset(col, false, sizeof(col));
        for (int i = 0; i < 9; i++)
        {
            scanf("%s", g[i]);
            for (int j = 0; j < 9; j++)
            {
                if(g[i][j]=='0')
                    v.push_back(node(i, j));
                else
                {
                    int tx = i / 3 * 3, ty = j / 3 * 3;
                //    cout << tx <<' '<< ty << endl;
                    row[i][g[i][j] - '0'] = col[j][g[i][j] - '0'] = grid[tx][ty][g[i][j] - '0'] = true;
                }
            }
        }
        dfs(0);
        for (int i = 0; i < 9; i++)
        {
            for (int j = 0; j < 9; j++)
            {
                printf("%c", g[i][j]);
            }
            printf("
");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/joeylee97/p/6776895.html