给你代码,你给出的那两个类,不能满足,我的需要,我就没有使用。
你看一下吧。
----------------------------------------
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.add(startPoint);
findNext(startPoint);
puzzle(maze, startPoint, route);
System.out.println(route);
}
private static void puzzle(int[][] maze, Point p, List
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;
}
}
如果你现在的程序会把入口直接当出口,那么你就把出口给先封住了就是。。
这要用到AA算法,自己百度一下javaAA算法吧
存在多个真确路线的情况么?