Sudoku 数独问题(dfs)

程序设计与算法(二)期末考试之06:Sudoku 描述 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 cel 阅读详情

Problem 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


#if 0
#include<iostream>
using namespace std;
int a[15][15],cnt;
bool flag=0;

int ok(int x,int y,int k)
{
	for(int i=0; i<9; i++)
	{
		if(a[x][i]==k)
			return 0;
		if(a[i][y]==k)
			return 0;	
	}
	
	int sx=x-x%3;
	int sy=y-y%3;
	for(int i=sx; i<sx+3; i++)
	{
		for(int j=sy; j<sy+3; j++)
		{
			if(a[i][j]==k)
				return 0;
		}
	}
	return 1;
}


void dfs(int num)
{
	
	bool flag1=0;
	int x,y;

	if(num==cnt)  
	{
		for(int i=0; i<9; i++)
		{
			for(int j=0; j<9; j++)
			{
				cout<<a[i][j];
			}
			cout<<endl;
		}
		flag=1;
		return;
	}
	if(flag)
		return;
    
	for(int i=0; i<9; i++)
	{
		for(int j=0; j<9; j++)
		{
			if(a[i][j]==0)
			{
				flag1=1;
				x=i;y=j;
				break;
			}
		}
		if(flag1)
			break;
	}
			
		for(int k=1; k<=9; k++)
		{
			if(ok(x,y,k))
			{
				a[x][y]=k;
				dfs(num+1);
				a[x][y]=0;
			}
		
		}		
}

int main()
{
	int N;
	char c;
	cin>>N;
	
	while(N--)
	{
		flag=0;
		cnt=0;
		
		for(int i=0; i<9; i++)
		{
			for(int j=0; j<9; j++)
			{
				cin>>c;
				a[i][j]=c-'0';
				if(a[i][j]==0)
					cnt++;	 
			}
		}
	
		dfs(0);

		
	}
		
}
#endif 

2024最新版Anaconda Navigator安装使用指南 本文简要介绍如何下载安装Anaconda Navigator最新版,以及如何使用Python内核搭载来完成应用程序开发。 阅读详情

相关推荐

Pytorch实现softmax多分类

PyTorch实现softmax多分类

天涯雨的博客 1388

POJ 2676 Sudoku 数独(枚举dfs)

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 othe...

点点滴滴 361

顺序功能图基本结构

顺序功能图(Sequential Function Chart,SFC)是一种用于描述工业控制系统中顺序控制流程的图形化编程语言,广泛应用于PLC(可编程逻辑控制器)编程。其核心思想是将复杂的控制流程分解为多个有序的步骤,通过逻辑条件实现步骤间的切换。顺序功能图通过图形化的方式简化了复杂控制逻辑的设计,尤其适用于需要严格顺序控制的工业自动化场景。[初始步] → 转换条件1 → [步1] → 转换条件2 → [步2] → ...[步X] → → [步B] → 转换条件B → → [合并步]

ayingqin1的博客 1226

【详解】Sudoku POJ - 2676(经典问题 DFS 重复剪枝)⭐⭐⭐

Sudoku POJ - 2676 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 fr...

Suprit's blog 786

别再翻手册了!用Materials Studio的Forcite模块,5步搞定复杂溶剂的溶解度参数计算

本文详细介绍了如何使用Materials Studio的Forcite模块,通过5个步骤快速计算复杂溶剂的溶解度参数。从分子建模、结构优化到分子动力学采样,再到最终的内聚能密度计算,该方法大幅提升了石油化工和高分子材料研发中的效率,避免了传统手册查阅和实验的繁琐过程。

weixin_28707739的博客 280

Oracle 11g版本下载及安装超详细教程图解

新手Oracle安装教程

weixin_43738344的博客 10万+

POJ-2676 Sudoku(简单数独-dfs深搜)

Sudoku Time Limit: 2000MS Memory Limit: 65536K 题目链接 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 s...

孤风的博客 797

LVGL项目内存告急?试试把中文字库放到SD卡里(基于LvglFontTool V0.5)

本文详细介绍了在LVGL嵌入式项目中如何通过将中文字库存储到SD卡来优化内存使用。针对Flash资源有限的MCU,如STM32F103,提供了基于LvglFontTool V0.5的外部字库生成与动态加载方案,适用于多字号、多语言及OTA场景,显著减少固件体积并提升显示灵活性。

weixin_33726318的博客 456

Sudoku POJ - 2676 题解

原题 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 cell

qq_45750017的博客 967

R语言 百度坐标转换至WGS84坐标

本文利用R语言封装了百度坐标转换至WGS84坐标的函数,亲测有效,提供了便捷的百度坐标转换方法。

qingchen98的博客 2135

MCU-内部flash模拟成EepRom(重新梳理)

通过fee写数据Data的过程中,数据会mapping一个虚拟地址,这个虚拟地址就是你的身份,在向flash写数据的过程中会写入很多相同虚拟地址的不同数据,在读取或切页的过程中,遍历数据只会认为最下面第一个的虚拟地址所持有的数据是有效数据,其他的都是老数据,可以丢弃。模拟成eeprom的两个eep_page写数据逻辑就是,先在eep_page0写数据,eep_page0写满后,将eep_page0的有效数据copy到eep_page1,之后擦eep_page0;

xiandang8023的博客 711

华为机试HJ44:Sudoku(数独问题,深度优先遍历DFS解法)

C++实现——Sudoku(数独问题)

翟天保的博客 1万+

YOLO11改进策略【独家融合改进】| EfficientViMBlock + LGAG + DySample

通过学习采样来学习上采样DySample。

Limiiiing的博客 363

[DFS]poj 2676 Sudoku 数独问题

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14917   Accepted: 7372   Special Judge Description Sudoku is a very simple task. A square table wi

童凌的技术博客 778

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

poj2676--Sudoku(搜索练习5-数独游戏)

Sudoku Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2013-01-21) Description Sudoku is a very si

随风的专栏 1119

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

poj2676(数独 sudoku)

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11943   Accepted: 5952   Special Judge Description Sudoku is a very simple task. A square table with 9

小周的专栏 1106

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

hdu 1426 Sudoku Killer(DFS 数独问题)

坑爹的输入啊!!! 思路:先将需要填的数的坐标记录在结构体数组中,运用深搜填数,然后在通过一个judge函数判断是否符合要求 #include #include int map[9][9],num; struct node { int x,y; }s[81]; bool judge(int t,int cur) { int i,j; for(i = 0 ; i <

no pains,no gains 1338

求解数独sudoku问题 9*9c++代码_数独小项目开篇:DFS解决数独难题

01引言这周小刀是挺忙的,周末加班,哎,谁不是996呢?(打工魂燃烧吧~这次我们来讲讲一个小项目——九宫格数独。有很多关于这个问题的益智游戏,但是它的解法其实也可以用我们讲的DFS来解决。不过我这次讲的是个小项目,那自然会较为完整一些。在DFS算法实现数独的基础上,利用图像处理来将一张数独题目的图片(可以从软件上截图获取)进行数据读取:即给我一张数独的图片,我就能给出这道数独题目的解法。...

weixin_29254673的博客 474

数独问题的介绍及POJ 2676-Sudoku(dfs+剪枝)

知道是数独问题后犹豫了一下要不要做(好像很难的样纸==。),用dfs并剪枝,是一道挺规范的搜索题。 先介绍以下数独吧~ 数独(Sudoku)是一种运用纸、笔进行演算的逻辑游戏。玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格 的数字,并满足每一行、每一列、每一个粗线宫内的数字均含1-9,不重复。 每一道合格的数独谜题都有且仅有唯一答案,推理方法也以此为基础,任何无解或多解的题目...

dianche1354的博客 219

HDU - 6341 Let Sudoku Rotate (DFS+剪枝)

Let Sudoku Rotate 题目链接: Problem J. Let Sudoku Rotate 题意 给你一个16 * 16的矩阵,里面出现的数为十六进制的1到F,为数独分布,有人将其任意个4 * 4的单独小块逆旋转任意多次,现在,需要我们将其顺时针还原,问最少还原几次。 思路 直接dfs+可行性剪枝即可,因为在数独中,剪枝效率高。 代码 #includ...

帅帅的博客 268

poj 2676(dfs求解数独问题,对行列和格子分别加bool数组优化搜索)

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12350   Accepted: 6163   Special Judge Description Sudoku is a very simple task. A square table with 9

有礼貌的程序员 1158

位运算优化数独

闲得无聊,写个数独玩玩,结果调试了半天。

总理同学的编程尝试 2158

【wikioi】1174靶形数独

DFS爆搜,相当慢…… #include #include #include #include const int M = 10; const int p[10][10] = { {0,0,0,0,0,0,0,0,0,0}, {0,6,6,6,6,6,6,6,6,6}, {0,6,7,7,7,7,7,7,7,6}, {0,6,7,8,8,8,8,8,7,6}, {0,6,7

生息之地 1万+

POJ2676 Sudoku

Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21055 Accepted: 10043 Special Judge Description Sudoku is a very simple task. A square table ...

weixin_30516243的博客 130

POJ 2676 Sudoku

Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6771 Accepted: 3277 Spe...

weixin_33705053的博客 71
上一篇: pots 倒水问题(bfs)
下一篇: Children of the Candy Corn(搜索方向变化问题)(bfs)(dfs)
Joined
博客等级 码龄10年 19粉丝 134原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值