poj 3740 dfs
来源:互联网 发布:数据交易是指什么 编辑:程序博客网 时间:2024/06/07 19:10
Easy Finding
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 17610 Accepted: 4825
Description
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 N integers 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
题意:就是任意选几行组成一行,在这一行中全部为 1 ,不能多也不能少于 1
c++比G++时间用的短一些
正确代码
注意的地方:
两个减枝的时候比较合适,如果在其他地方放,就会比较麻烦
这里的套路就是 减枝和准备进行下一层调用与回溯分开的,这样避免了很多没有必要的事情
但另外一个调用judge_2的时候若是提到dfs 开头,但是这样会加大时间,会有时候调用没有那个必要的
所以放到后面
这里减少了第二种方法的循环,本程序就是通过最后的再一次进行递归调用等效替代的,pos就是循环因子
#include<stdio.h>#include<string.h>int a[20][310];int temp[310];int m,n;//-------------判断是不是全为 1-----------------//int judge_1(){ for(int i=0;i<n;i++) if(temp[i]!=1) return 0; return 1;}//-------------判断是不是超出1------------------//int judge_2(){ for(int i=0;i<n;i++) if(temp[i]>1) return 0; return 1;}//-------------深搜+回溯-------------------------//int dfs(int pos){//-----------------减枝--------------------// if(judge_1()) return 1; if(pos>=m) return judge_1();//-----------------搜索--------------------// for(int i=0;i<n;i++) temp[i]+=a[pos][i]; if(judge_2()) if(dfs(pos+1)) return 1;//-----------------回溯--------------------// for(int i=0;i<n;i++) temp[i]-=a[pos][i]; if(dfs(pos+1)) return 1; return 0;}int main(){ while(scanf("%d%d",&m,&n)!=EOF) { memset(temp,0,sizeof(temp)); for(int i=0;i<m;i++) for(int j=0;j<n;j++) scanf("%d",&a[i][j]); if(dfs(0)) printf("Yes, I found it\n"); else printf("It is impossible\n"); } return 0;}
循环太多了,数据比较大
#include<stdio.h>#include<string.h>int a[40][400];int temp[400];int m,n;int used[40];int dfs(){ int tag=1; for(int i=0;i<n;i++){ if(temp[i]>1) return 0; if(temp[i]<1){ tag=0; break; } } if(tag) return 1; for(int i=0;i<m;i++){ if(!used[i]){ for(int j=0;j<n;j++) temp[j]+=a[i][j]; used[i]=1; if(dfs()) return 1; else{ for(int j=0;j<n;j++) temp[j]-=a[i][j]; used[i]=0; } } }}int main(){ while(scanf("%d%d",&m,&n)!=EOF) { memset(temp,0,sizeof(temp)); memset(used,0,sizeof(used)); for(int i=0;i<m;i++) for(int j=0;j<n;j++) scanf("%d",&a[i][j]); if(dfs()) printf("Yes, I found it\n"); else printf("It is impossible\n"); } return 0;}
0 0
- poj 3740 dfs
- poj 3740 dfs回溯
- DFS POJ
- POJ DFS
- [ACM] POJ 3740 Easy Finding (DFS)
- [ACM] POJ 3740 Easy Finding (DFS)
- POJ 3740 Easy Finding(dfs回溯)
- [ACM] POJ 3740 Easy Finding (DFS)
- poj 3083 DFS+DFS+BFS
- POJ 1753 ID+DFS
- poj 1950 数字dfs
- poj 1816(trie+dfs)
- POJ 3107 Godfather(DFS)
- poj 3620(DFS)
- poj 1020 DFS
- poj 2362Square(DFS)
- POJ 2676 Sudoku dfs
- POJ 2357 Labyrinth [DFS]
- A1310. 矩形计算(钟沛林) 二维莫队
- Java容器类
- 查看ADB被占的PID
- 读C#线程方面知识做笔记
- win764位 jdk32位 android studio的使用
- poj 3740 dfs
- leetcode——链表快排
- 深度思维:网络推广绝不是千篇一律?
- 单例设计模式(饿汗式,懒汉式)
- C++ String写时拷贝(Copy On Write)
- 基于QT和opencv的摄像头(本地图片)读取并输出程序
- 我的Linux日常命令
- 蛤玮的机房
- HTTP返回码总结