迷宫问题,一般采用回溯法,利用stack节点探测,算法复杂度O(n^2),好像采用队列也可以实现,下片日志再使用队列试试。
package com.xx.dataStructure.stack;
enum Direction {
east, south, west, north, nothing
}
class Position {
int x;
int y;
boolean isInPath = false;
Direction nextPosDirection;
private Position(int x, int y, boolean isInPath, Direction nextPosDirection) {
this.x = x;
this.y = y;
this.isInPath = isInPath;
this.nextPosDirection = nextPosDirection;
}
private static Position[][] positions = new Position[11][11];
static {
for (int i = 1; i < positions.length; i++) {
for (int j = 1; j < positions[i].length; j++) {
positions[i][j] = new Position(i, j, false, Direction.east);
}
}
}
public static Position getPosition(int x, int y) {
return positions[x][y];
}
public boolean isInPath() {
return isInPath;
}
@Override
public String toString(){
return "("+x +"," + y + ")";
}
}
// 迷宫求解
public class Strap {
static Stack<Position> stack = new Stack<Position>(100);
static {
stack.init();
}
// true
static int[][] strap2 = { { 1,1, 1,1 } ,
{ 1,0, 0 ,1},
{ 1,1, 0,1 } ,
{ 1,1, 1,1 }
};
// true
static int[][] strap3 = {
{ 1, 1, 1, 1, 1 }
, {1, 0, 0, 0, 1 }
, {1, 0, 1, 1, 1 }
, {1, 0, 0, 0, 1 }
, {1, 1, 1, 1, 1 }
};
// false
static int[][] strap4 = { { 0, 1, 1 }, { 0, 0, 1 }, { 0, 1, 0 } };
// true
static int[][] strap10 = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1 },
{ 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1 },
{ 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1 },
{ 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1 },
{ 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1 },
{ 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1 },
{ 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1 },
{ 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1 },
{ 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
static Position getNextPosition(int[][] strap, Position p) {
if (p.nextPosDirection == Direction.nothing)
return null;
if (p.nextPosDirection.compareTo(Direction.east) <= 0 && strap[p.x - 1][p.y] == 0 &&
!Position.getPosition(p.x - 1,p.y).isInPath()) {
p.nextPosDirection = Direction.south;
return Position.getPosition(p.x - 1, p.y);
} else if (p.nextPosDirection.compareTo(Direction.south) <= 0 && !Position.getPosition(p.x,p.y + 1).isInPath()
&& strap[p.x][p.y + 1] == 0) {
p.nextPosDirection = Direction.west;
return Position.getPosition(p.x, p.y + 1);
} else if (p.nextPosDirection.compareTo(Direction.west) <= 0 && !Position.getPosition(p.x + 1,p.y).isInPath()
&& strap[p.x + 1][p.y] == 0) {
p.nextPosDirection = Direction.north;
return Position.getPosition(p.x + 1, p.y);
} else if (p.nextPosDirection.compareTo(Direction.north) <= 0 && !Position.getPosition(p.x,p.y - 1).isInPath()
&& strap[p.x][p.y - 1] == 0) {
p.nextPosDirection = Direction.nothing;
return Position.getPosition(p.x, p.y - 1);
}
return null;
}
static boolean isTrapCanSolution(int [][] strap,int n){
//访问标记n*n阶数组 0 未在路径 1 在路径上
if (n < 2 || strap.length < 4 || strap.length != n + 2)
return false;
if (strap[1][1] != 0 || strap[n][n] != 0){
return false;
}
boolean result = false;
stack.init();
try {
Position pos = Position.getPosition(1, 1);
stack.push(pos);
pos.isInPath = true;
while(!stack.isEmpty()){
Position p = stack.getTop();
if (p == Position.getPosition(n, n)){
result = true;
break;
}
else {
Position nextP = getNextPosition(strap,p);
if (nextP == null){
stack.pop();
p.isInPath = false;
}else {
stack.push(nextP);
nextP.isInPath = true;
}
}
}
if (stack.isEmpty() ){
System.out.println("can not slove the strap problems; ");
}
else {
stack.traverse();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
isTrapCanSolution(strap10,10);
}
}