POJ2676 DFS+剪枝

原创 2018年04月17日 16:36:05
Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 22411 Accepted: 10576 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

DFS+剪枝, 暴力DFS会wa

//DFS+剪枝
#include <iostream>
#include <cstdio>
#include <set>
#include <queue>
#include <algorithm>
#include <stack>
#include <map>
#include <cstring>
using namespace std;
typedef long long ll;
int G[10][10];
int vis_row[10][10];
int vis_col[10][10];
int vis_block[10][10];
bool dfs(int x, int y) {
	if (x == 10) return true;
	bool flag = false;
	if (G[x][y] != 0) {
		if (y == 9) flag = dfs(x + 1, 1);
		else flag = dfs(x, y + 1);
		if (flag) return true;
		else return false;
	} else {
		int k = 3*((x-1)/3)+(y-1)/3+1;
		for (int i = 1; i <= 9; i++) {
			if (!vis_row[x][i] && !vis_col[y][i] && !vis_block[k][i]) {
				G[x][y] = i;
				vis_row[x][i] = 1;
				vis_col[y][i] = 1;
				vis_block[k][i] = 1;
				if (y == 9) flag = dfs(x + 1, 1);
				else flag = dfs(x, y + 1);
				if (!flag) {
					G[x][y] = 0;
					vis_row[x][i] = 0;
					vis_col[y][i] = 0;
					vis_block[k][i] = 0;
				} else {
					return true;
				}
			}
		}
	}
	return false;
}
int main() {
	int t;
	cin >> t;
	while (t-- != 0) {
		char b[10][10];
		memset(vis_row, 0, sizeof(vis_row));
		memset(vis_col, 0, sizeof(vis_col));
		memset(vis_block, 0, sizeof(vis_block));
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= 9; j++) {
				cin >> b[i][j];
				G[i][j] = b[i][j] - '0';
				if (G[i][j] != 0) {
					int k = 3*((i-1)/3)+(j-1)/3+1;
					vis_row[i][G[i][j]] = 1;
					vis_col[j][G[i][j]] = 1;
					vis_block[k][G[i][j]] = 1;
				}
			}
		}
		dfs(1, 1);
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= 9; j++) {
				cout << G[i][j];
			}
			cout << endl;
		}
	}



	return 0;
}

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载 @爬动的小蜗牛 https://blog.csdn.net/qq_34649947/article/details/79973044

图---DFS剪枝

DFS剪枝即对DFS进行适当优化,满足条件则退出递归,不需要遍历完所有情况。 例如下面出自POJ的题目: TCGG GCAG CCGC GATC ATCG 这五个串是一个原串的子串,求最小原串的长度?...
  • henry860916
  • henry860916
  • 2015-12-27 16:24:44
  • 341

Sticks POJ - 1011(DFS+剪枝)

George took sticks of the same length and cut them randomly until all parts became at most 50 units ...
  • yinghui_yht
  • yinghui_yht
  • 2017-03-20 11:39:41
  • 69

POJ 1011 Sticks (DFS + 剪枝)

Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 135947   Accepted: 31984 ...
  • a2459956664
  • a2459956664
  • 2016-06-08 21:01:18
  • 147

DFS初级剪枝及心得

关于DFS心得: 1、利用结构体,记录mark和题目要求的基本属性。 2、用到递归,使用递归时注意要设置出口,即符合要求时return,注意对递归的理解,对于不同情况可能要传递不同的参数,但出口都...
  • yopilipala
  • yopilipala
  • 2017-01-14 11:21:22
  • 440

POJ3733-Changing Digits【DFS+强剪枝】

  • 2011年08月18日 16:23
  • 12KB
  • 下载

POJ3373-Changing Digits【DFS+强剪枝】

  • 2011年08月19日 00:44
  • 12KB
  • 下载

DFS + 剪枝策略

一:简介 (1)相信做过ACM的人,都很熟悉图和树的深度优先搜索;算法里面有蛮力法 —— 就是暴力搜索(不加任何剪枝的搜索); (2)蛮力搜搜需要优化时,就是需要不停的剪枝,提前减少不必要的搜索路径,...
  • u010700335
  • u010700335
  • 2015-03-06 09:29:49
  • 4432

uva 317 Hexagon

题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem...
  • hackerwin7
  • hackerwin7
  • 2013-11-26 22:01:26
  • 931

DFS之奇偶性剪枝

奇偶剪枝 【问题描述:】 给定一个N*M的迷宫以及起点和终点,迷宫中有一些障碍无法穿过,问能否不重复也不停留地在刚好一共走T步出迷宫。   【问题分析:】 先来看下这张图片: 也就是说当要走偶数...
  • i1020
  • i1020
  • 2017-02-08 00:02:21
  • 565

HDU 1518 Square(DFS+剪枝)

Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...
  • llwwlql
  • llwwlql
  • 2016-12-27 13:12:47
  • 208
收藏助手
不良信息举报
您举报文章:POJ2676 DFS+剪枝
举报原因:
原因补充:

(最多只允许输入30个字)