POJ2676 Sudoku、POJ2918 Tudoku(数独(一))

POJ2676 Sudoku

题目链接

Description

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task. 

Input

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

Output

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

Sample Input

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

给定一个由3*3的方块分割而成的9*9的格子。其中一些格子中填有1~9的数字,其余格子则是空白的。请在空白的格子中填入1~9的数字,使得在每行、每列和每个3*3的方块中,1~9的每个数字都恰好出现一次,如果解不唯一,输出任意一组即可。

考虑从左上角的空白格子开始填数字的深度优先搜索。所填的数字应该是所在行、列和方块中都没有填过的数字。

一开始利用函数bool isok(int x,int y,char num)计算数字num是否在可填在第(x,y)格中 ,结果超时了

超时代码:

//CSDN博客:https://blog.csdn.net/qq_40889820
#include<iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<string>
#include<cstring>
#include<iomanip>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define random(a,b) (rand()%(b-a+1)+a)
#define e 2.71828182
#define Pi 3.141592654
using namespace std;
char table[10][10];
bool isok(int x,int y,char num)//数字num是否在可填在第(x,y)格中 
{
	for(int j=1;j<=9;j++)//行
		if(table[x][j]==num) return false; 
	for(int i=1;i<=9;i++)//列 
		if(table[i][y]==num) return false;
	for(int i=(x-1)/3*3+1;i<(x-1)/3*3+1+3;i++)//3*3的方块 
		for(int j=(y-1)/3*3+1;j<(y-1)/3*3+1+3;j++)
			if(table[i][j]==num) return false;	
	return true;
}
bool flag=false;
void dfs(int x,int y)//填第(x,y)格 
{
	if(flag) return;
	if(y>9) y=1,x+=1;
	if(x>9)
	{
		for(int i=1;i<=9;i++)
		cout<<table[i]+1<<endl;
		flag=true;
		return;
	}
	if(table[x][y]!='0') dfs(x,y+1);
	else if(table[x][y]=='0') 
	{
		for(char i='1';i<='9';i++)
		{
			if(isok(x,y,i))
			{
				table[x][y]=i;
				dfs(x,y+1);
				table[x][y]='0';//回溯 
			}
			//if(i=='9') return; 
		}
	}
}
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		for(int i=1;i<=9;i++)
		cin>>table[i]+1;
		
		dfs(1,1);
		cout<<endl;
		flag=false;
	}
}

后来改为用三个数组保存数字的重复情况,然后就神奇的过了,也算是空间换时间了吧

row[i][j]为1代表第i行数字j已经出现过 ,col[i][j]为1代表第i列数字j已经出现过,grid[i][j]为1代表第i个方块数字j已经出现过(从上往下从左往右数)

AC代码:

//CSDN博客:https://blog.csdn.net/qq_40889820
#include<iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<string>
#include<cstring>
#include<iomanip>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define random(a,b) (rand()%(b-a+1)+a)
#define e 2.71828182
#define Pi 3.141592654
using namespace std;
char table[10][10];
int row[10][10],col[10][10],grid[10][10];
bool flag=false;
void dfs(int x,int y)//填第(x,y)格 
{
	if(flag) return;//已找到一种解法 
	if(y>9) y=1,x+=1;
	if(x>9)
	{
		for(int i=1;i<=9;i++)
		cout<<table[i]+1<<endl;
		flag=true;
		return;
	}
	if(table[x][y]!='0') dfs(x,y+1);
	else if(table[x][y]=='0') 
	{
		int ord=(x-1)/3*3+(y-1)/3+1;//第几个方块 
		for(int i=1;i<=9;i++)
		{
			if(!row[x][i]&&!col[y][i]&&!grid[ord][i])
			{
				table[x][y]=i+'0';
				row[x][i]=1;col[y][i]=1;grid[ord][i]=1;
				dfs(x,y+1);
				//回溯 
				table[x][y]='0';
				row[x][i]=0;col[y][i]=0;grid[ord][i]=0;
			}
		}
	}
}
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		mem(row,0);mem(col,0);mem(grid,0);
		for(int i=1;i<=9;i++)
		{
			cin>>table[i]+1;
			for(int j=1;j<=9;j++)
			{
				row[i][table[i][j]-'0']=1;
				col[j][table[i][j]-'0']=1;
				int ord=(i-1)/3*3+(j-1)/3+1;
				grid[ord][table[i][j]-'0']=1;
			}
		}
		
		dfs(1,1);
		cout<<endl;
		flag=false;
	}
}

POJ2918

题目链接

和POJ2676大致相同,输出结果有着细微的区别

for(int k=1;k<=n;k++)
{
	mem(row,0);mem(col,0);mem(grid,0);
	for(int i=1;i<=9;i++)
	{
		cin>>table[i]+1;
		for(int j=1;j<=9;j++)
		{
			row[i][table[i][j]-'0']=1;
			col[j][table[i][j]-'0']=1;
			int ord=(i-1)/3*3+(j-1)/3+1;
			grid[ord][table[i][j]-'0']=1;
		}
	}
		
	cout<<"Scenario #"<<k<<":\n";
	dfs(1,1);
	cout<<endl;
	flag=false;
}

猜你喜欢

转载自blog.csdn.net/qq_40889820/article/details/86681207