uestc 1222 Sudoku 搜索

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<bits/stdc++.h>
using namespace std;

int g[200][200];
int r[200][200];
int c[200][200];
int mp[200][200];
char s[200][200],flag;

int fnc(int x,int y)
{
    if(x<2)
    {
        if(y<2)
            return 0;
        else
            return 1;
    }
    else
    {
        if(y<2)
            return 2;
        else
            return 3;

    }
}

void dfs(int x,int y)
{
    if(flag==1) return ;
    int i,j,m;
    if(x==4)
    {
        for(i=0; i<4; i++)
        {
            for(j=0; j<4; j++)
                printf("%d",g[i][j]);
            printf("\n");
        }
        flag=1;
        return ;
    }
    else if(y>=4)  dfs(x+1,0);
    else if(g[x][y]!=-1) dfs(x,y+1);
    else
    {

        m=fnc(x,y);
        for(i=1; i<=4; i++)
        {
            if(r[x][i]==0&&c[y][i]==0&&mp[m][i]==0)
            {
                g[x][y]=i;
                mp[m][i]=r[x][i]=c[y][i]=1;
                dfs(x,y+1);
                if(flag) return ;
                g[x][y]=-1;
                mp[m][i]=r[x][i]=c[y][i]=0;
            }
        }
    }
    return ;
}

int main()
{
    int _,i,j,t,cas=0;
    scanf("%d",&_);
    while(_--)
    {
        for(i=0; i<4; i++)
            scanf("%s",s[i]);
        memset(r,0,sizeof(r));
        memset(c,0,sizeof(c));
        memset(mp,0,sizeof(mp));
        for(i=0; i<4; i++)
            for(j=0; j<4; j++)
            {
                if(s[i][j]=='*') g[i][j]=-1;
                else
                {
                    t=s[i][j]-'0';
                    mp[fnc(i,j)][t]=r[i][t]=c[j][t]=1;
                    g[i][j]=t;
                }
            }
        printf("Case #%d:\n",++cas);
        flag=0;
        dfs(0,0);
    }
    return 0;
}

你可能感兴趣的:(uestc 1222 Sudoku 搜索)