POJ 2918 Tudoku 【DFS】

30 篇文章 0 订阅

Tudoku
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2447 Accepted: 1356

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.

753284691
482916537
196753842
931 6 425
275491386
648 32179
567349218
824175963
319628754

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

2
000000000
817965430
652743190
175439820
308102950
294856370
581697240
903504610
746321580

781654392
962837154
543219786
439182675
158976423
627543918
316728549
895461237
274395861

Sample Output

Scenario #1:
439218765
817965432
652743198
175439826
368172954
294856371
581697243
923584617
746321589

Scenario #2:
781654392
962837154
543219786
439182675
158976423
627543918
316728549
895461237
274395861


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
#define ll long long
#define ms(a,b)  memset(a,b,sizeof(a))
const int M=1e2+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int i,j,k,n,m;
int mp[M][M];
int ans[M][M];
bool flag;

bool ok(int n,int cur)
{
    int r=n/9;
    int c=n%9;
    for(int j=0;j<9;j++)
        if(mp[r][j]==cur)
        return false;
    for(int i=0;i<9;i++)
        if(mp[i][c]==cur)
        return false;
    int x=r/3*3;
    int y=c/3*3;
    for(int i=x;i<x+3;i++)
    for(int j=y;j<y+3;j++){
        if(mp[i][j]==cur)
            return false;
    }
    return true;
}

void dfs(int n)
{
   if(n>80){
    flag=1;
    return ;
   }
   if(mp[n/9][n%9]){
     dfs(n+1);
     if(flag)return;
   }
   else {
    for(int i=1;i<=9;i++){
        if(ok(n,i)){
            mp[n/9][n%9]=i;
            dfs(n+1);
            if(flag)return;
            mp[n/9][n%9]=0;
        }
    }
   }
}

int main()
{
    int T;
    int cas=1;
    scanf("%d",&T);
    while(T--){
        for(int i=0;i<9;i++){
            char s[100];
            scanf("%s",s);
            for(int j=0;j<9;j++)
                mp[i][j]=s[j]-'0';
        }
        flag=0;
        dfs(0);
        printf("Scenario #%d:\n",cas++);
        for(int i=0;i<9;i++)
        for(int j=0;j<9;j++){
            printf("%d",mp[i][j]);
            if(j==8)printf("\n");
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值