Sudoku HDU - 5547(dfs暴力搜)

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×2pieces, 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

Solution:

没啥好说的,直接dfs暴搜就行了,代码里面的注释会详细解释。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

bool flag;
int gra[20][20];

bool check(int r1, int r2, int c1, int c2, int val)  //判断小矩阵的合法性
{
	for(int i = r1; i <= r2; ++ i)
	{
		for(int j = c1; j <= c2; ++ j)
		{
			if(gra[i][j] == val)
				return false;
		}
	}
	return true;
}

bool judge(int x, int y, int val)
{
	for(int i = 1; i <= 4; ++ i)   //判断行列中是否有和val相同的数
	{
		if(gra[x][i] == val)
			return false;
		if(gra[i][y] == val)
			return false;
	}
    //检查四个小矩阵的合法性
	if(x >= 1 && x <= 2 && y >= 1 && y <= 2 && !check(1, 2, 1, 2, val))
		return false;
	if(x >= 1 && x <= 2 && y >= 3 && y <= 4 && !check(1, 2, 3, 4, val))
		return false;
	if(x >= 3 && x <= 4 && y >= 1 && y <= 2 && !check(3, 4, 1, 2, val))
		return false;
	if(x >= 3 && x <= 4 && y >= 3 && y <= 4 && !check(3, 4, 3, 4, val))
		return false;
	return true;
}

void dfs(int x, int y)
{
	if(flag)
		return ;
	if(x > 4)            //如果全部找完了直接输出
	{
		flag = true;
		for(int i = 1; i <= 4; ++ i)
		{
			for(int j = 1; j <= 4; ++ j)
			{
				cout << gra[i][j];
			}
			cout << endl;
		}
		return ;
	}
	if(gra[x][y])           //如果这个位置不是未知数,那么跳过
	{
		if(y >= 4)
			dfs(x + 1, 1);
		else 
			dfs(x, y + 1);
	}
	else 
	{
		for(int i = 1; i <= 4; ++ i)     //枚举1~4的所有情况
		{
			if(judge(x, y, i))         //判断是否合法
			{
				gra[x][y] = i;
				if(y >= 4)
					dfs(x + 1, 1);
				else 
					dfs(x, y + 1);
				gra[x][y] = 0;        //回溯时在把值变回来
			}
		}
	}
}

int main()
{
	//freopen("in.txt", "r", stdin);
	int t, Case = 0;
	cin >> t;
	while(t --)
	{
		char ch;
		for(int i = 1; i <= 4; ++ i)
		{
			for(int j = 1; j <= 4; ++ j)
			{
				cin >> ch;
				if(ch != '*')                 //转化成数字存下来
					gra[i][j] = ch - '0';
				else 
					gra[i][j] = 0;
			}
		}
		printf("Case #%d:\n", ++Case);
		flag = false;
		dfs(1, 1);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/aqa2037299560/article/details/85242232