ACM: ICPC/CCPC Sudoku DFS - 数独

Sudoku

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other)
Total Submission(s) : 19   Accepted Submission(s) : 5

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 [b]Case #x:[/b], where $x$ 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  

 

Source The 2015 China Collegiate Programming Contest    

/*/
最进学校的课真是让人崩溃啊,作业都快做不过来了,挤个下午刷刷题目,却被一个做过的题目卡了好久,我的天啊。。

题意:
这个题目就是数独,但是是4*4的数独。

补全这个数独。。

一开始少读了一句 2x2 也要保持独立性,WA了一发。。。

AC代码:
/*/

#include "iostream"
#include "cstdio"
#include "cstring"
#include "string"
#include "cmath"
using namespace std;
typedef long long LL;
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(x))

const int MX = 1e5 + 100 ;
int _;
int vix[5][5] ,viy[5][5],IXI[3][3][5],maps[5][5];
char tem[5][5];
int flag;

void _X_() {
	puts("");
	puts("");
	puts("");
}

void Print() {
	for(int i=0; i<4; i++) {
		for(int j=0; j<4; j++) {
			printf("%d",maps[i][j]);
		}
		puts("");
	}
}

void init() {
	flag=0;
	_=0;
	memset(vix,0);
	memset(viy,0);
	memset(IXI,0);
}

int DFS(int i,int j,int __) {
	if(__==_) {
		return flag=1;
	}
	if(tem[i][j]=='*') {
		for(int k=1; k<=4; k++) {
			if(vix[j][k]||viy[i][k]||flag||IXI[i/2][j/2][k])continue;
//			cout<< __ <<"["<<k<<"]";
			maps[i][j]=k;
			vix[j][k]=1;
			viy[i][k]=1;
			IXI[i/2][j/2][k]=1;
			if(j<3) DFS(i,j+1,__+1);
			else if(i<3) DFS(i+1,0,__+1);
			else if(i==3&&j==3) {
				DFS(3,3,__+1);
			}
			if(flag)break;
			maps[i][j]=0;
			vix[j][k]=0;
			viy[i][k]=0;
			IXI[i/2][j/2][k]=0;
		}
	}
	if(j<3) DFS(i,j+1,__);
	else if(i<3) DFS(i+1,0,__);
	return 0;
}

int main() {
	int T;
	while(~scanf("%d",&T)) {
		for(int qq=1; qq<=T; qq++) {
			init();
			for(int i=0; i<4; i++) {
				cin>>tem[i];
				for(int j=0; j<4; j++) {
					if(tem[i][j]=='*') {
						maps[i][j]=0;
						_++;
					} else {
						maps[i][j]=tem[i][j]-'0';
						vix[j][maps[i][j]]=1;
						viy[i][maps[i][j]]=1;
						IXI[i/2][j/2][maps[i][j]]=1;
					}
				}
			}
//			cout<<_<<endl;
//			_X_();
			DFS(0,0,0);
			printf("Case #%d:\n",qq);
			Print();
//			_X_();
		}
	}
	return 0;
}

  

 

 

    原文作者:~HDMaxfun
    原文地址: https://www.cnblogs.com/HDMaxfun/p/5868983.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞