今天策划提出了一个欠打的需求:生成一张地图,只有一条路从底部到达顶部,要求线路不能重复不能交叉,并且线路之前最少有一堵墙隔离开。行列数可配置。
大概需求模型如下:
红色的是线路,黄色的障碍物。
然后我冥思苦想和借鉴网上的迷宫算法后想出了写法,分享给大家,如果大家还有更好的idea可以分享出来一起学习。
Main类:
import org.apache.commons.lang3.RandomUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
//@Data
public class Main {
private int rowNum;
private int columnNum;
private int[][] map;
private Point startPoint;
private Point curPoint;
private Stack<Point> points = new Stack<>();
public Main(int rowNum, int columnNum) {
this.rowNum = rowNum;
this.columnNum = columnNum;
this.map = new int[rowNum][columnNum];
}
public void genMap() {
try {
//第一步获取起点
genStartPoint();
//第二步根据起点生成墙壁
int even = startPoint.getColumn() % 2 == 0 ? 1 : 0;
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < columnNum; j++) {
if (i % 2 == 0) {
//
if (i % 4 == 0) {
if (j % 2 == even) {
map[i][j] = 1;
}
} else {
if (j % 2 != even) {
map[i][j] = 1;
}
}
} else {
if (j != columnNum - 1) {
map[i][j] = 1;
}
}
}
}
//第三步随机打通墙壁
curPoint = startPoint;
//当到达最后一层停止
while (curPoint.getRow() < rowNum - 1) {
//偏移
int[] offR = {curPoint.getRow() - 2, curPoint.getRow() + 2};
int[] offC = {curPoint.getColumn() - 1, curPoint.getColumn() + 1};
//找出有效的点
List<Point> valid = new ArrayList<>();
for (int x : offR) {
for (int y : offC) {
if (!isValid(x, y)) {
curPoint.getDest().add(new Point(x, y));
} else {
valid.add(new Point(x, y));
}
}
}
if (curPoint.getDest().size() >= 4) {
curPoint.getFather().getDest().add(curPoint);
removePoint(points.pop(), 0);
removePoint(points.pop(), 1);
curPoint = curPoint.getFather();
continue;
}
Point point = valid.get(RandomUtils.nextInt(0, valid.size()));
//打通墙壁
int middleR = point.getRow() > curPoint.getRow() ? curPoint.getRow() + 1 : curPoint.getRow() - 1;
int middleC = point.getColumn() > curPoint.getColumn() ? curPoint.getColumn() : curPoint.getColumn() - 1;
Point middlePoint = new Point(middleR, middleC);
points.push(middlePoint);
map[middleR][middleC] = 2;
points.push(point);
map[point.getRow()][point.getColumn()] = 2;
point.setFather(curPoint);
curPoint = point;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void removePoint(Point pop, int s) {
map[pop.getRow()][pop.getColumn()] = s;
}
/**
* 是否有效点
*
* @return
*/
public boolean isValid(int i, int j) {
if (i < 0 || i >= rowNum) {
return false;
}
if (j < 0 || j >= columnNum) {
return false;
}
if (map[i][j] == 2) {
return false;
}
if (curPoint.getDest().contains(new Point(i, j))) {
return false;
}
return true;
}
/**
* 获取起点
*/
private void genStartPoint() {
startPoint = new Point(0, RandomUtils.nextInt(0, columnNum));
points.push(startPoint);
map[startPoint.getRow()][startPoint.getColumn()] = 2;
}
public void print() {
for (int i = rowNum - 1; i >= 0; i--) {
if (i % 2 == 1) {
System.out.print(" ");
}
for (int j = 0; j < columnNum; j++) {
if (map[i][j] == 1) {
System.out.print(". ");
} else if (map[i][j] == 2) {
System.out.print("😊 ");
} else {
System.out.print(" ");
}
// System.out.print(map[i][j] == 1 ? "♦ " : " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Main main = new Main(21, 15);
main.genMap();
System.out.println(main.getStartPoint());
main.print();
}
public Point getStartPoint() {
return startPoint;
}
}
Poin类:
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
@Data
@AllArgsConstructor
public class Point {
private int row;
private int column;
private Point father;
private Set<Point> dest = new HashSet<>();
public Point(int row, int column) {
this.row = row;
this.column = column;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return row == point.row &&
column == point.column;
}
@Override
public int hashCode() {
return Objects.hash(row, column);
}
}
运行结果展示: