寻找迷宫中的最少步数的一个小算法

/**
 *
 *  s 0 1 0
 *  0 0 0 0
 *  0 0 1 0
 *  0 1 e 0
 *  0 0 0 1
 *
 *
 *  竖轴为x轴
 *  横轴为Y轴
 */
 

	/**
	 * 迷宫找最短路径 0代表平地,1代表障碍物,s代表起点,e代表终点 (起点和终点肯定也是0)
	 */

1深度优先搜索

package algorithm;

public class Algorithm2 {


   static int[][] next = {
			{0, 1},//右
			{1, 0},//下
			{0, -1},//左
			{-1, 0}//上
	};
	static int[][] allLocation = new int[50][50];
	static int[][] marks = new int[50][50];

	static {
		//我这列从1,1开始赋值
		allLocation[1][1] = 0;
		allLocation[1][2] = 0;
		allLocation[1][3] = 1;
		allLocation[1][4] = 0;

		allLocation[2][1] = 0;
		allLocation[2][2] = 0;
		allLocation[2][3] = 0;
		allLocation[2][4] = 0;

		allLocation[3][1] = 0;
		allLocation[3][2] = 0;
		allLocation[3][3] = 1;
		allLocation[3][4] = 0;

		allLocation[4][1] = 0;
		allLocation[4][2] = 1;
		allLocation[4][3] = 0;
		allLocation[4][4] = 0;

		allLocation[5][1] = 0;
		allLocation[5][2] = 0;
		allLocation[5][3] = 0;
		allLocation[5][4] = 1;

	}

	static int endX, endY;//终点
	static int startX, startY;//起点
	static int min = 99999;
	static int n = 5;//行数
	static int m = 4;//列数


	public static void main(String[] args) {
		startX = 1;
		startY = 1;

		endX = 4;
		endY = 3;

		marks[startX][startY] = 1;

		dfs(startX,startY,0);

		System.out.println("最短步数为:"+min);

	}

	//深度优先遍历
	public static void dfs(int x, int y, int step) {

		int tx, ty, k;

		//判断是否到达终点的位置
		if (x == endX && y == endY) {
			//更新最小值
			if (step < min) {
				min = step;
			}


			return;
		}

		//枚举四种走法 这里的走法是 右 下 左 上
		for (k = 0; k <= 3; k++) {
			//计算下一个点的坐标
			tx = x + next[k][0];
			ty = y + next[k][1];

			//判断是否越界
			if (tx < 1 || tx > n || ty < 1 || ty > m) {
				continue;
			}

			//判断该点是否为障碍物或者已经在路径中
			if (allLocation[tx][ty] == 0 && marks[tx][ty] == 0) {
				marks[tx][ty] = 1;//已经走过了
				dfs(tx, ty, step + 1);
				marks[tx][ty] = 0;//取消标记位
			}


		}

		return;
	}


}

2广度优先搜索

package algorithm;

public class Algorithm3 {

	//宽度优先探索
	static class Note {
		int x;//横坐标
		int y;//纵坐标
		int f;//父亲在队列中的编号
		int s;//步数
	}


	public static void main(String[] args) {
		//队列最大为2500
		Note[] que = new Note[2501];
		for (int h = 0; h < que.length; h++) {
			que[h] = new Note();
		}

		int a[][] = new int[51][51],//迷宫坐标存储
				book[][] = new int[51][51];//标记是否使用过
		//我这列从1,1开始赋值
		a[1][1] = 0;
		a[1][2] = 0;
		a[1][3] = 1;
		a[1][4] = 0;

		a[2][1] = 0;
		a[2][2] = 0;
		a[2][3] = 0;
		a[2][4] = 0;

		a[3][1] = 0;
		a[3][2] = 0;
		a[3][3] = 1;
		a[3][4] = 0;

		a[4][1] = 0;
		a[4][2] = 1;
		a[4][3] = 0;
		a[4][4] = 0;

		a[5][1] = 0;
		a[5][2] = 0;
		a[5][3] = 0;
		a[5][4] = 1;
		//右 下 左 上
		int next[][] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
		int head = 1,//头
		 tail = 1;//尾
		int n = 5, //行数
				m = 4,//列数
				startX = 1,//标记开始坐标被占用
				startY = 1,
				p = 4,//终点x坐标
				q = 3,//终点y坐标
				tx = 1,//记录变化的x坐标
				ty = 1,//记录变化的y坐标
				flag;//标记是否到达终点 0:没达到 1:表示达到

		que[tail].x = startX;
		que[tail].y = startY;

		que[tail].f = 0;
		que[tail].s = 0;

		tail++;

		book[startX][startY] = 1;


		flag = 0;//标记是否到达终点 0:没达到 1:表示达到

		while (head < tail) {
			for (int k = 0; k <= 3; k++) {
				tx = que[head].x + next[k][0];
				ty = que[head].y + next[k][1];

				if (tx < 1 || tx > n || ty < 1 || ty > m) {
					continue;
				}


				if (a[tx][ty] == 0 && book[tx][ty] == 0) {
					book[tx][ty] = 1;
					que[tail].x = tx;
					que[tail].y = ty;

					que[tail].f = head;
					que[tail].s = que[head].s + 1;
					tail++;
				}

				if (tx == p && ty == q) {
					flag = 1;
					break;
				}
			}
			if (flag == 1) {
				break;
			}
			head++;

		}
		System.out.println("################最少步数为########");
		System.out.println(que[tail - 1].s);


		System.out.println("###########最短路径的逆行坐标###############");

		Note note = que[tail - 1];
		do {
			System.out.println(note.x+","+note.y);
			note = que[note.f];
		}while (note.f != 0);


	}


}

打印结果如下:
################最少步数为########
7
###########最短路径的逆行坐标###############
4,3
4,4
3,4
2,4
2,3
2,2
1,2

参考书籍:啊哈算法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值