永发信息网

急需一java高手帮忙写一迷宫程序

答案:4  悬赏:0  手机版
解决时间 2021-03-20 17:20
  • 提问者网友:焚苦与心
  • 2021-03-20 00:13
急需一java高手帮忙写一迷宫程序
最佳答案
  • 五星知识达人网友:低血压的长颈鹿
  • 2021-03-20 00:36
给你代码,你给出的那两个类,不能满足,我的需要,我就没有使用。
你看一下吧。
----------------------------------------
package stackpackage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Maze {
// 上
private static Point directionTop = new Point(-1, 0);
// 下
private static Point directionBottom = new Point(1, 0);
// 左
private static Point directionLeft = new Point(0, -1);
// 右
private static Point directionRight = new Point(0, 1);
private static Point[] directions = { directionTop, directionRight,
directionBottom, directionLeft };
private static boolean isStop = false;
private static int row = 0;
private static int col = 0;
private static Point startPoint = new Point();
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("data.txt");
BufferedReader br = new BufferedReader(fr);
int rowIndex = 1;
int[][] maze = null;
while (br.ready()) {
String line = br.readLine();
Scanner sc = new Scanner(line);
if (rowIndex == 1) {
row = sc.nextInt();
col = sc.nextInt();
maze = new int[row][col];
} else {
if (rowIndex < row + 2) {
for (int i = 0; i < col; i++) {
maze[rowIndex - 2][i] = sc.nextInt();
}
} else {
startPoint.x = sc.nextInt();
startPoint.y = sc.nextInt();
}
}
rowIndex++;
}
List route = new ArrayList();
route.add(startPoint);
findNext(startPoint);
puzzle(maze, startPoint, route);
System.out.println(route);
}
private static void puzzle(int[][] maze, Point p, List route) {
if (isStop) {
return;
}
Point[] nextDirections = p.nextDirections;
for (int i = 0; i < nextDirections.length; i++) {
if (isStop) {
return;
}
Point direction = nextDirections[i];
Point newP = new Point(p.x + direction.x, p.y + direction.y);
if (newP.isEffective() && maze[newP.x][newP.y] == 0
&& !route.contains(newP)) {
newP.before = p;
findNext(newP);
route.add(newP);
if (isExit(newP)) {
isStop = true;
break;
}
puzzle(maze, newP, route);
}
}
if (isStop) {
return;
}
route.remove(route.size() - 1);
}
private static void findNext(Point p) {
int index = 0;
Point[] nextDirections = new Point[3];
for (int i = 0; i < nextDirections.length; i++) {
nextDirections[i] = new Point(0, 0);
}
for (int i = 0; i < directions.length; i++) {
Point direction = directions[i];
Point newP = new Point(p.x + direction.x, p.y + direction.y);
if (newP.isEffective() && !newP.equals(p.before) && newP.x < row
&& newP.y < col) {
nextDirections[index++] = direction;
}
}
p.nextDirections = nextDirections;
}
private static boolean isExit(Point p) {
if (startPoint.equals(p)) {
return false;
}
for (int i = 0; i < directions.length; i++) {
Point direction = directions[i];
Point newP = new Point(p.x + direction.x, p.y + direction.y);
if (!newP.equals(p.before)
&& (newP.x >= row || newP.y >= col || newP.x < 0 || newP.y < 0)) {
return true;
}
}
return false;
}
}
class Point {
int x = 0;
int y = 0;
Point[] nextDirections = null;
Point before = null;
public Point() {
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "<" + x + "," + y + ">";
}
public boolean isEffective() {
return x >= 0 && y >= 0;
}
public boolean equals(Object obj) {
return equals((Point) obj);
}
public boolean equals(Point p) {
if (p == null) {
return false;
}
return this.x == p.x && this.y == p.y;
}
}
全部回答
  • 1楼网友:山有枢
  • 2021-03-20 02:39
存在多个真确路线的情况么?追问按顺时针找路线,所以第一个按顺时针找的路线就是答案,不过给的迷宫应该是只有一个正确路线的
  • 2楼网友:有你哪都是故乡
  • 2021-03-20 01:51
这要用到AA算法,自己百度一下javaAA算法吧
  • 3楼网友:孤老序
  • 2021-03-20 01:19
如果你现在的程序会把入口直接当出口,那么你就把出口给先封住了就是。。追问迷宫是给的,怎么封?追答你可以给已经给出的矩阵,添加一个边框,例如,你把3*3的矩阵,镶嵌在5*5的矩阵中间。边框的外围数值全部设为 障碍(出口除外) 这样不就可以了吗?
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯