zoukankan      html  css  js  c++  java
  • 深度优先搜索(迷宫救人最短路径) Major^

     1 import java.util.Scanner;
     2 
     3 public class One {
     4     //n,m为迷宫的行列范围,p,q是某人迷路所在地点,min用于记录走到终点最小路径的步数
     5     public static int n,m,p,q,min=9999;
     6     //数组a是迷宫,1代表有障碍物,数组d用于移动方向(右、下、左、上),数组book用于标记当前位置是否在路径中
     7     public static int a[][]=new int[51][51],book[][]=new int[51][51],d[][]={{0,1},{1,0},{0,-1},{-1,0}};
     8     //用于寻找最短路径的方法
     9     public static void f(int x,int y,int step){
    10         if(x==p&&y==q){
    11             if(step<min){
    12                 min=step;
    13                 return;
    14             }
    15         }
    16         for(int k=0;k<=3;k++){
    17             int tx=x+d[k][0];
    18             int ty=y+d[k][1];
    19             if(tx<1||ty<1||tx>n||ty>m||a[tx][ty]==1||book[tx][ty]==1)continue;
    20             book[tx][ty]=1;
    21             f(tx,ty,step+1);
    22             book[tx][ty]=0;
    23         }
    24         return;
    25     }
    26     public static void main(String args[]){
    27         Scanner scanner=new Scanner(System.in);
    28         System.out.println("输入迷宫的行列数n、m:");
    29         n=scanner.nextInt();
    30         m=scanner.nextInt();
    31         System.out.println("输入迷宫地图:");
    32         for(int i=1;i<=n;i++){
    33             for(int j=1;j<=m;j++){
    34                 a[i][j]=scanner.nextInt();
    35             }
    36             System.out.println("");
    37         }
    38         System.out.println("输入终点:");
    39         p=scanner.nextInt();
    40         q=scanner.nextInt();
    41         f(1,1,0);
    42         System.out.println("最短步数是:"+min);
    43         
    44     }
    45 }
    迷宫搜索
    基础学习
  • 相关阅读:
    aria2安装webui
    c++指针参数是如何传递内存的
    ssl 证书申请
    LNMP一键包安装后解决MySQL无法远程连接问题
    流水线设计 转:http://www.opengpu.org/forum.php?mod=viewthread&tid=2424
    IUS nc simulator
    ccd与coms摄像头的区别
    昨天下午写的FPGA驱动VGA显示图片
    tcl脚本
    用FPGA驱动ov7670摄像头用tft9328显示
  • 原文地址:https://www.cnblogs.com/qinmeizhen/p/6786371.html
Copyright © 2011-2022 走看看