李德鑫迷宫算法

前几天在抖音看到个题,当时没做出来,今天突然想起就用程序做了一个。居然是无解,也不敢完全保证程序没问题,有发现的告诉我,我会更正。

 

题目是图中0的格子走一遍,从左上开始,任意点结束,不能重复不能走斜线,图中1的位置不能走。

01000
00000
00000
00000
00000

 

基本上可以理解为迷宫,下面是我的代码:

package test;

import java.util.ArrayList;
import java.util.List;

public class Test {

	public static int[][] room = new int[8][8];
	// 行动路径
	public static List<Spot> path = new ArrayList<Spot>();

	public static void main(String[] args) {
		
		// 初始化棋盘
		init();
		
		run();
		
		System.out.println("GAME OVER!");
		
	}
	
	private static void init() {
		// 空间初始化
		for(int i = 0; i < 5; i++) {
			for(int j = 0; j < 5; j++) {
				room[i][j] = 0;
			}
		}
		// 不可通行坐标(1,2)
		room[0][1] = 1;
		// 初始两步固定
		room[0][0] = 1;
		room[1][0] = 1;

		// 行动路径初始化
		int[] f = {0,0};
		int[] s = {1,0};
		path.add(new Spot(0, null, f, s));
		path.add(new Spot(1, f, s, null));
		
	}
	
	private static boolean run() {
		int lastSpotNum = path.size();
		Spot lastSpot = path.get(lastSpotNum-1);
		int x = lastSpot.getSpot()[0];
		int y = lastSpot.getSpot()[1];
//		System.out.println(x+","+y);
		
		if(check()) {
			return true;
		}else {
			//上
			if(0 < x) {
				x-=1;
				if(0 == room[x][y]) {
					//移动
					room[x][y] = 1;
					lastSpot.setSon(new int[] {x,y});
					path.add(new Spot(lastSpotNum, lastSpot.spot, new int[] {x,y}, null));
					if(run()) {
						return true;
					}else {
						// 减坐标
						room[x][y] = 0;
						lastSpot.setSon(null);
						path.remove(lastSpotNum);
						x++;
//						System.out.println(x+","+y);
					}
				}else {
					x++;
				}
			}
			//下
			if(x < 4) {
				x+=1;
				if(0 == room[x][y]) {
					//移动
					room[x][y] = 1;
					lastSpot.setSon(new int[] {x,y});
					path.add(new Spot(lastSpotNum, lastSpot.spot, new int[] {x,y}, null));
					if(run()) {
						return true;
					}else {
						// 减坐标
						room[x][y] = 0;
						lastSpot.setSon(null);
						path.remove(lastSpotNum);
						x--;
//						System.out.println(x+","+y);
					}
				}else {
					x--;
				}
			}
			//左
			if(y > 0) {
				y-=1;
				if(0 == room[x][y]) {
					//移动
					room[x][y] = 1;
					lastSpot.setSon(new int[] {x,y});
					path.add(new Spot(lastSpotNum, lastSpot.spot, new int[] {x,y}, null));
					if(run()) {
						return true;
					}else {
						// 减坐标
						room[x][y] = 0;
						lastSpot.setSon(null);
						path.remove(lastSpotNum);
						y++;
//						System.out.println(x+","+y);
					}
				}else {
					y++;
				}
			}
			//右
			if(y < 4) {
				y+=1;
				if(0 == room[x][y]) {
					//移动
					room[x][y] = 1;
					lastSpot.setSon(new int[] {x,y});
					path.add(new Spot(lastSpotNum, lastSpot.spot, new int[] {x,y}, null));
					if(run()) {
						return true;
					}else {
						// 减坐标
						room[x][y] = 0;
						lastSpot.setSon(null);
						path.remove(lastSpotNum);
						y--;
//						System.out.println(x+","+y);
					}
				}else {
					y--;
				}
			}
			return false;
		}
	}
	
	private int[] moveLeft(int[] spot) {
		
		return spot;
	}
	
	private static boolean check() {
		// 遍历棋盘是否全走完
		for(int i = 0; i < 5; i++) {
			for(int j = 0; j < 5; j++) {
				if(0 == room[i][j]) {
					return false;
				}
			}
		}
		for(int i = 0; i < path.size(); i++) {
			System.out.println(path.get(i).getSpot());
		}
		System.out.println(path.toString());
		return true;
	}
}

 

package test;

public class Spot {
	
	public int num;
	
	public int[] father = new int[2];
	
	public int[] spot = new int[2];
	
	public int[] son = new int[2];

	Spot(int n, int[] f, int[] s, int[] ss) {
		this.num = n;
		this.father = f;
		this.spot = s;
		this.son = ss;
	}
	
	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public int[] getFather() {
		return father;
	}

	public void setFather(int[] father) {
		this.father = father;
	}

	public int[] getSpot() {
		return spot;
	}

	public void setSpot(int[] spot) {
		this.spot = spot;
	}

	public int[] getSon() {
		return son;
	}

	public void setSon(int[] son) {
		this.son = son;
	}

}

 

代码比较简单,开始还以为递归会很费时间和资源,结果发现也挺快。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值