poj 2676 深搜之sudo

来源:互联网 发布:mac有线网络设置 编辑:程序博客网 时间:2024/06/06 00:49
Sudoku
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 16919 Accepted: 8242 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

1103000509002109400000704000300502006060000050700803004000401000009205800804000107

Sample Output

143628579572139468986754231391542786468917352725863914237481695619275843854396127

输入:char输入;

输入完成后:数字保存到board中,空位记录下来;

然后对所有空位进行深搜,搜索中注意每次搜索失败后的清零

//////////////////////////////////////////

<span style="font-size:14px;color:#6666cc;background-color: rgb(204, 204, 204);">#include <vector>#include <algorithm>#include <cstdio>#include <iostream>#include <cstring>using namespace std;int hang[9][10];int lie[9][10];int block[9][10];int board[9][10];struct pos{    int a,b;    pos(int aa,int bb):a(aa),b(bb){ }};//类的构造函数vector<pos> blankpos;int countblock(int i,int j){    int re;    re = (int)(i/3)*3+j/3;    return re;}void setall(int i,int j,int num,int n){    if(n)    {        board[i][j] = num;    }    else    {        board[i][j] = 0;    }    hang[i][num] = n;    lie[j][num] = n;    block[countblock(i,j)][num] = n;}bool ok(int i,int j,int num){    if(hang[i][num]!=1)    {        if(lie[j][num]!=1)        {            if(block[countblock(i,j)][num]!=1)                return true;        }    }    return false;}bool dfs(int n){    if(n<0)        return true;    int i = blankpos[n].a;    int j = blankpos[n].b;    for(int k = 1; k < 10; k++)    {        if(ok(i,j,k))        {            setall(i,j,k,1);            if(dfs(n-1))                return true;            else                setall(i,j,k,0);        }    }    return false;}int main(){    int n;    cin >> n;    while(n--)    {        memset(hang,0,sizeof(hang));        memset(lie,0,sizeof(lie));        memset(block,0,sizeof(block));        blankpos.clear();       //        for(int i = 0; i < 9; i++)        {            for(int j = 0; j < 9; j++)            {                char c;                int d;                cin >> c;                d=(int)(c-'0');//输入字符转化为整数储存                if(d == 0)                {                    blankpos.push_back(pos(i,j));//注意返回值的用法                }                else                {                    setall(i,j,d,1);                }            }        }        if(dfs(blankpos.size()-1))        {            for(int i = 0; i < 9; i++)            {                for(int j = 0; j < 9; j++)                {                    cout << board[i][j];                }                cout << endl;            }        }    }    return 0;}</span>


1 0
原创粉丝点击