• 万能的搜索3


      大家小的时候都玩过迷宫的游戏,迷宫里面有障碍物有一个出口,和一个入口,通过入口,走到出口,你就找到一条路线。

      

    package 中介者设计模式;
    
    import java.util.Scanner;
    
    public class MainMain {
    	
    	private static int col = 0;
    	private static int row = 0;
    	
    	//地图
    	private static int a[][] = new int[50][50];
    	
    	//标记地图中的点
    	private static int book[][] = new int[50][50];
    	
    	//路径的个数
    	private static int min = 99999;
    	
    	//读入入口的坐标
    	static	int startx = 0;
    	static	int starty = 0;
    			
    			//读入出口的坐标
    	static	int endx = 0;
    	static	int endy = 0;
    	
    	public static void main(String[] args) {
    		
    		for (int i = 0;i<a.length;i++) {
    			for (int j = 0;j<a[i].length;j++) {
    				a[i][j]=0;
    				book[i][j]=0;
    			}
    		}
    		
    		
    		Scanner input = new Scanner(System.in);
    		row = input.nextInt();//行
    		col = input.nextInt();//列
    		
    		//读入地图
    		for (int i = 1; i<=row;i++) {
    			for (int j = 1;j<=col;j++) {
    				a[i][j] = input.nextInt();
    			}
    		}
    		
    		//读入入口的坐标
    		 startx = input.nextInt();
    		 starty = input.nextInt();
    		
    		//读入出口的坐标
    		 endx = input.nextInt();
    		 endy = input.nextInt();
    		
    		//从入口的位置开始搜索
    		book[startx][starty] = 1;//标记为1代表已经在路径中,防止以后重复走,0代表还没有走。
    		dfs(startx,starty,0);
    		System.out.println(min);
    		
    	}
    
    	private static void dfs(int startx, int starty, int step) {
    		//定义一个方向数组
    		int next[][] = {{0,1},//代表右
    				{1,0},//代表下
    				{0,-1},//代表左
    				{-1,0}//代表上
    	    };
    		
    		//判断是不是到达出门的位置
    		if (startx == endx && starty == endy) {
    			//更新路线的值
    			if (step < min) {
    				min = step;
    			}
    			return ;
    		}
    		
    		//试探4个方向的走法
    		for (int i = 0;i<=3; i++) {
    			//计算下一个点的坐标的值
    			int tx = startx+next[i][0];
    			int ty = starty+next[i][1];
    			
    			//判断是不是越界
    			if (tx < 1|| tx > row || ty<1||ty>col) {
    				continue;
    			}
    			
    			if (a[tx][ty] == 0 && book[tx][ty] == 0) {
    				book[tx][ty] = 1;
    				dfs(tx,ty,step+1);
    				book[tx][ty] = 0;
    			}
    		
    		}
    		
    		return;
    		
    		
    		
    		
    		
    	}
    
    }
    

        首先我们要写这个算法的时候,首先要知道,在迷宫中我需要知道那些属性,比如我需要知道方向,方向用什么表示,在这里我们是用一个二维数组表示

    int next[][] = {(0,1),(1,0),(-1,0),(0,-1)}

          如果大家看不懂的话可以画图。比如

          1,2

        2,1 2,2  2,3

                     3,2

      看出来了把2,2是如何变成1,2的是不是横坐标的值减少1是不是对应着(-1,0)代表向上。

      还有一点这个算法其实跟我们之前讲解的算法框架是一样的都是,递归求解,找到一个结束的条件,和递归的条件

       算法框架:

          void dfs () {

            if(判断是不是结束的条件){

              XXXX;

                                 XXX;

                                 return ;//这个不要忘记了

            }else {

                                   //条件(比如标记啊什么的)

                                  dfs();

                                  //条件(恢复设置啊什么的);

                           }

          }

    是不是瞬间感觉清楚了很多!!

  • 相关阅读:
    他人监控相关博客
    cassandra高级操作之JMX操作
    【原创】官方文档-hive 启动命令
    oracle必须启动的服务
    【官方文档】elasticsearch中的API
    Oracle 11g即时客户端在windows下的配置
    Oracle 使用SQL*Plus连接数据库
    Oracle 关闭数据库(未使用Oracle Restart)
    Oracle 启动实例(instance)、打开数据库
    Oracle win32_11gR2_database在Win7下的安装与卸载
  • 原文地址:https://www.cnblogs.com/airycode/p/4828504.html
Copyright © 2020-2023  润新知