急需一java高手帮忙写一迷宫程序给你代码,你给出java迷宫的代码的那两个类,不能满足,java迷宫的代码我的需要,java迷宫的代码我就没有使用 。
你看一下吧 。
----------------------------------------
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 (rowIndexrow + 2) {
for (int i = 0; icol; i++) {
maze[rowIndex - 2][i] = sc.nextInt();
}
} else {
startPoint.x = sc.nextInt();
startPoint.y = sc.nextInt();
}
}
rowIndex++;
}
ListPoint route = new ArrayListPoint();
route.add(startPoint);
findNext(startPoint);
puzzle(maze, startPoint, route);
System.out.println(route);
}
private static void puzzle(int[][] maze, Point p, ListPoint route) {
【java迷宫的代码 java实现迷宫小游戏】if (isStop) {
return;
}
Point[] nextDirections = p.nextDirections;
for (int i = 0; inextDirections.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; inextDirections.length; i++) {
nextDirections[i] = new Point(0, 0);
}
for (int i = 0; idirections.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.xrow
newP.ycol) {
nextDirections[index++] = direction;
}
}
p.nextDirections = nextDirections;
}
private static boolean isExit(Point p) {
if (startPoint.equals(p)) {
return false;
}
for (int i = 0; idirections.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.x0 || newP.y0)) {
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() {
推荐阅读
- 包含win7vpn服务器的词条
- 路由器闪红灯怎么连接电脑,路由器桥接怎么连接
- 判断有值js,判断对象是否有值
- 青少年儿童居家体育游戏,青少年家庭游戏
- c语言如何cn定义新函数 c语言自定义函数
- 榆次小程序搭建软件,榆次小程序制作公司
- 快手是什么平台的视频,快手是干什么的平台
- linux命令下快速输入 怎么在linux命令行输入汉字
- antdcss动画,an动画软件怎么用