Sudoku - ccpc2015 && UESTC 1222

来源:互联网 发布:虚拟穿衣软件 编辑:程序博客网 时间:2024/06/08 06:39
Sudoku
Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)


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(1≤T≤100). 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 and output
Sample Input Sample Output
3


****
2341
4123
3214


*243
*312
*421
*134


*41*
**3*
2*41

4*2*


Case #1:
1432
2341
4123
3214
Case #2:
1243
4312
3421
2134
Case #3:
3412
1234
2341
4123
Source

The 2015 China Collegiate Programming Contest

//直接dfs搜索,用三个标记数组,分别标记行,列,块中已使用的数字,当前位置填入没有被标记的数字。若矛盾不能填,返回上一步#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;int mp[10][10];bool found, row[10][10], col[10][10], piece[10][10]; //标记行,列,块void dfs(int x, int y){    if(x == 4 && y == 0) //走到这里说明正确填出    {        found = true;        return;    }    int t; //用来确定当前位置所在的块    if(x == 0 || x == 1)    {        if(y == 0 || y == 1) t = 1;        else t = 2;    }    if(x == 2 || x == 3)    {        if(y == 0 || y == 1) t = 3;        else t = 4;    }    if(mp[x][y] == 0)    {        for(int i = 1; i <= 4; i++)        {            if(row[x][i] == false && col[y][i] == false && piece[t][i] == false) //必须行列块都没使用过            {                mp[x][y] = i;                row[x][i] = col[y][i] = piece[t][i] = true;                if(y == 3) dfs(x + 1, 0);                else dfs(x, y + 1);                if(found == true) return; //已正确填出,直接返回                                //清除标记                mp[x][y] = 0;                row[x][i] = col[y][i] = piece[t][i] = false;            }        }    }    else    {        if(y == 3) dfs(x + 1, 0);        else dfs(x, y + 1);    }}int main(){    int t, x = 0;    char s[10][10];    scanf("%d", &t);    while(t--)    {        for(int i = 0; i < 4; i++)           scanf(" %s", s[i]);        found = false;        memset(mp, 0, sizeof mp);        memset(row, false, sizeof row);        memset(col, false, sizeof col);        memset(piece, false, sizeof piece);        for(int i = 0; i < 4; i++)            for(int j = 0; j < 4; j++)                if(s[i][j] != '*')                {                    mp[i][j] = s[i][j] - '0';                    row[i][ mp[i][j] ] = true; //标记行中已使用的数字                    col[j][ mp[i][j] ] = true; //标记列中已使用的数字                    if(i == 0 || i == 1) //划分成四块,分别标记为1,2, 3, 4                    {                        if(j == 0 || j == 1) piece[1][ mp[i][j] ] = true;                        else piece[2][ mp[i][j] ] = true;                    }                    if(i == 2 || i == 3)                    {                        if(j == 0 || j == 1) piece[3][ mp[i][j] ] = true;                        else piece[4][ mp[i][j] ] = true;                    }                }        dfs(0, 0);        printf("Case #%d:\n", ++x);        for(int i = 0; i < 4; i++)        {            for(int j = 0; j < 4; j++)                printf("%d", mp[i][j]);            printf("\n");        }    }    return 0;}


0 0