HDU

来源:互联网 发布:中铁采购网络交易平台 编辑:程序博客网 时间:2024/06/05 11:03
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×44×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×22×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(1≤T≤100)T(1≤T≤100). TT 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 xx 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

题意:让你用1,2,3,4去填上*的位置,要求每行和每列都没有重复的,你根据样例会发现,把数列分成4部分,各部分都没有相同的数字,所以判断根据数字的位置,去判断一下。

#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>using namespace std;int dp[5][5];bool decide (int x, int y, int va) // 判断是否有重复{    for(int i = 1; i <= 4; i++)    {        if(dp[x][i] == va)            return 0;        if(dp[i][y] == va)            return 0;    }    if(x <= 2 && y <= 2)    {        for(int i = 1; i <= 2; i++)            for(int j = 1; j <= 2; j++)                if(dp[i][j] == va)                    return 0;    }    if(x >= 3 && y <= 2)    {        for(int i = 3; i <= 4; i++)        {            for(int j = 1; j <= 2; j++)                if(dp[i][j] == va)                    return 0;        }    }    if(x >= 3 && y >= 3)    {        for(int i = 3; i <= 4; i++)        {            for(int j = 3; j <= 4; j++)                if(dp[i][j] == va)                    return 0;        }    }    if(x <= 2 && y>= 3)    {        for(int i = 1; i <= 2; i++)        {            for(int j = 3; j <= 4; j++)                if(dp[i][j] == va)                    return 0;        }    }    return 1;}void dfs(int x,int y){    if(x == 5) // 遍历完成,输出结果    {         for(int i = 1; i <= 4; i++)    {        for(int j = 1; j <= 4; j++)            printf("%d", dp[i][j]);        printf("\n");    }        return;    }    if(dp[x][y]) // 如果这个不是未知数就遍历下一个数字,注意每行遍历完,遍历下一行    {        if(y == 4)            dfs(x + 1, 1);        else            dfs(x, y + 1);    }    else    {        for(int i = 1; i <= 4; i++) // 判断下如果是未知数,填写哪个数符合要求        {            if(decide(x, y, i))            {                dp[x][y] = i;                if(y == 4)                    dfs(x + 1, 1);                else                    dfs(x, y + 1);                dp[x][y] = 0; // 回溯            }        }    }}int main(){    int t;    scanf("%d", &t);    char ch;      for(int k = 1; k <= t; k++) // 讲字符串改成数字,记住不要用getchar()去输入ch,不知道为什么错误,一直为什么错误,后来用cin就对了,很难受。    {        for(int i = 1; i <= 4; i++)        {                       for(int j = 1; j <= 4; j++)            {                cin >> ch;                if(ch == '*')                    dp[i][j] = 0;                else                    dp[i][j] = ch - '0';            }        }    printf("Case #%d:\n", k);    dfs(1, 1); // 遍历开始    }    return 0;}

0 0
原创粉丝点击