poj2245Lotto(最基础的dfs)
题目链接:
思路:最開始画好搜索状态,然后找好结束条件,最好预推断当前找到的个数和能够找到的是否大于6就可以。。
题目:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6291 | Accepted: 3995 |
Description
with choosing numbers only from S. For example, for k=8 and S = {1,2,3,5,8,13,21,34} there are 28 possible games: [1,2,3,5,8,13], [1,2,3,5,8,21], [1,2,3,5,8,34], [1,2,3,5,13,21], ... [3,5,8,13,21,34].
Your job is to write a program that reads in the number k and the set S and then prints all possible games choosing numbers only from S.
Input
follow in ascending order. Input will be terminated by a value of zero (0) for k.
Output
by the lowest number first, then by the second lowest and so on, as demonstrated in the sample output below. The test cases have to be separated from each other by exactly one blank line. Do not put a blank line after the last test case.
Sample Input
7 1 2 3 4 5 6 7
8 1 2 3 5 8 13 21 34
0
Sample Output
1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 6 7
1 2 3 5 6 7
1 2 4 5 6 7
1 3 4 5 6 7
2 3 4 5 6 7 1 2 3 5 8 13
1 2 3 5 8 21
1 2 3 5 8 34
1 2 3 5 13 21
1 2 3 5 13 34
1 2 3 5 21 34
1 2 3 8 13 21
1 2 3 8 13 34
1 2 3 8 21 34
1 2 3 13 21 34
1 2 5 8 13 21
1 2 5 8 13 34
1 2 5 8 21 34
1 2 5 13 21 34
1 2 8 13 21 34
1 3 5 8 13 21
1 3 5 8 13 34
1 3 5 8 21 34
1 3 5 13 21 34
1 3 8 13 21 34
1 5 8 13 21 34
2 3 5 8 13 21
2 3 5 8 13 34
2 3 5 8 21 34
2 3 5 13 21 34
2 3 8 13 21 34
2 5 8 13 21 34
3 5 8 13 21 34
Source
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=50+10;
int a[maxn],n,ans[maxn];
void dfs(int ith,int num)
{
if(num==6)
{
for(int i=1;i<=5;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[6]);
return;
}
//if(num+n-ith<6) return;
for(int ai=ith+1;i<=n;i++)
{
ans[num+1]=a[i];
dfs(i,num+1);
}
}
int main()
{
while(~scanf("%d",&n))
{
if(n==0) return 0;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
ans[1]=a[i];
dfs(i,1);
}
printf("\n");
}
return 0;
}
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=50+10;
int a[maxn],n,ans[maxn]; void dfs(int ith,int num)
{
if(num==6)
{
for(int i=1;i<=5;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[6]);
return;
}
//if(num+n-ith<6) return;
for(int ai=ith+1;i<=n;i++)
{
ans[num+1]=a[i];
dfs(i,num+1);
}
} int main()
{
while(~scanf("%d",&n))
{
if(n==0) return 0;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
{
ans[1]=a[i];
dfs(i,1);
}
printf("\n");
}
return 0;
}
poj2245Lotto(最基础的dfs)的更多相关文章
- 基础BFS+DFS poj3083
//满基础的一道题 //最短路径肯定是BFS. //然后靠右,靠左,就DFS啦 //根据前一个状态推出下一个状态,举靠左的例子,如果一开始是上的话,那么他的接下来依次就是 左,上 , 右 , 下 // ...
- 数据结构基础(21) --DFS与BFS
DFS 从图中某个顶点V0 出发,访问此顶点,然后依次从V0的各个未被访问的邻接点出发深度优先搜索遍历图,直至图中所有和V0有路径相通的顶点都被访问到(使用堆栈). //使用邻接矩阵存储的无向图的深度 ...
- poj1979【基础bfs/dfs】
挑战习题搜索-1 题意: 给定起点,然后求一个可以到达的数量,位置"."都可以走.每次应该是上下左右都可以走. 思路: 这题应该DFS更好写,但是BFS也可以写吧. 好久没写了- ...
- PTA 2-1 列出连通集【DFS+BFS基础】
给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点. 输入格式: 输入第1 ...
- 用深度优先搜索(DFS)解决多数图论问题
前言 本文大概是作者对图论大部分内容的分析和总结吧,\(\text{OI}\)和语文能力有限,且部分说明和推导可能有错误和不足,希望能指出. 创作本文是为了提供彼此学习交流的机会,也算是作者在忙碌的中 ...
- SCC(强连通分量)
1.定义: 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(SC---strongly connected). 有向图中的极大强连通子图,成为强连通分量(SCC---strongly ...
- tarjan算法 POJ3177-Redundant Paths
参考资料传送门 http://blog.csdn.net/lyy289065406/article/details/6762370 http://blog.csdn.net/lyy289065406/ ...
- [原]poj-3009-Curling 2.0-dfs
题目太长就不贴了,题意: 上下左右四联通块,2表示起点,3表示终点,1为block,0为空地,每动一次冰壶,冰壶就会向推动的方向一直移动,直到碰到block或出界,如果碰到block就在block前停 ...
- 有向强连通分支Tarjan算法
本文转载自:http://blog.csdn.net/xinghongduo/article/details/6195337 说到以Tarjan命名的算法,我们经常提到的有3个,其中就包括本文所介绍的 ...
随机推荐
- iOS开发 - 应用内打开第三方应用并传值
首先说下这个功能, 应该都有接触过. 比方,你下载了一个电子书,然后选择打开方式的时候,可能会看到你手机中已经安装的阅读类App. 或者,你的QQ收到了某个文件,你也能够选择本地的应用来打开. 那这种 ...
- Redhat下安装fedora
步骤具体解释: 1:到fedora官网下载fedora的DVD镜像文件. 2:在linux系统中预留一部分为未分区的空间大约50G 3: 在linux系统上的根分区创建一个fedora的目录,里面 ...
- ASM丢失disk header导致ORA-15032、ORA-15040、ORA-15042 Diskgroup无法mount
SQL> select * from v$version; BANNER --------------------------– Oracle Database 11g Enterprise E ...
- W英语: 紧急, 非紧急
take your time 慢慢来 It is not urgent. Take it easy please. 不急,慢慢来.
- 解 自己关于 C# Button的Click事件的疑惑
先说说C#中事件的用法,从事件的用法中,我自己会产生一个疑惑 C#事件 class Program { static void Main(string[] args) { EventClass ec ...
- 利用VS2005进行dump文件调试(17篇博客)
前言:利用drwtsn32或NTSD进行程序崩溃处理,都可以生成可用于调试的dmp格式文件.使用VS2005打开生成的DMP文件,能很方便的找出BUG所在位置.本文将讨论以下内容: 1. 程序编译选 ...
- 1.1.6-学习Opencv与MFC混合编程之---播放WAV音乐和 alpha融合功能
源代码:http://download.csdn.net/detail/nuptboyzhb/3961698 Alpha融合菜单项 1. 增加alpha融合菜单项,修改相应的属性,建立类向导 ...
- Koa -- 基于 Node.js 平台的下一代 web 开发框架
http://koa.bootcss.com/ 多研究点 react 和 nodejs 这个是未来
- html-图片button,抓包---Shinepans
askLike.html <html> <meta http-equiv="content-type" content="text/html;chars ...
- robotframework ride 版本兼容问题
在安装robotFramework ride的时候,必须需要使用wxpython 目前使用的wxpython 还必须是unicode 版本的要不然不支持中文 目前使用的 wx.version.2.8. ...