POJ 3740 Easy Finding(dfs回溯)
来源:互联网 发布:美白水知乎 编辑:程序博客网 时间:2024/04/30 00:37
B - Easy Finding
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
Given a M× N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn contains and only contains one 1.
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 every line contains Nintegers separated by space.
Output
For each test case, if you could find it output "Yes, I found it", otherwise output "It is impossible" per line.
Sample Input
3 30 1 00 0 11 0 04 40 0 0 11 0 0 01 1 0 10 1 0 0
Sample Output
Yes, I found itIt is impossible
这是一道子集枚举的回溯题,针对当前行选不选进行回溯,并且进行剪枝。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define maxn 1005int n,m;int a[20][305];int vis[305];bool judge(){ for(int i=0;i<n;i++) if(vis[i]==0)return false; return true;}bool judges(){ for(int i=0;i<n;i++) if(vis[i]>1)return false; return true;}bool dfs(int cur){ if(judge())return true; if(cur>=m)return judge(); for(int i=0;i<n;i++){ if(a[cur][i]==1)vis[i]++; } if(judges()){ if(dfs(cur+1))return true; } for(int i=0;i<n;i++){ if(a[cur][i]==1)vis[i]--; } if(dfs(cur+1))return true; return false;}int main(){ //freopen("in.txt","r",stdin); while(scanf("%d%d",&m,&n)!=EOF){ for(int i=0;i<m;i++) for(int j=0;j<n;j++) scanf("%d",&a[i][j]); memset(vis,0,sizeof vis); if(dfs(0))puts("Yes, I found it"); else puts("It is impossible"); }}
0 0
- POJ 3740 Easy Finding(dfs回溯)
- [ACM] POJ 3740 Easy Finding (DFS)
- [ACM] POJ 3740 Easy Finding (DFS)
- [ACM] POJ 3740 Easy Finding (DFS)
- POJ 3740 Easy Finding
- poj 3740 Easy Finding
- poj 3740 Easy Finding
- POJ 3740 Easy Finding
- poj-3740 Easy Finding
- POJ 3740 Easy Finding 位运算压缩+DFS or Dancing Links
- POJ-3740(dfs边界点和还原处理)(Easy Finding)
- poj 3740 dfs回溯
- POJ 3740 Easy Finding 详细讲解
- POJ 3740 Easy Finding(舞蹈链)
- poj 3740 Easy Finding 搜索+位运算
- poj 3740 Easy Finding(Dancing Links)
- POJ 3740 - Easy Finding (Dancing links)
- POJ 3740 Easy Finding (DLX模板)
- fds fgdfds
- 求一批整数中出现最多的个位数字
- 大规模优化算法 - LBFGS算法
- 既有名门正派的,也有邪门歪道的,既有稳扎稳打的,也有极走偏锋的
- fstream的使用方法介绍
- POJ 3740 Easy Finding(dfs回溯)
- Javaweb相关知识总结
- C语言堆栈入门——堆和栈的区别
- android开发中控件大小和字体大小的选择
- Eclipse JBPM4.4 集成安装!!!
- “远程遥控”APP技术支持博客
- jquery实现效果比较好的table选中行颜色
- Git常用命令
- 关于键盘