Sudoku HDU - 5547 (暴力dfs)详解

 

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(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.

 

就是简单的4*4的数独填空,不过不同的是这个数独斜对角线上的数字可以相同,但四个2*2的小方块里不能有相同的数字

非常裸的dfs。下面上代码


#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <stdio.h>
#include <map>
using namespace std;
#define LL long long
const int MAX = 1e5 + 50;
char arr[5][5]; 
int col[5][5]; // col[1][3]表示第一列3这个数字是否存在
int row[5][5]; // 与上面同理
int four[5][5]; // 从左上逆时针将四个2*2的方块标号为1,2,3,4
int k = 0;
struct Node
{
	int x;
	int y;
} node[20];
void deal(int i, int j, int x, int num){
	row[i][x] = num;
	col[j][x] = num;

	if(i <= 2 && j <= 2){ // 判断属于哪个区域
		four[1][x] = num;
	}
	if(i <= 2 && j > 2){
		four[2][x] = num;
	}

	if(i > 2 && j <= 2){
		four[3][x] = num;
	}

	if(i > 2 && j > 2){
		four[4][x] = num;
	}
}

int isok(int i, int j, int x){ // 判断能不能放
	if(row[i][x] == 1){ 
		return 0;
	}
	if(col[j][x] == 1){
		return 0;
	}

	if(i <= 2 && j <= 2 && four[1][x] == 1){
		return 0;
	}
	if(i <= 2 && j > 2 && four[2][x] == 1){
		return 0;
	}

	if(i > 2 && j <= 2 && four[3][x] == 1){
		return 0;
	}

	if(i > 2 && j > 2 && four[4][x] == 1){
		return 0;
	}

	return 1;
}
int flag = 1;
void dfs(int cnt){
	if(cnt == k){
		flag = 0;
	}
	int x = node[cnt].x;
	int y = node[cnt].y;
	for(int i = 1; i <= 4 && flag; i++){
		if(isok(x, y, i)){
			deal(x, y, i, 1);
			arr[x][y] = i + '0';
			dfs(cnt + 1);
			deal(x, y, i, 0);
		}
	}
}

int main(int argc, char const *argv[])
{
	int n;
	scanf("%d", &n);
	for(int m = 1; m <= n; m++){
		k = 0;
		flag = 1;
		for(int i = 1; i <= 4; i++){ //多组输入,初始化
			for(int j = 1; j <= 4; j++){
				col[i][j] = 0;
				row[i][j] = 0;
				four[i][j] = 0;
			}
		}
		for(int i = 1; i <= 4; i++){
			for(int j = 1; j <= 4; j++){
				scanf(" %c", &arr[i][j]);
				char x = arr[i][j];
				if(x != '*'){
					deal(i, j, x - '0', 1); // 标记
				} else{
					node[k].x = i; // 记录为*的点
					node[k++].y = j;
				}
			}
		}

		dfs(0);
		printf("Case #%d:\n", m);
		for(int i = 1; i <= 4; i++){
			for(int j = 1; j <= 4; j++){
				printf("%c", arr[i][j]);
			}
			printf("\n");
		}
	}
	return 0;
}

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值