1. 程式人生 > >組合(dfs)

組合(dfs)

從n個數裡選r個數組合

#include<stdio.h>
#include<string.h>
int n,r;
int a[11];
int vis[11];

void dfs(int step)
{
	if(step==r+1)
	{
		for(int i=1;i<=r;i++)
			printf("%d",a[i]);
		printf("\n");
		return;
	}
	for(int i=n;i>0;i--)
	{
		if(vis[i]==0 && i<a[step-1])
		{
			vis[i]=1;
			a[step]=i; 
			dfs(step+1);
			vis[i]=0;
		}
	}
 } 
 int main()
 {
	a[0]=1000;
	scanf("%d %d",&n,&r);
	memset(vis,0,sizeof(vis));
	dfs(1);	 
	return 0;
 }

相關推薦

51nod1268 和為K的組合DFS

group () 關註 text lag int name 分享圖片 iostream 1268 和為K的組合 基準時間限制:1 秒 空間限制:131072 KB 分值: 20 難度:3級算法題 收藏 關註 給出N個正整數組成的數組A,求能否從中選出若

組合dfs

從n個數裡選r個數組合 #include<stdio.h> #include<string.h> int n,r; int a[11]; int vis[11]; void

[ACM] POJ 3740 Easy Finding DFS

cas oss miss 矩陣 org fin contest std size Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16202 Accepted: 4349

ECNU 3260 袋鼠媽媽找孩子dfs

highlight 另一個 () class div names 中一 ble problem 鏈接:http://acm.ecnu.edu.cn/problem/3260/ 題意: 給出一個x,y,k。求從左上角到(x,y)最短路徑不少於k而且最快到達(x,y)的迷宮。(

POJ 2386 Lake CountingDFS

poj 所有 tput algorithm space 答案 復雜度 for pre 題意:有一個大小為N×M的園子,雨後積起了水。八連通的積水被認為是連在一起的。求園子裏一共有多少水窪? * * * * W* (八連通指的就是左圖中相對W的*的部分)

HIT1946 希爾伯特分形曲線dfs

pic ios put 組成 快的 結束 return ext ati 補第二次期末考的題……發現代碼細節還需要加強啊……這樣一道題一直犯小錯誤。 題目鏈接:   http://acm.hit.edu.cn/hoj/problem/view?id=1946 題目描述:

POJ - 3984 - 迷宮問題 DFS

define class ac代碼 ng- art clu cstring fff table 迷宮問題 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10936 Accep

uva1103dfs

lan hide type case clu for ont eof origin UVA - 1103 還是沒寫好,,看的別人的 1 #include <iostream> 2 #include <cstdio> 3 #includ

Kick the ball!dfs湖南省賽第十屆

board have class weight you determine wikipedia spa scanf Problem K: Kick the ball! Time Limit: 1 Sec Memory Limit: 128 MB S

HDU 5952 Counting Cliquesdfs

lap ont there ins -- icpc output stream script Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja

HRBUST2030dfs

-c math cas bitset bsp lin ng- printf n) 成語接龍 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %lld , %

[poj 2331] Water pipe ID A*叠代加深搜索dfs

系統 叠代加深搜索 space ces ted nes rain log names Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265

POJ 2531 Network Saboteurdfs

idt 分配 最大 val work 解題思路 clu mit enter 題目代號:POJ 2531 題目鏈接:http://poj.org/problem?id=2531 Language: Default Network Saboteur Time Limi

A Knight's Journey DFS

rpe for pos sca board around span this osi Background The knight is getting bored of seeing the same black and white squares again and a

HDU 2660 Accepted Necklace DFS

%d stones 01背包 pla time 要求 highest size bits Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja

CodeVS 1294 全排列dfs

spl pan ref blog mes pac targe amp open 題目: http://codevs.cn/problem/1294/ 代碼(用cout 會超時!!!): #include <iostream> #include<cs

poj2078 MatrixDFS

[] mes inf clas namespace pre r+ 右移 ems 題目鏈接 http://poj.org/problem?id=2078 題意 輸入一個n×n的矩陣,可以對矩陣的每行進行任意次的循環右移操作,行的每一次右移後,計算矩陣中每一列的和的最大值,輸出

poj1321 棋盤問題DFS

problem ble pen 兩個 oid boa 可能 ref 題意 題目鏈接 http://poj.org/problem?id=1321 題意 給定一塊棋盤(棋盤可能是不規則的),有k個相同棋子,將k個棋子擺放在棋盤上,使得任意兩個棋子不同行,不同列,求有多少

CodeForces 884A.B.C Book Reading | Japanese Crosswords Strike Back | Bertown SubwayDFS

有序 req air 改變 rain ont ring cal word Recently Luba bought a very interesting book. She knows that it will take t seconds to read the book

【UVa】439 Knight Movesdfs

scan sca return true false stdout 沒有 nss logs 題目 題目 ? ? 分析 沒有估價函數的IDA。。。。。。 ? ? 代碼 #include <cstdio> #include <cstring> #in