迷宫问题(java实现)
1、
public class Direction {
private int x;
private int y;
public Direction(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
2、
public class Position {
private int x;
private int y;
private int d;
public int getX() {
return this.x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return this.y;
}
public void setY(int y) {
this.y = y;
}
public Position(int x, int y, int d) {
this.x = x;
this.y = y;
this.d = d;
}
public int getD() {
return this.d;
}
public void setD(int d) {
this.d = d;
}
}
3、
import java.util.Iterator;
import java.util.Stack;
public class MazeProblem {
public static Stack<Position> path(int[][] maze, Direction[] move) {
Stack<Position> s = new Stack<Position>();
// 起点位置 还未开始探索所以无方向
Position start_p = new Position(1, 1, -1);
s.push(start_p);
maze[1][1] = -1; // 起点位置表示已经走过,不可以往回探索,pop的时候 可以退回
while (!s.empty()) {
Position temp = s.pop(); // 取出当前的位置 准备进行向下探索
// 确定探索的位置方向
int x = temp.getX(); // 当前处在正在探索的位置和方向
int y = temp.getY();
int d = temp.getD() + 1;// 探索的方向
while (d < 8) { // 开始探索 一共八个方向
int i = x + move[d].getX(); // 根据某个方向探的下一个位置
int j = y + move[d].getY();
if (maze[i][j] == 0) { // 如果下一个位置是可以进去的,则放入当前位置
s.push(new Position(x, y, d));
x = i; // 把当前探索位置 调整为放入的位置
y = j;
d = 0; // 调整方向为0,为下次探索做准备
maze[x][y] = -1; // 然后设置为已走过
if (x == Destination.m && y == Destination.n) { // 在判断是不是已经是终点位置 如果是 则程序退出
s.push(new Position(x, y, d));
return s;
}
} else {
d++; //// 如果下一个位置是不可以进去的,则放入调整方向
}
}
}
return new Stack<Position>();
}
//终点位置
static class Destination {
static int m = 6;
static int n = 8;
}
public static void main(String[] arg) {
// 0表示可进入 1 表示 不可以进去 -1 表示走过的路劲也不可进入
// 每个位置 都有八个方向
int[][] maze = {
// 0 1 2 3 4 5 6 7 8 9
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, //
{ 1, 0, 1, 1, 1, 0, 1, 1, 1, 1 }, //
{ 1, 1, 0, 1, 0, 1, 1, 1, 1, 1 }, //
{ 1, 0, 1, 0, 0, 0, 0, 0, 0, 1 }, //
{ 1, 0, 1, 1, 1, 1, 1, 0, 1, 1 }, //
{ 1, 1, 0, 0, 1, 1, 1, 1, 0, 1 }, //
{ 1, 0, 1, 1, 0, 0, 0, 0, 0, 1 }, //
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } //
};
Direction[] move =
{
new Direction(0, 1),
new Direction(1, 1),
new Direction(1, 0),
new Direction(1, -1),
new Direction(0, -1),
new Direction(-1,-1),
new Direction(-1, 0),
new Direction(-1, 1)
};
Stack<Position> s = MazeProblem.path(maze, move);
Iterator<Position> it = s.iterator();
while (it.hasNext()) {
Position e = it.next();
System.out.println(e.getX() + ",-->" + e.getY());
}
}
}
迷宫问题(java实现)的更多相关文章
- 小项目特供 简易迷宫(基于Java)
明天返校,于是昨天和今天简单熟系了一下JAVA的GUI,做了一个简易的迷宫小游戏(暂时没有时间实现随机迷宫及多关卡,仅供学习) 源码及运行文件(提供JRE8):链接:简易迷宫 密码:hy8v
- hdu 1272 小希的迷宫(java实现)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 算法笔记_107:蓝桥杯练习 算法提高 学霸的迷宫(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗.但学霸为了不要别人打扰,住在一个城堡里,城堡外面是一个二维的格子迷宫,要 ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- Java迷宫游戏
缘起: 去年(大三上学期)比较喜欢写小游戏,于是想试着写个迷宫试一下. 程序效果: 按下空格显示路径: 思考过程: 迷宫由一个一个格子组成,要求从入口到出口只有一条路径. 想了一下各种数据结构,似乎树 ...
- HDOJ-ACM1010(JAVA) 奇偶剪枝法 迷宫搜索
转载声明:原文转自:http://www.cnblogs.com/xiezie/p/5568822.html 第一次遇到迷宫搜索,给我的感觉是十分惊喜的:搞懂这个的话,感觉自己又掌握了一项技能~ 个人 ...
- Java与算法之(12) - 老鼠再闯迷宫(广度优先算法)
贪吃的小老鼠又回来了,这次有什么新的办法吃到奶酪呢? 规则不变,只能上下左右在格子内移动. 因为上次的深度优先算法让老鼠走了不少冤枉路,这次老鼠带来了帮手探路鼠.探路鼠的使用规则如下: 小老鼠按右.下 ...
- Java与算法之(5) - 老鼠走迷宫(深度优先算法)
小老鼠走进了格子迷宫,如何能绕过猫并以最短的路线吃到奶酪呢? 注意只能上下左右移动,不能斜着移动. 在解决迷宫问题上,深度优先算法的思路是沿着一条路一直走,遇到障碍或走出边界再返回尝试别的路径. 首先 ...
- 剑指Offer——回溯算法解迷宫问题(java版)
剑指Offer--回溯算法解迷宫问题(java版) 以一个M×N的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍.设计程序,对任意设定的迷宫,求出从入口到出口的所有通路. 下面我们来详细讲一 ...
- Java————迷宫问题
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线. package algorithm_java; import java. ...
随机推荐
- RSA的密钥把JAVA格式转换成C#的格式(2)
把C#格式转换成Java:RSA的密钥把JAVA格式转换成C#的格式(1) 我已经在第一篇介绍过如何把C#格式转换成Java,现在来看看如何把Java格式转换成C#. /// <summary& ...
- ps -ef/ps -aux 查看正在活动的进程
ps -ef 查看正在活动的进程 ps -ef |grep abc 查看含有"abc"的活动进程 ps -ef |grep -v abc 查看不含abc的活动进程 1)ps a 显 ...
- AR路由器web界面每IP限速配置方法
一.做下载方向的限速:在 QOS>接口限速,选择“新建”“接口名称”选择内网接口“限速类型”选择IP限速(目的)“方向”选择流出“起始/目的ip”写内网的ip“类型”选择独占“承诺速率”为限速的 ...
- Flume、Kafka、Storm结合
Todo: 对Flume的sink进行重构,调用kafka的消费生产者(producer)发送消息; 在Sotrm的spout中继承IRichSpout接口,调用kafka的消息消费者(Consume ...
- URL.createObjectURL()
URL.createObjectURL() 静态方法会创建一个 DOMString,其中包含一个表示参数中给出的对象的URL.这个 URL 的生命周期和创建它的窗口中的 document 绑定.这个新 ...
- CentOS设置sendmail发件人,让sendmail不显示通过XXX代发
0.有一个十分快速的方法 命令:hostname itzhanzhang.com,但是重启后会失效,于是请接着往下看一劳永逸的方法: 1.设置你的主机名 默认的主机名是类似于“VM_24_76_cen ...
- mock实例方法
1.Mockito.when(categoryService.queryTopCategory("1")).thenReturn(categories);//返回的是list列表, ...
- PLSQL快捷键设置
1.在PL/SQL Developer中编写sql语句时,如果无法自动提示字段那是一件痛苦的事情,工作效率又低,在此演示下如何在PL/SQL Developer工具中自动提示字段,让开发者省时又省心, ...
- 机器人的运动范围 剑指offer66题
include "stdafx.h" #include<vector> #include<algorithm> #include<string> ...
- address-already in use 以及查看端口
https://stackoverflow.com/questions/19071512/socket-error-errno-48-address-already-in-use