POJ 2676:Sudoku 数独

Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15830   Accepted: 7737   Special Judge

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

数独填数,深搜+暴力。

自己也优化了很多,结果一直TLE。当然还没有优化够,比方说按照可供选择的多少排序,从少的开始深搜。但觉得太麻烦了。看得TLE快绝望了,结果看discuss要从后面搜,果然。。。

但是这个题目的数据给得也是够了,没有道理前面搜TLE,后面搜16ms的啊。。。真的是

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;

char chess[15][15];
char test[15][15];
int m,n,flag;
vector<char>kefang[15][15];

bool pend(int row,int col,char value)
{
	int i,j,temp1,temp2;
	if(row%3==0)
		temp1=row/3;
	else
		temp1=row/3+1;
	if(col%3==0)
		temp2=col/3;
	else
		temp2=col/3+1;
	for(i=(temp1-1)*3+1;i<=temp1*3;i++)
	{
		for(j=(temp2-1)*3+1;j<=temp2*3;j++)
		{
			if(i==row&&j==col)continue;
			if(value==test[i][j])return false;
		}
	}

	for(i=1;i<=9;i++)
	{
		if( i!=col && value==test[row][i])
			return false;
	}
	for(i=1;i<=9;i++)
	{
		if( i!=row && value==test[i][col])
			return false;
	}

	return true;
}

void find(int i_r,int j_r)
{
	if(j_r==1)
	{
		for(m=i_r-1;m>=1;m--)
		{
			for(n=9;n>=1;n--)
			{
				if(chess[m][n]=='0')
					return;
			}
		}
	}
	else
	{
		m=i_r;
		for(n=j_r-1;n>=1;n--)
		{
			if(chess[m][n]=='0')
				return;
		}
		for(m=i_r-1;m>=1;m--)
		{
			for(n=9;n>=1;n--)
			{
				if(chess[m][n]=='0')
					return;
			}
		}
	}
	m=0;
	n=0;
}

void dfs(int i,int j,char u)
{
	if(flag)
		return;
	test[i][j]=u;

	if(i<=1&&j<=1)
	{
		int h,k;
		for(h=1;h<=9;h++)
		{
			for(k=1;k<=9;k++)
			{
				chess[h][k]=test[h][k];
			}
		}
		flag=1;
		return;
	}

	find(i,j);
	char temp_c;
	int m_temp=m;
	int n_temp=n;
	int size=kefang[m_temp][n_temp].size();
	int xu;
	for(xu=0;xu<size;xu++)
	{
		temp_c=kefang[m_temp][n_temp][xu];
		if(pend(m_temp,n_temp,temp_c))
		{
			dfs(m_temp,n_temp,temp_c);
		}
	}
	if(m==0&&n==0)
	{
		int h,k;
		for(h=1;h<=9;h++)
		{
			for(k=1;k<=9;k++)
			{
				chess[h][k]=test[h][k];
			}
		}
		flag=1;
		return;
	}

	test[i][j]='0';
}

void solve()
{		
	char temp_c;
	int i,j;
	for(i=9;i>=1;i--)
	{
		for(j=9;j>=1;j--)
		{
			if(chess[i][j]=='0')
			{
				int size=kefang[i][j].size();
				int xu;
				for(xu=0;xu<size;xu++)
				{
					temp_c=kefang[i][j][xu];
					if(pend(i,j,temp_c))
					{
						dfs(i,j,temp_c);
						if(flag)
							return;
					}
				}
			}
		}
	}

}

void init()
{
	int g,d;
	for(g=1;g<=9;g++)
	{
		for(d=1;d<=9;d++)
		{
			char temp_c;
			for(temp_c='1';temp_c<='9';temp_c++)
			{
				if(pend(g,d,temp_c))
				{
					kefang[g][d].push_back(temp_c);
				}
			}
		}
	}
}

int main()
{
	int Test,i,j;
	scanf("%d",&Test);

	while(Test--)
	{
		for(i=1;i<=9;i++)
		{
			scanf("%s",chess[i]+1);
			for(j=1;j<=9;j++)
			{
				test[i][j]=chess[i][j];
			}
		}
		for(i=1;i<=9;i++)
			for(j=1;j<=9;j++)
				kefang[i][j].clear();
		flag=0;
		init();
		solve();
		for(i=1;i<=9;i++)
		{
			cout<<chess[i]+1<<endl;
		}
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2015-07-31 15:02  光速小子  阅读(212)  评论(0)    收藏  举报

导航