poj 2918

来源:互联网 发布:网站域名交易流程 编辑:程序博客网 时间:2024/06/02 03:58

       还是一道关于数独搜索的题,用之前那个方法(poj 2676),可以很快解决。

代码(C++):

#include <iostream>#include <cstdio>#define MAX 100using namespace std;//#define LOCALstruct Node{    int x;    int y;} node[MAX];int c;char matrix[10][10];bool check(int x,int y,int n){    int i,j,a,b;    for(i=0;i<9;i++)    {        if(matrix[x][i]=='0'+n) return false;        if(matrix[i][y]=='0'+n) return false;    }    a=x/3;    b=y/3;    for(i=a*3;i<a*3+3;i++)    {        for(j=b*3;j<b*3+3;j++)        {            if(matrix[i][j]=='0'+n) return false;        }    }    return true;}bool dfs(int num){    int i,x,y;    if(num>=c) return true;    x=node[num].x;    y=node[num].y;    for(i=1;i<=9;i++)    {        if(check(x,y,i))        {            matrix[x][y]=i+'0';            if(dfs(num+1)) return true;            matrix[x][y]='0';        }    }    return false;}int main(){#ifdef LOCAL    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);#endif    int t,i,j,k;    scanf("%d",&t);    for(k=1;k<=t;k++)    {        c=0;        for(i=0;i<9;i++)        {            scanf("%s",matrix[i]);            for(j=0;j<9;j++)            {                if(matrix[i][j]=='0')                {                    node[c].x=i;                    node[c++].y=j;                }            }        }        dfs(0);        printf("Scenario #%d:\n",k);        for(i=0;i<9;i++) printf("%s\n",matrix[i]);        printf("\n");    }    return 0;}

题目(http://poj.org/problem?id=2918):

Tudoku
Time Limit: 1000MS Memory Limit: 65536K   

Description

Tom is a master in several mathematical-theoretical disciplines. He recently founded a research-lab at our university and teaches newcomers like Jim. In the first lesson he explained the game of Tudoku to Jim. Tudoku is a straight-forward variant of Sudoku, because it consists of a board where almost all the numbers are already in place. Such a board is left over when Tom stops solving an ordinary Sudoku because of being too lazy to fill out the last few straight-forward cells. Now, you should help Jim solve all Tudokus Tom left for him.

Sudoku is played on a 9 × 9 board that is divided into nine different 3 × 3 blocks. Initially, it contains only a few numbers and the goal is to fill the remaining cells so that each row, column, and 3 × 3 block contains every number from 1 to 9. This can be quite hard but remember that Tom already filled most cells. A resulting Tudoku board can be solved using the following rule repeatedly: if some row, column or 3 × 3 block contains exactly eight numbers, fill in the remaining one.

In the following example, three cells are still missing. The upper left one cannot be determined directly because neither in its row, column, or block, there are eight numbers present. The missing number for the right cell can be determined using the above rule, however, because its column contains exactly eight numbers. Similarly, the number for the lower-most free cell can be determined by examining its row. Finally, the last free cell can be filled by either looking at its row, column or block.

753284691482916537196753842931 6 425275491386648 32179567349218824175963319628754

Input

The first line contains the number of scenarios. For each scenario the input contains nine lines of nine digits each. Zeros indicate the cells that have not been filled by Tom and need to be filled by you. Each scenario is terminated by an empty line.

Output

The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then, print the solved Tudoku board in the same format that was used for the input, except that zeroes are replaced with the correct digits. Terminate the output for the scenario with a blank line.

Sample Input

2000000000817965430652743190175439820308102950294856370581697240903504610746321580781654392962837154543219786439182675158976423627543918316728549895461237274395861

Sample Output

Scenario #1:439218765817965432652743198175439826368172954294856371581697243923584617746321589Scenario #2:781654392962837154543219786439182675158976423627543918316728549895461237274395861

0 0