递归实现指数型枚举
创始人
2025-05-31 13:10:11
0

普通方法:

引例:如果题目不要求输出方案必须升序

填坑,从填1个坑到填n个坑。
坑可以随便填,比如第1个坑选了2之后,第2个坑可以填1(非升序),也可以填3(升序)

代码:

import java.io.*;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;public class Main
{static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N = 100010;static int num[] = new int[N];static boolean state[] = new boolean[N];static int n;static void dfs(int pos,int tar){if(pos == tar + 1){for(int i = 1 ; i <= tar ; i ++)  pw.print(num[i] + " ");pw.println();return;}for(int i = 1 ; i <= n ; i ++){if(!state[i]){num[pos] = i;state[i] = true;dfs(pos + 1, tar);num[pos] = 0;state[i] = false;}}}public static void main(String[] args) throws NumberFormatException, IOException{n = rd.nextInt();pw.println(); // 不取for(int i = 1 ; i <= n ; i ++)  dfs(1,i);pw.flush();}
}class rd
{static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));static StringTokenizer tokenizer = new StringTokenizer("");static String nextLine() throws IOException { return reader.readLine(); }static String next() throws IOException{while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());return tokenizer.nextToken();}static int nextInt() throws IOException { return Integer.parseInt(next()); }static double nextDouble() throws IOException { return Double.parseDouble(next()); }static long nextLong() throws IOException { return Long.parseLong(next()); }static BigInteger nextBigInteger() throws IOException{BigInteger d = new BigInteger(rd.nextLine());return d;}
}class math
{int gcd(int a,int b){if(b == 0)  return a;else return gcd(b,a % b);}int lcm(int a,int b){return a * b / gcd(a, b);}// 求n的所有约数List get_factor(int n){List a = new ArrayList<>();for(long i = 1; i <= Math.sqrt(n) ; i ++){if(n % i == 0){a.add(i);if(i != n / i)  a.add(n / i);  // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况  ^-^复杂度能减少多少是多少}}// 相同因子去重,这个方法,完美a = a.stream().distinct().collect(Collectors.toList());// 对因子排序(升序)Collections.sort(a);return a;}// 判断是否是质数boolean check_isPrime(int n){if(n < 2) return false;for(int i = 2 ; i <= n / i; i ++)  if (n % i == 0) return false;return true;}
}class PII implements Comparable
{int x,y;public PII(int x ,int y){this.x = x;this.y = y;}public int compareTo(PII a){if(this.x-a.x != 0)return this.x-a.x;  //按x升序排序else return this.y-a.y;  //如果x相同,按y升序排序}
}class Edge
{int a,b,c;public Edge(int a ,int b, int c){this.a = a;this.b = b;this.c = c;}
}

但是,题目要求输出所有升序方案

依旧是填坑,从填1个坑到填n个坑。
和上面不同的是,上面是第1个坑选了2之后,第2个坑还可以从2之前的数开始填坑,现在是第1个坑选了2之后,第2个坑只能从大于2的数里选了。
即,当前的坑pos处填了num,则填下一个坑pos+1时,只能从大于num的数里选择填坑。

解决办法:dfs里加一个start,选数的时候,只能从start之后的数里面选择

总结:dfs需要四个变量记录当前状态:
当前位于的坑pos,当前可以选的最小数字start,当前的目标总坑数tar,当前已经填的坑数组num[]。

题目AC代码:

import java.io.*;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;public class Main
{static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N = 100010;static int num[] = new int[N];static boolean used[] = new boolean[N]; //判断数字有没有用过static int n;static void dfs(int pos, int startindex ,int tar){if(pos == tar + 1){for(int i = 1 ; i <= tar ; i ++)  pw.print(num[i] + " ");pw.println();return;}// 枚举pos位置可以填哪些数for(int i = startindex ; i <= n ; i ++){if(!used[i]){num[pos] = i; // pos位置填iused[i] = true; // i标记为已使用过dfs(pos + 1, i + 1, tar); // 进入下一层num[pos] = 0;used[i] = false;}}}public static void main(String[] args) throws NumberFormatException, IOException{n = rd.nextInt();pw.println(); // 不取for(int i = 1 ; i <= n ; i ++)  dfs(1,1,i);pw.flush();}
}class rd
{static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));static StringTokenizer tokenizer = new StringTokenizer("");static String nextLine() throws IOException { return reader.readLine(); }static String next() throws IOException{while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());return tokenizer.nextToken();}static int nextInt() throws IOException { return Integer.parseInt(next()); }static double nextDouble() throws IOException { return Double.parseDouble(next()); }static long nextLong() throws IOException { return Long.parseLong(next()); }static BigInteger nextBigInteger() throws IOException{BigInteger d = new BigInteger(rd.nextLine());return d;}
}class math
{int gcd(int a,int b){if(b == 0)  return a;else return gcd(b,a % b);}int lcm(int a,int b){return a * b / gcd(a, b);}// 求n的所有约数List get_factor(int n){List a = new ArrayList<>();for(long i = 1; i <= Math.sqrt(n) ; i ++){if(n % i == 0){a.add(i);if(i != n / i)  a.add(n / i);  // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况  ^-^复杂度能减少多少是多少}}// 相同因子去重,这个方法,完美a = a.stream().distinct().collect(Collectors.toList());// 对因子排序(升序)Collections.sort(a);return a;}// 判断是否是质数boolean check_isPrime(int n){if(n < 2) return false;for(int i = 2 ; i <= n / i; i ++)  if (n % i == 0) return false;return true;}
}class PII implements Comparable
{int x,y;public PII(int x ,int y){this.x = x;this.y = y;}public int compareTo(PII a){if(this.x-a.x != 0)return this.x-a.x;  //按x升序排序else return this.y-a.y;  //如果x相同,按y升序排序}
}class Edge
{int a,b,c;public Edge(int a ,int b, int c){this.a = a;this.b = b;this.c = c;}
}

y总的思路:

枚举每个位置,看看该位置填不填数,填的话填哪些数字

AC代码:(自己写的!!!)
 

import java.io.*;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;public class Main
{static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N = 100010;static List num = new LinkedList<>();static boolean state[] = new boolean[N];static boolean used[] = new boolean[N];static int n;// dfs存的状态:当前第pos位置static void dfs(int pos){if(pos == n + 1){for(int i = 1 ; i <= n ; i ++)   if(state[i]) pw.print(i + " ");  // state[i]为true,即该位置选了,需要输出pw.println();return;}// pos位置不选state[pos] = false;dfs(pos + 1);state[pos] = true;// pos位置选state[pos] = true;dfs(pos + 1);state[pos] = false;}public static void main(String[] args) throws NumberFormatException, IOException{n = rd.nextInt();dfs(1);pw.flush();}
}class rd
{static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));static StringTokenizer tokenizer = new StringTokenizer("");static String nextLine() throws IOException { return reader.readLine(); }static String next() throws IOException{while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());return tokenizer.nextToken();}static int nextInt() throws IOException { return Integer.parseInt(next()); }static double nextDouble() throws IOException { return Double.parseDouble(next()); }static long nextLong() throws IOException { return Long.parseLong(next()); }static BigInteger nextBigInteger() throws IOException{BigInteger d = new BigInteger(rd.nextLine());return d;}
}class math
{int gcd(int a,int b){if(b == 0)  return a;else return gcd(b,a % b);}int lcm(int a,int b){return a * b / gcd(a, b);}// 求n的所有约数List get_factor(int n){List a = new ArrayList<>();for(long i = 1; i <= Math.sqrt(n) ; i ++){if(n % i == 0){a.add(i);if(i != n / i)  a.add(n / i);  // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况  ^-^复杂度能减少多少是多少}}// 相同因子去重,这个方法,完美a = a.stream().distinct().collect(Collectors.toList());// 对因子排序(升序)Collections.sort(a);return a;}// 判断是否是质数boolean check_isPrime(int n){if(n < 2) return false;for(int i = 2 ; i <= n / i; i ++)  if (n % i == 0) return false;return true;}
}class PII implements Comparable
{int x,y;public PII(int x ,int y){this.x = x;this.y = y;}public int compareTo(PII a){if(this.x-a.x != 0)return this.x-a.x;  //按x升序排序else return this.y-a.y;  //如果x相同,按y升序排序}
}class Edge
{int a,b,c;public Edge(int a ,int b, int c){this.a = a;this.b = b;this.c = c;}
}

相关内容

热门资讯

节假日胖东来吸引大量外地游客,... 极目新闻记者 詹钘 端午假期,河南许昌胖东来持续火爆,吸引了大量外地游客。6月1日,有许昌当地出租车...
乡村舞台变身奇幻乐园,“童梦奇... 6月1日,“童梦奇缘 乐在桦墅”金陵艺术村落六一主题活动,在栖霞区桦墅村如约启幕。这座掩映在青山绿水...
刚刚!上海警方通报迪士尼打架事... 据@警民直通车-浦东 通报:5月31日18时许,浦东公安分局接报警称迪士尼乐园内有人打架。经初步调查...
3600亿打造最难修的铁路,川... 随着5月21日拉萨-林芝铁路扩能改造项目的招标公告发布,川藏铁路的建设步伐愈加加速。乍一看,这似乎是...
【MySQL】锁 锁 文章目录锁全局锁表级锁表锁元数据锁(MDL)意向锁AUTO-INC锁...
Centos7安装升级最新版R... Centos7安装升级最新版Redis7.0.10 闭坑攻略1、前言2、升级GCC3、下载编译Red...
2023年长租公寓行业研究报告 第一章 行业概况 长租公寓是一种新型的住房形式,它为租客提供长期的住宿选择࿰...
《全国儿童友好公园打卡地图》在... 中新网广州6月1日电 (记者 程景伟)广州市儿童公园6月1日开启2025年“童迎十五运活力向未来”六...
终于弄明白6-7月去威海,怎么... 威海攻略做好了!去威海不下5次了,建议还没去的姐妹看完这篇再出发,不然真的会不小心就踩雷,去威海玩的...
宜昌三峡三天两晚品质旅游行程,... 宜昌三峡,那是一幅流动的山水画卷,自古以来就吸引着无数文人墨客和游客。这里既有壮美的自然风光,又有深...
做一个元气满满的同路人,在社科... 人生需要归零。每过一段时间都要将过去清零,让自己重新开始。该放手时就放手,...
微软WHQL认证有哪些步骤?驱... 微软WHQL认证是指Microsoft Windows Hardware Quality Labs&...
移动应用架构设计:如何转变开发... 移动应用架构设计:如何转变开发流程 2023 年掌握移动应用程序架构的指南࿰...
原创 最... 夏天开个火炒个菜,汗珠子噼里啪啦往下掉,油烟一熏,胃口都跑没影儿了。这时候,谁还乐意在灶台边吭哧吭哧...
蔬菜中的“维 C 之王”竟是它... 在蔬菜界中,甜椒以其鲜艳的色彩和清脆的口感,常常被当作菜肴的点缀。无论是青红黄相间的炒菜,还是沙拉中...
翠湖又添打卡地!这幅墙绘让三角... 端午假期,春城市民不用出远门也能邂逅众多风景。在翠湖边,“一名”簪花少女悄然现身,吸引了众多市民游客...
当端午遇上“六一” 两江新区景... 三大动漫IP集结、 萌趣恐龙巡游、泰式泼水狂欢..... 6月1日, 端午假期邂逅“六一”儿童节, ...
夏日文旅新地标 来璧山梦界水世... 6月1日,璧山梦界水世界在玉泉湖畔开园。开园首日即迎来众多游客,共同见证了一场融合“水战”激情、亲子...