Java:深度优先搜索(DFS)解决纸牌问题以及迷宫最短路径问题

一.纸牌问题

/*
* 有n个盒子排成一行
* 有n张牌,上面数字分别为1-n
* 将这n张牌放入n个盒子有多少种放法
*/

 1 package DFSearch;
 2 
 3 import java.util.Scanner;
 4 /*
 5  * 有n个盒子排成一行
 6  * 有n张牌,上面数字分别为1-n
 7  * 将这n张牌放入n个盒子有多少种放法
 8  */
 9 public class PlayCards {
10     static int n;
11     static int[] a = new int[10];
12     static int[] book = new int[10];
13     public static void main(String[] args){
14         @SuppressWarnings("resource")
15         Scanner scan = new Scanner(System.in);
16         n = scan.nextInt();
17         dfs(1);
18         return;
19     }
20     public static void dfs(int step){
21         //递归完成的结束条件
22         //如果站在第n+1个盒子前,则放置完成
23         if(step==n+1){
24             //输出放置的顺序
25             for(int i=1;i<=n;i++){
26                 System.out.print(a[i]+" ");
27             }
28             System.out.println();
29             //必须要有return,出递归
30             return;
31         }
32         //面对第step个盒子,从牌1遍历下去,如果发现还在手中则放入盒子,标记,接着递归,再次标记返回上一层递归
33         for(int i=1;i<=n;i++){
34             if(book[i]==0){
35                 a[step] = i;
36                 book[i] = 1;
37                 dfs(step+1);
38                 book[i] = 0;
39             }
40         }
41         return;
42     }
43 }

执行结果:

二.迷宫最短路径

5*5迷宫初始化如下

0 0 1 0 1

0 0 0 0 1

0 0 1 0 0

0 1 0 0 1

0 0 0 0 1

初始位置在(0,0)处,欲到达(3,2)处最少需要走多少步

代码如下:

 1 package DFSearch;
 2 
 3 import java.util.Scanner;
 4 
 5 public class FindPath {
 6     //人质所在迷宫的位置
 7     static int fx,fy;
 8     //迷宫为5*5
 9     static int n=5;
10     //上下左右移动
11     static int[][] temp ={{0,1},{1,0},{0,-1},{-1,0}};
12     //迷宫数组
13     static int [][] squera = new int [n][n];
14     //标记数组,走过就标记为1
15     static int [][] book = new int [n][n];
16     //最短步数
17     static int min = 9999999;
18     public static void main(String[] args){
19         @SuppressWarnings("resource")
20         Scanner scan  = new Scanner(System.in);
21         
22         System.out.println("请输入迷宫5*5:");
23         for(int i=0;i<n;i++){
24             for(int j=0;j<n;j++){
25                 squera[i][j] = scan.nextInt();
26             }
27         }
28         System.out.println("请输入人质所在位置:");
29         fx = scan.nextInt();
30         fy = scan.nextInt();
31         book[0][0] = 1;
32         dfs(0,0,0);
33         System.out.println(min);
34         /*for(int i=0;i<5;i++){
35             for(int j=0;j<5;j++){
36                 if(book[i][j]==1){
37                     System.out.println("<"+i+","+j+">->");
38                 }
39             }
40         }*/
41     }
42     public static void dfs(int x,int y,int step){
43         //如果到达地点,结束
44         if(x==fx&&y==fy){
45             if(step<min){
46                 min = step;
47             }
48             return;
49         }
50         //循环移动到四个方向
51         for(int i=0;i<4;i++){
52             int tx = temp[i][0];
53             int ty = temp[i][1];
54             //如果该方向越界了,改变到另一个方向
55             if(x+tx<0||x+tx>=n)
56                 continue;
57             if(y+ty<0||y+ty>=n)
58                 continue;
59             //如果该位置没有障碍物并且也没有走过,走
60             if(squera[x+tx][y+ty]==0 && book[x+tx][y+ty]==0){
61                     //标记为走过
62                     book[x+tx][y+ty] = 1;
63                     //搜索过程
64                     //System.out.println(""+(x+tx)+","+(y+ty)+"->");
65                     //往下一层递归
66                     dfs(x+tx,y+ty,step+1);
67                     //取消标记,回到上一层
68                     book[x+tx][y+ty] = 0;
69                 }
70             }
71         return;
72     }
73 }

执行结果:

 

转载于:https://www.cnblogs.com/fanghuiplus/p/9463314.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值