Sudoku poj2676 (dfs)

Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19520 Accepted: 9359 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


数独问题 ---

利用了step来代替了传统的四个方向遍历后dfs

简洁 但是转化稍微麻烦一点

详见代码


#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>

using namespace std;

#define For(i,a,b) for(i=a;i<=b;i++)
#define _For(i,a,b) for(i=b;i>=a;i--)
#define Out(x) cout<<x<<endl
#define mset(arr,num) memset(arr,num,sizeof(arr))

#define ll long long
const ll inf = 2000; ///
#define ok std::ios::sync_with_stdio(0)

#ifndef SYMBOL
#define SYMBOL value
#endif

#pragma comment(linker, "/STACK:102400000,102400000")
// #define debug
#if defined (debug)
---check---
#endif

///

char mp[11][11];
int shudo[11][11];
bool flag;

struct node
{
	int dx,dy;	
}shudostep[110];  ///81 ~ 1 一格子一格子的来 可以用坐标化来上下左右移动的方法代替 只不过比较麻烦

bool check(int x,int y,int num)  ///检查有没有重复
{ 
	int i,j,k;
	For(i,1,9) ///check row 
	{
		if(shudo[x][i] == num)
		{
			return 0;
		}
	}
	For(i,1,9) ///check line
	{
		if(shudo[i][y] == num)
		{
			return 0;
		}
	}
	int dx = (x-1)/3+1,dy = (y-1)/3+1; ///利用x y相关运算判断在哪个3*3矩阵
	dx*=3;
	dy*=3;
	For(i,dx-2,dx) ///for 3*3
	{
		For(j,dy-2,dy)
		{
			if(shudo[i][j] == num)
			{
				return 0;
			}
		}
	}
	return 1;
}

void dfs(int step)
{
	int i;
	int x,y;

	if(step > 81)
	{
		flag = 1;
		return ;
	}
	x=shudostep[step].dx;
	y=shudostep[step].dy;
	if(shudo[x][y] != 0)  ///如果不是零 搜索下一个位置
	{
		dfs(step+1);
	}
	else
	{
		For(i,1,9)
		{
			if(check(x,y,i))  ///如果数字可以用
			{
				shudo[x][y] = i;  ///填入数字
				dfs(step+1);
				if(flag)
				{
					return ;
				}
				shudo[x][y] = 0;  ///说明前面不符合 重新置为0
			}
		}
	}
}

int main()
{
	int i,j,t;
	cin>>t;
	ok;
	while(t--)
	{
		flag = 0;
		For(i,1,9)
		{
			For(j,1,9)
			{
				cin>>mp[i][j];
				shudo[i][j] = mp[i][j]-'0';  ///用整型数组存起来
			}
		}
		int cnt = 0;
		For(i,1,9)
		{
			For(j,1,9)
			{
				cnt++;
				shudostep[cnt].dx=i;
				shudostep[cnt].dy=j;  ///路径
			}
		}
		dfs(1);

		for(i=1;i<=9;i++)
		{
			for(j=1;j<=9;j++)
			{
				cout<<shudo[i][j];
			}
			cout<<endl;
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值