深度优先搜索——DFS(全排列)

资源限制

内存限制:256.0MB   C/C++时间限制:1.0s   Java时间限制:3.0s   Python时间限制:5.0s

问题描述

  HJQ同学发现了一道数学题,要求n拆分成若干自然数和的方案

输入格式

  输入n

输出格式

  输出n拆分成若干自然数和的方案,每个方案一行

数据规模和约定

  n <= 10

package 算法提高;

import java.io.PrintWriter;
import java.util.Scanner;

public class 自然数拆分 {
	
	/*
	 
1+1+1+1+1
1+1+1+2
1+1+3
1+2+2
1+4

	 */
	
	
	//a用来存储拆分的数 0位置是第一个
	static int a[] = new int[10];
    /*1,核心 ,输入一个数 s  ,对这个数进行1~s/2的拆分,
     * 2,将拆分出的数放到数组a中,记录当前a中存放多少个数
     * 3,更新s,直到我们完成一次拆分结束这个过程
     */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		for (int i = 1; i < n / 2; i++) {
			a[0] = i;
			dfs(n - i, 1);//n-i继续要拆分的,1是存到第几位了 
		}
	}

	public static void dfs(int s, int step) {
		int i;
		if (s == 0) {
			for (i = 0; i < step - 1; i++)
				System.out.print(a[i] + "+");
			if (i == step - 1)
				System.out.println(a[step - 1]);
		}
		for (int j = 1; j <= s; j++) {
			if (j < a[step - 1])
				continue;
			a[step] = j;
//		  System.out.println("存储的是a"+step+"="+a[step]);
			dfs(s - j, step + 1);
		}
	}
}

方法二:

package 算法提高;

import java.util.Scanner;

public class  自然数分解dfs{

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        Scanner sc =new Scanner(System.in);
        int n =sc.nextInt();
        dfs(n,0,0,"");
	}
	//nowget:目前已经得到的值   到n了 就退出   n 原始的数   ,max 已经用到的最大值
	
	static void dfs(int n , int nowget , int max  , String ans ) {
		if(nowget==n) {
			System.out.println(n+"="+ans.substring(0,ans.length()-1));
			return;
		}
		for(int i =1; i<=n-nowget;i++) { //目标加到n  当前已经到了nowget
			if(i>=max) dfs(n, nowget+i, i, ans+i+"+");
		}
	}

}

全排列:

DFS

package 算法提高;

import java.util.Scanner;

public class 全排列dfs {
	
	static int a [] ;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
            Scanner sc =new Scanner(System.in);
            int n = sc.nextInt();
            a= new int[n+1];
            String s ="\t";
            dfs(n , 0 , s);
            
	}
	static void  dfs(int n,int index ,String str ) {
		if(index ==n) {  //次数和n相等了 说明已经试探完毕输出
		  System.out.println(str);
		}
		for(int i=1 ; i<=n;i++) {
			if(a[i]==0) {
				a[i]=1; //表示已经用过 了
				dfs(n,index+1,str+i+"\t");  //index表示次数 每次加1 
				a[i]=0;//回溯,当满足一种全排列后,下一个尝试
			}
		}
	}
}

BFS:

package 算法提高;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class 全排列bfs {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
         Scanner sc =new Scanner( System.in);
          int n =sc.nextInt();
         Queue <String> q= new LinkedList<>();
         
         for(int i = 1 ; i<=n ; i++)q.offer(i+"");
         while(!q.isEmpty()) {
        	 String  head =q.poll();  //弹出列头
//        	 开枝散叶
        	 for(int i =1 ; i<=n;i++) {
        		 if(head.contains(""+i))continue;
        		 String son = head +i  ;
        		 if(son.length()==n)System.out.println(son);
        		 else q.offer(son);
        	 }
         }
         
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值