#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
//#define INPUT
/**
Problem: poj3740 - Easy Finding
Begin Time:8:30 p.m. 20th/Mar/2012
End Time: 8:42 p.m. 21st/Mar/2012
Last Time: Maybe 4hours-;
Input:
Standard output:
Knowledge point:
DFS+回溯+剪枝
State: WA x 1 , TLE x 1 -> AC
Thought:
--------------------AC后的总结----------------------
1.定义一个数组selectedCol,表示目前选出了几列中有"1".
2.在choose之前,要检查selectedRow[i]是否为true.
如果是true,这行就选过了,就continue
3.在每次choose某行的时候,都在choose函数里进行check
if choose_row[i] == 1 && selectedCol[i] == 1 return false;
if choose_row[i] && !selectedCol[i] => selectedCol[i] = 1;
但是在choose之前记得保存selectedCol的状态,以便return false之前进行回溯。
4.如果choose失败的话,就要检查是否已经找到了全1,如果找到了
isFound = true;
在递归函数里检查isFound = true的话就return true;
5.如果没找到的话,记得回溯。这就要求在递归函数里每次保存selectedCol的状态,失败了进行回溯。
6.如果leftRow == 0 ,那么就check,如果check,return true;,否则return false;
剪枝策略:
每层递归都是从 i = 0 to rowNum 开始选
如果上层递归选了i,那就下层递归从i+1开始选。
因为上层递归从0开始选,选到i的话[0,i]的情况已经由上层递归选完了。
而且行数跟顺序无关
比如说
第一层递归选了1
那么第二,第三……一直到最后一层递归,肯定是把第一次选1的所有情况都遍历了,
第一层递归才能去选2
而且
1 2 3 4 5 6
和
6 5 4 3 2 1
这么选择是没有区别的,所以下一层递归从ind+1开始就可以了
加上这个剪枝,653ms就过了。
更变态的思路:
由于是0,1我们可以按位表示嘛!然后按照规则算,应该会非常快~
递归函数定义:
f(int totalRow,int totalCol,int leftRow,int ind);
totalRow表示总共有多少行
totalCol表示列数
leftRow表示还有几行没选
int ind表示从现在第几行开始选。
--------------------AC前的涂鸦----------------------
其实这道题跟STICK是差不多的,当然,状态压缩是必须的。
每行压缩成一个数字,然后是否有几个数字相加起来正好等于
1111111....
注意,最多可以有10^300,所以这个比较囧!
每一个状态就是按行i是否被选择来决定,
选择了某一行之后,就定义tmp[m]对应的列已经被占领。
那么剩下的就转化为用leftNum - 1行来拼出来其他的列了,如果都能拼出来,那么
return true,否则return false;
定义函数f(rowNum,colNum,leftNum);表示总共有rowNum列,有colNum行,剩leftNum没有选
每次选择的时候定义一个check,看看是否已经选出了每列全有1的行。
还有一个check2,看看当前选的行是否跟以前的行有冲突。
**/
using namespace std;
int matrix[20][310]; ///开大一点总是没错的
bool selectedCol[310];
bool selectedRow[18];
bool isFound;
bool checked(const int colNum)
{
for(int i = 0 ; i < colNum ; i++)
{
if (!selectedCol[i])
return false;
}
return true;
}
bool choose(const int a[310],int colNum)
{
int tmp_state[310];
memset(tmp_state,0,sizeof(tmp_state));
memcpy(tmp_state,selectedCol,sizeof(tmp_state));
for(int i = 0 ; i < colNum ; i++)
{
if(!selectedCol[i] && a[i] )
{
selectedCol[i] = a[i];
}
else
{
if ( selectedCol[i] && a[i] )
{
memcpy(selectedCol,tmp_state,sizeof(tmp_state));
return false;
}
}
}
return true;
}
bool Solve(int rowNum,int colNum,int leftNum,int ind)
{
int tmp_state[310];
memset(tmp_state,0,sizeof(tmp_state));
if( isFound )
return true;
if (leftNum == 0)
{
if ( checked(colNum) )
{
isFound = true;
return true;
}
else
{
return false;
}
}
for(int i = ind ; i < rowNum ; i++)
{
memcpy(tmp_state,selectedCol,sizeof(tmp_state));
if( isFound ) return true;
if ( selectedRow[i] ) continue;
if ( choose(matrix[i],colNum) )
{
if( selectedRow[i] != true && !checked(colNum) )
{
selectedRow[i] = true;
Solve(rowNum,colNum,leftNum - 1,i+1);
}
else
{
if ( checked(colNum) )
{
isFound = true;
return true;
}
}
// memcpy(matrix[i],tmp_state,sizeof(tmp_state));
memcpy(selectedCol,tmp_state,sizeof(selectedCol));
selectedRow[i] = false;
}
// selectedRow[i] = false;
}
return false;
}
int main()
{
int n,m;
#ifdef INPUT
freopen("b:\\acm\\poj3740\\input.txt","r",stdin);
#endif
while(scanf("%d%d",&m,&n) != EOF )
{
isFound = false;
memset(matrix,0,sizeof(matrix));
memset(selectedCol,0,sizeof(selectedCol));
memset(selectedRow,0,sizeof(selectedRow));
for(int i = 0 ; i < m ; i++)
{
for(int j = 0 ; j < n ; j++)
{
scanf("%d",&matrix[i][j]);
}
}
if ( Solve(m,n,m,0) )
{
printf("Yes, I found it\n");
}
else
{
printf("It is impossible\n");
}
}
return 0;
}
相关推荐
FOC实战笔记:RC吸收电路的设计考量与参数调优
本文深入探讨了FOC驱动器中RC吸收电路的设计与调优。针对MOS管开关过程中的电压尖峰问题,详细阐述了RC电路的钳位与能量吸收原理,并提供了从参数计算、实验调试到布局布线的完整实战指南,旨在帮助工程师有效提升系统可靠性与稳定性。
POJ-3740-Easy Finding【DLX精确覆盖】
POJ-3740-Easy Finding
(实测可用)STM32L431RCT6开发板-实验1 跑马灯实验
一、开发板平台简介: 1、开发板资源简介 (1)开发板主芯片型号:STM32L431RCT6 (2)开发板主芯片封装:LQFP-64_10x10x05P (3)开发板主芯片内核:ARM® Cortex®-M4 (4)开发板主芯片主频:80MHz (5)开发板主芯片Flash大小:256KB (6)开发板主芯片RAM大小:64KB
poj 3740 Easy Finding
题意:给一个01矩阵,问是否能找到一个行的集合,使得集合中每列出现且仅出现一次‘1’。 思路:舞蹈链X算法模版题。来谈谈我对这个算法的理解。。这个算法是一个深搜,但是它的数据结构非常巧妙,是一个循环十字链表,把矩阵内为1的元素建立节点,都链起来,还加上了一个“表头”。每次先选一列没找到‘1’的列,如果没有,则寻找失败;如果有,选择一行,把这行,这行有‘1’的列,与这行有公共列出现
【RViz2】导入urdf模型时报错:Could not load resource xxx,Unable to open file xxx,Error retrieving file xxx
初学ROS2,想要在RViz2中显示urdf模型。其中.urdf文件引用了外部的meshes文件夹。
无人机频谱侦测与智能识别技术实战解析
本文深入解析无人机频谱侦测与智能识别技术实战应用,涵盖无线电侦测、信号处理和定位技术等核心内容。通过频谱分析、特征提取和机器学习算法优化,实现高精度无人机识别与定位,并分享复杂环境下的应对策略和系统集成经验。
POJ 3740 Easy Finding(DLX精确覆盖裸题)
POJ 3740 Easy Finding 题目链接 题意:选一些行,要求每行正好覆盖一个列 思路:精确覆盖裸题 代码: #include #include using namespace std; const int MAXNODE = 500010; const int MAXN = 510; const int MAXM = 1010; const i
别再用Excel硬算传热了!用MATLAB/Octave复现国赛数学建模A题(附完整代码与避坑指南)
本文详细介绍了如何利用MATLAB/Octave高效解决数学建模中的传热问题,替代传统的Excel计算方法。通过复现2018年国赛A题的高温作业服传热模型,从理论推导到代码实现,提供了完整的有限差分法解决方案和参数拟合技巧,帮助读者提升数学建模效率和准确性。
POJ 3740 Easy Finding(精确覆盖模板题)
题目大意 给定一个M*N的01矩阵,能否找到一个行的集合,使得集合中每一列都恰好包含一个1。
电网渗透之终端安全
电力系统安全不仅关系到电网的稳定运行,也关系到国家能源安全和国计民生,甚至关系到国家的安全和利益,电力系统由于结构复杂多样、分布广泛、重要性高,一旦系统瘫痪影响巨大,因此,极容易成为敌对势力攻击的首选目标。随着信息安全暴露面迅速增加,将给公司网络安全防线带来严峻挑战。 监控、保障这些电力系统安全运行变的尤为重要。内部安全防护产品有防火墙、纵向加密、反向隔离设备等...
军品研制过程-转阶段
介绍军品转阶段评审
POJ3740-Easy Finding
#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define LL long long const int INF =
团体程序设计天梯赛竞赛题--登顶题【L3-039 攀岩】
登顶级 3 道题,每道题 30 分,满分为 90 分。
[ACM] POJ 3740 Easy Finding (DFS)
Easy Finding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16482 Accepted: 4476 Description Given a M×N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), co
jre9下载 jdk9
(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】
Camera2Demo
在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作
[poj3740]Easy Finding_状态压缩_dfs
Easy Finding poj-3470 题目大意:给你一个01矩阵,问能否选出一些行,使得这些行所新组成的01矩阵每列中有且只有1个1。 注释:1<=行数<=16,1<=列数<=300. 想法:对于一个单独的01矩阵来讲,我们可以用一个数表示其中的每一行,然后暴力枚举每一行选取情况即可。 最后,附上丑陋的代码... ... ...
深度学习工具包 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');```[](https://bitdeli.com/free "Bitdeli Badge")
poj 3740 Easy Finding(Dancing Links)
Easy Finding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15668 Accepted: 4163 Description Given a M×N matrix A. Aij ∈ {0, 1} (0 ≤ i Input There ar
OV9734 DATASHEET,完整规格书
OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等
poj3740 Easy Finding(深搜)
Easy FindingTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11432 Accepted: 2802DescriptionGiven a M×N matrix A. Aij ∈ {
POJ 3740 Easy Finding(舞蹈链)
/* 舞蹈链模板题 */ #include #include const int mMax = 50, nMax = 500; int map[mMax][nMax]; int M, N; struct Node { int left, right, up, down; int col; }node[mMax * nMax];//双向链表 int len; int cnt[nMax];
POJ 3740 Easy Finding 跳舞链模板
题目:http://poj.org/problem?id=3740题意:给出一个n∗mn*m的0101矩阵,问能不能够选出来若干行,使每列恰好只有一个11思路:跳舞链模板题。代码中其实把方案记录在ans数组中,要方案的话打印ans数组即可#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using
POJ 3740 Easy Finding 位运算压缩+DFS or Dancing Links
Easy Finding 这道题运用二进制DFS是比较好的方法,当然还可以用dancing links,这个比较高级,先把我看别人的二进制思路然后写的代码贴上来。 当然本题是体现不出DLX的高级之处的。用位运算和DLX的速度应该是相当 /* ID: sdj22251 PROG: calfflac LANG: C++ */ #include #include #include #in
[ACM] POJ 3740 Easy Finding (DLX模板题)
Easy Finding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 4343 Description Given a M×N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), co
poj 3740 Easy Finding(Dancing Links 精确覆盖)
Easy Finding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16128 Accepted: 4321 Description Given a M×N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), co
POJ 3740 Easy Finding (DLX)
题意:精确覆盖,DLX模板题思路:这个博客写的不错 刚刚开学DLX,感觉是个很简单的东西,只能算是十字链表优化了DFS,所以理论上有些DLX题目的有些题目应该DFS也能水过去 去看板子,发现所有人的板子都几乎一模一样。。。错误及反思:代码:#include <iostream> #include <stdio.h> #include <string.h>const int MN = 1005;/
POJ 3740 Easy Finding 位运算压缩+DFS
Easy Finding 这道题运用二进制DFS是比较好的方法,当然还可以用dancing links,这个比较高级,先把我看别人的二进制思路然后写的代码贴上来。 /* ID: sdj22251 PROG: calfflac LANG: C++ */ #include <iostream> #include <vector> #include <list>...
POJ-3740(dfs边界点和还原处理)(Easy Finding)
【题目描述】 选出一些行使得这些行构成矩阵的每一列都有且只有一个1。 【接替思路】 直接判断每一行是否选中。 此方法并不高效。 int m, n; int Map[16][300]; bool used[300]; bool Find; bool match()//判断所选取的行是否让每一列都恰好有一个1 { int i; for (i = 0; i < n; ++i) if
POJ3740--Easy Finding(Dancing Links)
Description Given a M×N matrix A. Aij ∈ {0, 1} (0 ≤ i Input There are multiple cases ended by EOF. Test case up to 500.The first line of input is M, N (M ≤ 16, N ≤ 300). The next M lines e
276



被折叠的 条评论
为什么被折叠?



