POJ2676 Sudoku - Dancing Links


Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11558 Accepted: 5735 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<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<algorithm>

using namespace std;
//   列:(行+列+块)*9种可能+9*9个格子
//   行: 9*9*9  表示第i行第j列填k
const int MAXN=(9+9+9)*9+9*9+9*9*9*9*9*4+10;
#define INF 0xFFFFFF
int size;
int head,sz;
int U[MAXN],D[MAXN],L[MAXN],R[MAXN];
int H[MAXN],ROW[MAXN],C[MAXN],S[MAXN],O[MAXN];

void remove(int c)
{
    L[R[c]]=L[c];
    R[L[c]]=R[c];
    for(int i=D[c];i!=c;i=D[i])
    {
        for(int j=R[i];j!=i;j=R[j])
        {
            U[D[j]]=U[j];
            D[U[j]]=D[j];
            --S[C[j]];
        }
     }
}

void resume(int c)
{
    for(int i=U[c];i!=c;i=U[i])
    {
        for(int j=L[i];j!=i;j=L[j])
        {
            ++S[C[j]];
            U[D[j]]=j;
            D[U[j]]=j;
          }
     }
     L[R[c]]=c;
     R[L[c]]=c;
}

bool dfs(int k)
{
    if(R[head]==head)
    {
        sort(O,O+9*9);
        int p=0;
        for(int i=0;i<9;i++)
        {
            for(int j=0;j<9;j++)
            {
                int num=O[p++];
                //cout<<num<<endl;
                num=num-(i*9+j)*9;
                printf("%d",num);
            }
            printf("\n");
        }
        printf("\n");
        return  true;
    }
    int s=INF,c;
    for (int t=R[head];t!=head;t=R[t])
    {
        if (S[t]<s)
        {
            s=S[t];
            c=t;
        }
     }
     remove(c);
     for(int i=D[c];i!=c;i=D[i])
     {
          O[k]=ROW[i];
          for(int j=R[i];j!=i;j=R[j])
              remove(C[j]);
          if(dfs(k+1))
               return  true;
          for(int j=L[i];j!=i;j=L[j])
               resume(C[j]);
     }
     resume(c);
     return  false;
}

void initDL(int n)
{
    head=0;
    for(int i=0;i<=n;i++)
    {
        U[i]=i;D[i]=i;
        L[i]=i-1;R[i]=i+1;
        S[i]=0;
    }
    R[n]=0;L[0]=n;S[0]=INF+1;
    sz=n+1;
    memset(H,0,sizeof(H));
}

void insert(int i, int j)
{
    if(H[i])
    {
        L[sz]=L[H[i]];
        R[sz]=H[i];
        L[R[sz]]=sz;
        R[L[sz]]=sz;
    }
    else
    {
        L[sz]=sz;
        R[sz]=sz;
        H[i]=sz;
    }
    U[sz]=U[j];
    D[sz]=j;
    U[D[sz]]=sz;
    D[U[sz]]=sz;
    C[sz]=j;
    ROW[sz]=i;
    ++S[j];
    ++sz;
}

char str[200][200];

void build()
{
    initDL(9*9*4);
    for(int i=0;i<9;i++)
        for(int j=1;j<=9;j++)
        {
            int base=(i*9+j-1)*9;
            if(str[i][j-1]=='0')
            {
                for(int k=1;k<=9;k++)
                {
                    int r;
                    r=base+k;
                    //第i行有数字k
                    insert(r,i*9+k);
                    //第j列有数字k
                    insert(r,9*9+(j-1)*9+k);
                    //第k块有数字k
                    int block=(j-1)/3*3+i/3;
                    insert(r,9*9*2+block*9+k);
                    //第i行j列有一个数字(限制一个格子只填一个数)
                    insert(r,9*9*3+i*9+j);
                }
            }
            else
            {
                int k=str[i][j-1]-'0';
                int r=base+k;
                //第i行有数字k
                insert(r,i*9+k);
                //第j列有数字k
                insert(r,9*9+(j-1)*9+k);
                //第k块有数字k
                int block=(j-1)/3*3+i/3;
                insert(r,9*9*2+block*9+k);
                //第i行j列有一个数字(限制一个格子只填一个数)
                insert(r,9*9*3+i*9+j);
            }
        }
}

int main()
{
    int t;
    size=9; //9*9数独
    scanf("%d",&t);
    gets(str[0]);
    while(t--)
    {
        for(int i=0;i<9;i++)
            gets(str[i]);
        build();
        dfs(0);
    }
    return 0;
}


基于SSM框架的智能家政保洁预约系统,是一个旨在提高家政保洁服务预约效率和管理水平的平台。该系统通过集成现代信息技术,为家政公司、家政服务人员和消费者提供了一个便捷的在线预约和管理系统。 系统的主要功能包括: 1. **用户管理**:允许消费者注册、登录,并管理他们的个人资料和预约历史。 2. **家政人员管理**:家政服务人员可以注册并更新自己的个人信息、服务类别和服务时间。 3. **服务预约**:消费者可以浏览不同的家政服务选项,选择合适的服务人员,并在线预约服务。 4. **订单管理**:系统支持订单的创建、跟踪和管理,包括订单的确认、完成和评价。 5. **评价系统**:消费者可以在家政服务完成后对服务进行评价,帮助提高服务质量和透明度。 6. **后台管理**:管理员可以管理用户、家政人员信息、服务类别、预约订单以及处理用户反馈。 系统采用Java语言开发,使用MySQL数据库进行数据存储,通过B/S架构实现用户与服务的在线交互。系统设计考虑了不同用户角色的需求,包括管理员、家政服务人员和普通用户,每个角色都有相应的权限和功能。此外,系统还采用了软件组件化、精化体系结构、分离逻辑和数据等方法,以便于未来的系统升级和维护。 智能家政保洁预约系统通过提供一个集中的平台,不仅方便了消费者的预约和管理,也为家政服务人员提供了一个展示和推广自己服务的机会。同时,系统的后台管理功能为家政公司提供了强大的数据支持和决策辅助,有助于提高服务质量和管理效率。该系统的设计与实现,标志着家政保洁服务向现代化和网络化的转型,为管理决策和控制提供保障,是行业发展中的重要里程碑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值