数据结构与算法--简单的迷宫问题

package com.hadwinling.recursion;

import java.util.Iterator;

/*
 * 迷宫
 */



public class RecursionTest {
	public static void main(String[] args) {
		//先创建一个二维数组,来模拟迷宫
		int[][] map = new int[8][7];
		//使用1 作为墙
		//上下全部置为1
		for (int i = 0; i < 7; i++) {
			map[0][i] = 1;
			map[7][i] = 1;
		}
		//左右全部置为1
		for (int i = 0; i < 8; i++) {
			map[i][0] = 1;
			map[i][6] = 1;
		}
		//设置挡板,1表示
		map[3][1] = 1;
		map[3][2] = 1;
		//输出地图
		System.out.println("地图的情况");
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 7; j++) {
				System.out.print(map[i][j]);
			}
			System.out.println();
		}
		//使用递归回溯给小球找路
		setWay2(map, 1, 1);
		//输出新的地图,小球走过,并标识过的地图
		System.out.println("输出新的地图,小球走过,并标识过的地图");
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 7; j++) {
				System.out.print(map[i][j]);
			}
			System.out.println();
		}
	}
	//使用递归回溯来给小球找路
	//说明:
	//1.map表示地图
	//2.i,j表示从地图的哪个位置开始找
	//3.如果小球能到map[6][5]位置,则说明通路找到
	//4.约定:当map[][] 为0表示该店没有走过,当为1表示墙不能走,2表示通路 3表示该点走过但是走不通
	//5.在走迷宫时需要确定一个策略:下-》右—》上-》左,如果该点走不通,在回溯
	/**
	 * 
	 * @param map	地图	
	 * @param i	从哪个位置开始找
	 * @param j
	 * @return	如果找到通路,就返回true,否则返回false
	 */
	public static boolean setWay(int[][] map,int i ,int j) {
		if (map[6][5] ==2) {//通路已经找到
			return true;
		}else {
			if (map[i][j] == 0) {//如果当前这个点还没有走过
				//按照策略下-》右—》上-》左
				map[i][j] = 2;//假定该店时可以走通的
				if (setWay(map, i+1, j)) {//向下走
					return true;
				}else if (setWay(map, i, j+1)) {//向右走
					return true;
				}else if (setWay(map, i-1, j)) {//向上
					return true;
				}else if (setWay(map, i, j-1)) {//向左走
					return true;
				}else {
					//说明该点时走不通的,
					map[i][j] = 3;
					return false;
				}
			}else {//如果map[i][j]!=0,可能时1,2,3
				return false;
			}
		}
	}
	//修改找路的策略。改成上-右-下-左
	public static boolean setWay2(int[][] map,int i ,int j) {
		if (map[6][5] ==2) {//通路已经找到
			return true;
		}else {
			if (map[i][j] == 0) {//如果当前这个点还没有走过
				//按照策略下-》右—》上-》左
				map[i][j] = 2;//假定该店时可以走通的
				if (setWay2(map, i-1, j)) {//向上走
					return true;
				}else if (setWay2(map, i, j+1)) {//向右走
					return true;
				}else if (setWay2(map, i+1, j)) {//向下
					return true;
				}else if (setWay2(map, i, j-1)) {//向左走
					return true;
				}else {
					//说明该点时走不通的,
					map[i][j] = 3;
					return false;
				}
			}else {//如果map[i][j]!=0,可能时1,2,3
				return false;
			}
		}
	}
	
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* ****迷宫算法求解************* */ /**计目标:教学演示**************/ #include<stdio.h> #define rows 10 #define cols 10 typedef struct {int row; int col; }PosType;/* //坐标点结构 */ typedef struct {int ord;/*//通道块在路径上的“序号” */ PosType seat;/*//通道块在迷宫中的“坐标位置”*/ int di;/*//从此通道快走向下一通道块的“方向” */ }SElemType;/*//栈的元素类型 */ typedef struct {SElemType *base; SElemType *top; int stacksize; }SqStack;/*//堆栈结构 */ void InitStack(SqStack &s)/*//初始化堆栈 */ { s.base=(SElemType *)malloc(100*sizeof(SElemType)); if(!s.base) return NULL; s.top=s.base; s.stacksize=100; } int StackEmpty(SqStack s)/* //栈空判别*/ {return(s.top==s.base); } void Pop(SqStack &s ,SelemType &e)/*//弹栈 */ {e=*--s.top); } void Push(SqStack &s,SElemType e)/*//将元素压入堆栈*/ { *s.top++=e; } /*static int maze[rows][cols]= {{0,0,0,0,0,0,0,0,0,0}, {0,1,1,0,1,1,1,0,1,0}, {0,1,1,0,1,0,1,0,1,0}, {0,1,1,0,1,0,0,1,1,0}, {0,1,1,0,0,1,1,1,1,0}, {0,1,1,1,0,1,1,1,1,0}, {0,1,0,1,1,1,0,1,1,0}, {0,1,0,0,0,1,0,0,1,0}, {0,0,1,1,1,1,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0}, }; */ /* //初始迷宫数据(1-通,0-不通)*/ static int maze[rows][cols]= {{0,0,0,0,0,0,0,0,0,0}, {0,1,1,0,1,1,1,0,1,0}, {0,1,1,0,1,0,1,1,1,0}, {0,1,1,1,0,0,0,0,1,0}, {0,1,0,0,0,1,1,1,1,0}, {0,1,0,1,0,1,0,0,0,0}, {0,1,0,1,1,1,0,1,1,0}, {0,1,0,1,0,0,0,0,1,0}, {0,0,1,1,1,1,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0}, }; /* //初始迷宫数据(1-通,0-不通)*/ static int foot[10][10]={0};/*//标记某点是否走过(1-走过,0-未走过)*/ void printpath(SqStack &s)/*//打印迷宫通路*/ {int i,j; SElemType e; while(!StackEmpty(s)) { Pop(s,e); foot[e.seat.row][e.seat.col]=1; } for(i=0;i<10;i++) {printf("\n"); for(j=0;j<10;j++) if(foot[i][j]) printf(" # "); else printf(" . "); } } int Pass(PosType pos)/*//判断当前的通道块是否可通*/ { return(maze[pos.row][pos.col]); }; void FootPrint(PosType pos) { maze[pos.row][pos.col]=0; } PosType NextPos(PosType curpos,int dir)/*//取当前通道块的下一个通道块*/ { switch(dir) {case 1: curpos.row++; break; case 2: curpos.col++; break; case 3: curpos.row--; break; case 4: curpos.col--; } return curpos;/*//将下一个通道块变为当前通道块*/ } int END(PosType curpos,PosType end) {return(curpos.row==end.row && curpos.col==end.col); } void MazePath(SqStack &s,PosType start,PosType end) {PosType curpos,nextpos; int curstep; SElemType e; SqStack *s; s=InitStack(); curpos=start; curstep=1; do{ if(Pass(curpos)) {FootPrint(curpos); e.ord=curstep;e.seat=curpos;e.di=1; Push(s,e); if(END(curpos,end)) return s; curpos=NextPos(curpos,1); curstep++; }/* end of if */ else { if(!StackEmpty(s)) { e=Pop(s); while(e.di==4 && !StackEmpty(s)) {FootPrint(e.seat);/* The same fuction as MarkPrint ? */ e=Pop(s); }/* end of while */ if(e.di<4) {e.di++;Push(s,e); curpos=NextPos(e.seat,e.di); } /* end of if */ } /* end of if */ } /* end of else */ }while(!StackEmpty(s)); curstep=0; return NULL; } void main() {SqStack *s; static PosType start={1,1},end={8,8}; s=MazePath(start,end); if(s) printpath(s); else printf("\n NO find the path!"); }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值