hdu5547(深搜)

Problem Description
Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller.

Actually, Yi Sima was playing it different. First of all, he tried to generate a  4×4 board with every row contains 1 to 4, every column contains 1 to 4. Also he made sure that if we cut the board into four  2×2 pieces, every piece contains 1 to 4.

Then, he removed several numbers from the board and gave it to another guy to recover it. As other counselors are not as smart as Yi Sima, Yi Sima always made sure that the board only has one way to recover.

Actually, you are seeing this because you've passed through to the Three-Kingdom Age. You can recover the board to make Yi Sima happy and be promoted. Go and do it!!!
 

Input
The first line of the input gives the number of test cases,  T(1T100) T test cases follow. Each test case starts with an empty line followed by 4 lines. Each line consist of 4 characters. Each character represents the number in the corresponding cell (one of '1', '2', '3', '4'). '*' represents that number was removed by Yi Sima.

It's guaranteed that there will be exactly one way to recover the board.
 

Output
For each test case, output one line containing  Case #x:, where  x is the test case number (starting from 1). Then output 4 lines with 4 characters each. indicate the recovered board.
 

Sample Input
 
  
3 **** 2341 4123 3214 *243 *312 *421 *134 *41* **3* 2*41 4*2*
 

Sample Output
 
  
Case #1: 1432 2341 4123 3214 Case #2: 1243 4312 3421 2134 Case #3: 3412 1234 2341 4123
题意:很明显的数独,行、列、以及四角2*2的矩形内都不能有相同的数。
思路:找出需要填的点,用dfs去填。
代码:
#include<bits/stdc++.h>
using namespace std;
char s[50][50];
int cnt;
struct node//用于存储需要填数字的点
{
    int x;
    int y;
}a[50];
int check(int n,int k)
{
    for(int i=0;i<4;i++)//判断这一列是否出现过k
    {
        if(a[n].x==i) continue;
        if(s[i][a[n].y]-'0'==k) return 0;
    }
    for(int i=0;i<4;i++)//判断这一行是否出现过k
    {
        if(a[n].y==i) continue;
        if(s[a[n].x][i]-'0'==k) return 0;
    }
    for(int i=0;i<2;i++)//判断2*2矩形是否出现过k
    {
        for(int j=0;j<2;j++)//这里表示坐标也可以用一个方向来表示
        {
            int x1=a[n].x/2*2+i;
            int y1=a[n].y/2*2+j;
            if(x1==a[n].x&&y1==a[n].y) continue;
            if(s[x1][y1]-'0'==k) return 0;
        }
    }
    return 1;
}
void dfs(int m)
{
    if(cnt==m)
    {
        for(int i=0;i<4;i++) puts(s[i]);
        return;
    }
    for(int i=1;i<=4;i++)
    {
        if(check(m,i))
        {
            s[a[m].x][a[m].y]='0'+i;
            dfs(m+1);
            s[a[m].x][a[m].y]='0';
        }
    }
    return;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(a,0,sizeof(a));
        for(int i=0;i<4;i++) scanf("%s",s[i]);
        cnt=0;
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                if(s[i][j]=='*')
                {
                    s[i][j]=='0';
                    a[cnt].x=i;
                    a[cnt].y=j;
                    cnt++;//记录需要填的总的点数
                }
            }
        }
        static int t=1;
        printf("Case #%d:\n",t++);
        dfs(0);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/dl962454/article/details/78524821