package com.kingdz.algorithm.time201703;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.Stack;
/**
* 迷宫求解
*
* @author kingdz
*
*/
public class Algo29 {
public static void main(String[] args) {
int[][] maze = genMaze(7, 40);
printMaze(maze);
System.out.println("-----------------------------------------");
Stack<MazePoint> visted = solveMaze(maze, new MazePoint(0, 0), new MazePoint(6, 39));
if (visted != null && !visted.isEmpty()) {
printMaze(maze);
printMaze(maze, visted);
} else {
System.out.println("没有可达路径");
}
}
/**
* 打印迷宫,访问的路径顺序
*
* @param maze
* @param visted
*/
private static void printMaze(int[][] maze, Stack<MazePoint> visted) {
if (visted == null) {
return;
}
List<MazePoint> list = new ArrayList<MazePoint>();
while (!visted.isEmpty()) {
list.add(visted.pop());
}
Collections.reverse(list);
String[][] mazeStr = new String[maze.length][maze[0].length];
for (int i = 0; i < mazeStr.length; i++) {
for (int j = 0; j < mazeStr[i].length; j++) {
if (maze[i][j] == 1) {
mazeStr[i][j] = "■";
} else {
mazeStr[i][j] = " ";
}
}
}
char c = 'A';
for (MazePoint p : list) {
mazeStr[p.getX()][p.getY()] = "" + c;
c++;
if (c == 'O') {
c = 'A';
}
}
System.out.println("------------------str-----------------------");
for (int i = 0; i < mazeStr.length; i++) {
for (int j = 0; j < mazeStr[i].length; j++) {
System.out.print(mazeStr[i][j]);
}
System.out.println();
}
}
/**
* 求迷宫路线
*
* @param maze
* @param start
* @param end
* @return
*/
private static Stack<MazePoint> solveMaze(int[][] maze, MazePoint start, MazePoint end) {
// 起点是墙,直接返回
if (maze[start.getX()][start.getY()] == 1) {
return null;
}
Stack<MazePoint> hasVisted = new Stack<MazePoint>();
MazePoint now = start;
maze[start.getX()][start.getY()] = 2;
hasVisted.push(start);
while (!hasVisted.isEmpty()) {
// now = moveOne(maze, now);
now = moveOneRandom(maze, now);
if (now.equals(end)) {
maze[now.getX()][now.getY()] = 2;
hasVisted.push(now);
return hasVisted;
} else if (now.equals(new MazePoint(-1, -1))) {
MazePoint p = hasVisted.pop();
maze[p.getX()][p.getY()] = 3;
if (!hasVisted.isEmpty()) {
now = hasVisted.peek();
}
} else {
if (hasVisted.contains(now)) {
now = hasVisted.peek();
} else {
maze[now.getX()][now.getY()] = 2;
hasVisted.push(now);
}
}
printMaze(maze);
System.out.println("-----------------------------------------");
}
return null;
}
/**
* 随机走一步
*
* @param maze
* @param now
* @return
*/
private static MazePoint moveOneRandom(int[][] maze, MazePoint now) {
Set<Integer> set = new HashSet<Integer>();
Random r = new Random(System.currentTimeMillis());
while (set.size() < 4) {
int time = r.nextInt(4);
if (set.contains(time)) {
continue;
} else {
set.add(time);
}
int x = now.getX();
int y = now.getY();
switch (time) {
case 0:
y++;
break;
case 1:
x++;
break;
case 2:
y--;
break;
case 3:
x--;
break;
}
time++;
if (x < 0 || y < 0 || x >= maze.length || y >= maze[0].length) {
continue;
} else {
int ret = maze[x][y];
if (ret == 1 || ret == 2 || ret == 3) {
continue;
}
return new MazePoint(x, y);
}
}
return new MazePoint(-1, -1);
}
/**
* 按照右下左上的顺序走一步
*
* @param maze
* @param now
* @return
*/
@Deprecated
public static MazePoint moveOne(int[][] maze, MazePoint now) {
int time = 0;
while (time < 4) {
int x = now.getX();
int y = now.getY();
switch (time) {
case 0:
y++;
break;
case 1:
x++;
break;
case 2:
y--;
break;
case 3:
x--;
break;
}
time++;
if (x < 0 || y < 0 || x >= maze.length || y >= maze[0].length) {
continue;
} else {
int ret = maze[x][y];
if (ret == 1 || ret == 2 || ret == 3) {
continue;
}
return new MazePoint(x, y);
}
}
return new MazePoint(-1, -1);
}
/**
* 随机生成迷宫
*
* @param weight
* @param height
* @return
*/
private static int[][] genMaze(int weight, int height) {
int[][] maze = new int[weight][height];
Random r = new Random(System.currentTimeMillis());
for (int i = 0; i < weight; i++) {
for (int j = 0; j < height; j++) {
int ret = r.nextInt(6);
if (ret == 1) {
maze[i][j] = 1;
}
}
}
return maze;
}
/**
* 打印迷宫<br/>
* 0是空的,1是墙,2是成功的路,3是已经探测过的路
*
* @param maze
*/
private static void printMaze(int[][] maze) {
for (int i = 0; i < maze.length; i++) {
for (int j = 0; j < maze[i].length; j++) {
int re = maze[i][j];
if (re == 0) {
System.out.print(" ");
} else if (re == 1) {
System.out.print("O");
} else if (re == 2) {
System.out.print("A");
} else if (re == 3) {
System.out.print(" ");
} else {
System.out.print("4");
}
}
System.out.println();
}
}
}
class MazePoint {
private int x;
private int y;
public MazePoint(int x, int y) {
super();
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;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof MazePoint) {
MazePoint p2 = (MazePoint) obj;
if (this.x == p2.getX() && this.y == p2.getY()) {
return true;
}
}
return super.equals(obj);
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
}
【算法】程序猿不写代码是不对的37
最新推荐文章于 2023-12-24 16:00:47 发布