poj 2676Sudoku(DFS+回溯)

Sudoku

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15698   Accepted: 7678   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

Source

Southeastern Europe 2005

题目大意:数独游戏。将初始表示为0的数字全部填满,要求每行每列的数都不一样(1~9),并且每一个3*3的小块里面也是由1~9的数字填充。

所以可以把整个图里面的数字为0的位置都用一个数组存起来。然后在每一个空白的地方尝试去填写数字,然后每填一个数字就做好标记,表示在当前行、当前列、当前的小块里面已经放过这个数字了。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
char map[15][15];
int rownum[15][15],colnum[15][15],blocknum[15][15];    //当前行、当前列、当前小块放置数字的标记。
struct pos{
	int r,c;
	pos(int rr,int cc):r(rr),c(cc){ }
};
vector<pos>blankpos;
int  getblocknum(int r,int c)          //获取当前数字所在小块的位置
{
	int rr=r%3==0?r/3:(r/3+1);
	int cc=c%3==0?c/3:(c/3+1);
	return rr*3+cc;
}
void setflags(int i,int j,int num,int f)
{
	rownum[i][num]=f;
	colnum[j][num]=f;
	blocknum[getblocknum(i,j)][num]=f;
}
bool isOk(int i,int j,int num)        //判断数字是否可行
{
	return !rownum[i][num]&&!colnum[j][num]&&!blocknum[getblocknum(i,j)][num];
}
bool dfs(int n)
{
	if(n<0)return true;
	int i;
	int r=blankpos[n].r;
	int c=blankpos[n].c;
	for(i=1;i<=9;i++)
	{
	if(isOk(r,c,i)){
		map[r][c]=i;
		setflags(r,c,i,1);
		if(dfs(n-1))return true;
			setflags(r,c,i,0);
	}	

	}
	return false;
}
int main()
{
	int T,i,j,k;
	scanf("%d",&T);
	while(T--)
	{
		memset(rownum,0,sizeof(rownum));
		memset(colnum,0,sizeof(colnum));
		memset(blocknum,0,sizeof(blocknum));
		blankpos.clear();
		for(i=1;i<=9;i++)
		for(j=1;j<=9;j++)
		{
			cin>>map[i][j];
			map[i][j]=map[i][j]-'0';
			if(map[i][j])setflags(i,j,map[i][j],1);
			else blankpos.push_back(pos(i,j));     //把空的位置放入数组
		}
		if(dfs(blankpos.size()-1)){
			for(i=1;i<=9;i++)
			{
			for(j=1;j<=9;j++)
			cout<<char(map[i][j]+'0');
			cout<<endl;
			}
		}
	}
     return 0;
 }

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

时间: 2024-10-11 11:02:00

poj 2676Sudoku(DFS+回溯)的相关文章

poj 3009 DFS +回溯

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14567   Accepted: 6082 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

poj1321——dfs回溯

POJ 1321  DFS回溯+递归枚举 棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24813   Accepted: 12261 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每组数据的第一行

POJ 2907 Collecting Beepers (DFS+回溯)

Description Karel is a robot who lives in a rectangular coordinate system where each place is designated by a set of integer coordinates (x and y). Your job is to design a program that will help Karel pick up a number of beepers that are placed in he

POJ2488-A Knight&#39;s Journey(DFS+回溯)

题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36695   Accepted: 12462 Description Background The knight is getting bored of seeing the same black and white squares again and again

poj1011 Sticks DFS+回溯

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=1011 Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but

CodeForces 550B Preparing Olympiad(DFS回溯)

[题目链接]:click here~~ [题目大意] 一组题目的数目(n<=15),每个题目有相应的难度,问你选择一定的题目(大于r个且小于l个)且选择后的题目里最小难度与最大难度差不小于x,求选择方案数. [解题思路]: DFS+回溯. 先发一发比较拙的代码: #include <bits/stdc++.h> using namespace std; const int N=1e5+10; int num[N],mum[N]; int n,m,q,t,l,r; int top,ans,

HDU4499 Cannon DFS 回溯的应用

题意就是给你一个n*m的棋盘,然后上面已经有了 棋子,并给出这些棋子的坐标,但是这些棋子是死的就是不能动,然后让你在棋盘上面摆炮,但是炮之间不能互相吃,吃的规则我们斗懂得 炮隔山打嘛,问你最多能放几个炮 肯定是搜索了,n,m最大才5,可能挺久没做了,对于回溯反而把握不好了,写了好久调试了好久,才过 #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #

POJ 3321 DFS序+线段树

单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4:   5: #include <cstdio> 6: #include <cstring> 7: #include <cctype> 8: #include <algorithm> 9: #include <vector> 10: #include <iostream> 1

poj 1167 DFS

1 /* 2 题意:给出0-59的一排数字表示某一时刻出现了1辆bus,其中数字重复出现表示多辆bus同时出现,这些bus是 3 由多个bus线路组成的,每条线路都有一个时间间隔,而且同一线路的bus在0-59肯定会出现两次或以上,如果 4 有两条线路的间隔相同且到达时刻相同也算作两条不同的线路,问最少有多少条线路. 5 6 题解:DFS 7 这题是一题相对较难的搜索,关键在于找出状态以及剪枝: 8 对于每条bus线路都由开始时刻start和间隔interval组成,这样就找出了表示一条线路的状

蓝桥杯 算法提高 8皇后&#183;改 -- DFS 回溯

  算法提高 8皇后·改   时间限制:1.0s   内存限制:256.0MB 问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8的棋盘. 输出格式 所能得到的最大数字和 样例输入 1 2 3 4 5 6 7 89 10 11 12 13 14 15 1617 18 19 20 21 22 23 2425 26 27 28 29 30 31 3233 34 35 36 37 38 39 4041 42 43 44 45 46 47 48