2015ccpc——H

来源:互联网 发布:尚观云计算课程 编辑:程序博客网 时间:2024/06/03 21:58
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


题意:每一行、每一列、还有四个小的2*2的小矩阵中用1,2,3,4去填写,不能重复

解题思路:直接dfs判断就好了。

#include <iostream>#include <cstdio>#include <cstring>using namespace std;char maze[5][5];int judge(int x,int y,char c){    for(int i=0;i<4;i++)//判断行、列    {        if(maze[x][i]==c||maze[i][y]==c)            return 0;    }    if(x/2==0&&y/2==0)//判断左上    {        if(maze[0][0]==c||maze[0][1]==c||maze[1][0]==c||maze[1][1]==c)            return 0;    }    else if(x/2==0&&y/2==1)//判断右上    {        if(maze[0][2]==c||maze[0][3]==c||maze[1][2]==c||maze[1][3]==c)            return 0;    }    else if(x/2==1&&y/2==0)//判断左下    {        if(maze[2][0]==c||maze[2][1]==c||maze[3][0]==c||maze[3][1]==c)            return 0;    }    else if(x/2==1&&y/2==1)//判断右下    {        if(maze[2][2]==c||maze[2][3]==c||maze[3][2]==c||maze[3][3]==c)            return 0;    }    return 1;}void dfs(int x,int y){    if(x==4)    {        for(int i=0;i<4;i++)        {            printf("%s\n",maze[i]);        }        return ;    }    else    {        if(maze[x][y]!='*')        {            if(y==3)//该行的最后一列            {                dfs(x+1,0);            }            else                dfs(x,y+1);        }        else        {            for(int i=1;i<=4;i++)            {                if(judge(x,y,i+'0'))//每个数去尝试,可以就放                {                    maze[x][y]=i+'0';                    if(y==3)                        dfs(x+1,0);                    else                        dfs(x,y+1);                    maze[x][y]='*';//回溯                }            }        }    }}int main(){    int t,cas=1;    scanf("%d",&t);    while(t--)    {        for(int i=0;i<4;i++)            scanf("%s",maze[i]);        printf("Case #%d:\n",cas++);        dfs(0,0);    }    return 0;}


原创粉丝点击