poj 2676 Sudoku (裸dfs&标记)

Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12594   Accepted: 6275   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. 
poj 2676 Sudoku (裸dfs&标记)_第1张图片

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

Source

Southeastern Europe 2005

题意:

填数独。

思路:

标记。暴力dfs。

#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double PI=acos(-1.0);
const int maxn=100010;
typedef __int64 ll;
bool row[15][15],col[15][15],blk[5][5][15];//标记行列块的九个数字是否被使用
char maze[15][15];//存数独
int cnt;//记录未填格子数
struct yb
{
    int x,y;
} blank[100];//未填格子的坐标
void init()
{
    cnt=0;
    memset(row,0,sizeof row);
    memset(col,0,sizeof col);
    memset(blk,0,sizeof blk);
}
bool dfs()
{
    int x,y,i;
    if(cnt==0)
        return true;
    x=blank[cnt-1].x;
    y=blank[cnt-1].y;
    for(i=1;i<=9;i++)//枚举当前未填格的数字
    {
        if(!row[x][i]&&!col[y][i]&&!blk[(x+2)/3][(y+2)/3][i])//行列块都未被使用
        {
            row[x][i]=col[y][i]=blk[(x+2)/3][(y+2)/3][i]=true;
            maze[x][y]='0'+i;
            cnt--;
            if(dfs())
                return true;
            row[x][i]=col[y][i]=blk[(x+2)/3][(y+2)/3][i]=false;//回溯
            cnt++;
        }
    }
    return false;
}
int main()
{
    int cas,i,j,t;

    scanf("%d",&cas);
    while(cas--)
    {
        for(i=1;i<=9;i++)
            scanf("%s",maze[i]+1);
        init();
        for(i=1;i<=9;i++)
        {
            for(j=1;j<=9;j++)
            {
                t=maze[i][j]-'0';
                row[i][t]=col[j][t]=blk[(i+2)/3][(j+2)/3][t]=true;
                if(maze[i][j]=='0')//记录未填格的位置
                {
                    blank[cnt].x=i;
                    blank[cnt++].y=j;
                }
            }
        }
        if(dfs())
            for(i=1;i<=9;i++)
                printf("%s\n",maze[i]+1);
    }
    return 0;
}


你可能感兴趣的:(c,算法,ACM)