本文目录一览:
Java迷宫算法问题(用栈实现)有算法简述
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("Maze2tong.txt"));
int row = 0;
char[][] Mazemap = new char[12][58];
while (input.hasNext()) {
String line = input.nextLine();
for (int column = 0; column = line.length() - 1; column++) {
char c = line.charAt(column);
Mazemap[row][column] = c;
}
row++;
}
for (int i = 0; i 12; i++) {
for (int j = 0; j 58; j++) {
System.out.print(Mazemap[i][j]);
}
System.out.print("\n");
}
LinkedListTwoTupleInteger, Integer trace = new LinkedListTwoTupleInteger, Integer();
System.out.println(maze(Mazemap, trace));
System.out.println(trace);
}
public static boolean maze(char[][] maze,
ListTwoTupleInteger, Integer trace) {
LinkedListTwoTupleInteger, Integer path = new LinkedListTwoTupleInteger, Integer();
HashSetTwoTupleInteger, Integer traverse = new HashSetTwoTupleInteger, Integer();
for (int i = 0; i maze.length; i++) {
for (int j = 0; j maze[i].length; j++) {
if (maze[i][j] == 'S') {
path.add(new TwoTupleInteger, Integer(i, j));
}
}
}
while (!path.isEmpty()) {
TwoTupleInteger, Integer temp = path.pop();
if (traverse.contains(temp)) {
continue;
} else if (maze[temp.first][temp.second] == 'F') {
trace.add(temp);
return true;
} else if (!traverse.contains(temp)) {
if (temp.second + 1 maze[temp.first].length
maze[temp.first][temp.second + 1] != 'W')
path.add(new TwoTupleInteger, Integer(temp.first,
temp.second + 1));
if (temp.second - 1 0
maze[temp.first][temp.second - 1] != 'W')
path.add(new TwoTupleInteger, Integer(temp.first,
temp.second - 1));
if (temp.first + 1 maze.length
maze[temp.first + 1][temp.second] != 'W')
path.add(new TwoTupleInteger, Integer(temp.first + 1,
temp.second));
if (temp.first - 1 0
maze[temp.first - 1][temp.second] != 'W')
path.add(new TwoTupleInteger, Integer(temp.first - 1,
temp.second));
traverse.add(temp);
trace.add(temp);
}
}
trace.clear();
return false;
}
}
class TwoTupleA, B {
public final A first;
public final B second;
public TwoTuple(A a, B b) {
first = a;
second = b;
}
@Override
public int hashCode() {
return first.hashCode() + second.hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof TwoTuple)) {
}
return obj instanceof TwoTuple first.equals(((TwoTuple) obj).first)
second.equals(((TwoTuple) obj).second);
}
public String toString() {
return "(" + first + ", " + second + ")";
}
} // /:-
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Scanner;
class MyPoint
{
public boolean visited=false;
public int parentRow=-1;
public int parentColumn=-1;
public final char content;
public int x;
public int y;
public MyPoint(char c,int x,int y)
{
this.content = c;
this.x = x;
this.y = y;
}
}
public class Maze
{
public static MyPoint[][] getMazeArray(){
Scanner input = null;
MyPoint[][] mazemap = new MyPoint[12][58];
try {
input = new Scanner(new File("Maze2tong.txt"));
int row = 0;
while (input.hasNext()) {
String line = input.nextLine();
for (int column = 0; column = line.length() - 1; column++) {
char c = line.charAt(column);
MyPoint point = new MyPoint(c,row,column);
mazemap[row][column] = point;
}
row++;
}
input.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return mazemap;
}
public static boolean tomRun(MyPoint[][] maze,MyPoint end)
{
int x = maze.length;
int y = maze[0].length;
LinkedListMyPoint stack=new LinkedListMyPoint();
for (int i = 0; i maze.length; i++) {
for (int j = 0; j maze[i].length; j++) {
if (maze[i][j].content == 'S') {
stack.push(maze[i][j]);
maze[i][j].visited=true;
}
}
}
boolean result=false;
while(!stack.isEmpty())
{
MyPoint t=stack.pop();
//System.out.println("pop point:"+t.x+" "+t.y+" value:"+maze[t.x][t.y]);
if(t.content == 'F')
{
result=true;
end.x = t.x;
end.y = t.y;
break;
}
if(t.x-1 0 maze[t.x-1][t.y].visited==false maze[t.x-1][t.y].content != 'W')
{
stack.push(maze[t.x-1][t.y]);
maze[t.x-1][t.y].parentRow=t.x;
maze[t.x-1][t.y].parentColumn=t.y;
maze[t.x-1][t.y].visited=true;
}
if(t.x + 1 x maze[t.x+1][t.y].visited==false maze[t.x+1][t.y].content != 'W')
{
stack.push(maze[t.x+1][t.y]);
maze[t.x+1][t.y].parentRow=t.x;
maze[t.x+1][t.y].parentColumn=t.y;
maze[t.x+1][t.y].visited=true;
}
if(t.y - 1 0 maze[t.x][t.y - 1].visited==false maze[t.x][t.y-1].content != 'W')
{
stack.push(maze[t.x][t.y-1]);
maze[t.x][t.y-1].parentRow=t.x;
maze[t.x][t.y-1].parentColumn=t.y;
maze[t.x][t.y-1].visited=true;
}
if( t.y + 1 y maze[t.x][t.y + 1].visited==false maze[t.x][t.y+1].content != 'W')
{
stack.push(maze[t.x][t.y+1]);
maze[t.x][t.y+1].parentRow=t.x;
maze[t.x][t.y+1].parentColumn=t.y;
maze[t.x][t.y+1].visited=true;
}
}
return result;
}
public static void show(int x,int y,MyPoint[][] visited)
{
if(visited[x][y].parentRow==-1)
{
System.out.println("["+x+","+y+"]");
return;
}
show(visited[x][y].parentRow,visited[x][y].parentColumn,visited);
System.out.println("-"+"["+x+","+y+"]");
}
public static void main(String[] args)
{
MyPoint[][] maze = getMazeArray();
MyPoint point = new MyPoint('c',1,1);
if(tomRun(maze,point))
{
System.out.println("逃生路径如下:");
show(point.x,point.y,maze);
}
else
System.out.println("无法走出迷宫!");
}
}
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW
WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOWWWWWWWWWWWWWOOOOOOW
WWWWWWOOOOOOOOOOOOWWWWWWWOOOOOOOOOOOOWWWWWWWWWWWWWWWWOOOOW
WOOOOOOWWWWWWWWWWWWWWOOOOOOOOOOOWWWWWWWWWWWWWWWWWWWWWWWWWW
WOOOOWWWWWWWOOOOOOWWWWOOOOOOWWWWWWWWWWWOOOOWWWWWWWWWOWWWWW
WOO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WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
java怎么生成迷宫地图
//作者:zhongZw
package cn.zhongZw.model;
import java.util.ArrayList;
import java.util.Random;
public class MazeModel {
private int width = 0;
private int height = 0;
private Random rnd = new Random();
public MazeModel() {
this.width = 50; //迷宫宽度
this.height = 50; //迷宫高度
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public MazeModel(int width, int height) {
super();
this.width = width;
this.height = height;
}
public ArrayList MazePoint getMaze() {
ArrayList MazePoint maze = new ArrayList MazePoint ();
for (int h = 0; h height; h++) {
for (int w = 0; w width; w++) {
MazePoint point = new MazePoint(w, h);
maze.add(point);
}
}
return CreateMaze(maze);
}
private ArrayList MazePoint CreateMaze(ArrayList MazePoint maze) {
int top = 0;
int x = 0;
int y = 0;
ArrayList MazePoint team = new ArrayList MazePoint ();
team.add(maze.get(x + y * width));
while (top = 0) {
int[] val = new int[] {
-1, -1, -1, -1
};
int times = 0;
boolean flag = false;
MazePoint pt = (MazePoint) team.get(top);
x = pt.getX();
y = pt.getY();
pt.visted = true;
ro1: while (times 4) {
int dir = rnd.nextInt(4);
if (val[dir] == dir)
continue;
else
val[dir] = dir;
switch (dir) {
case 0: // 左边
if ((x - 1) = 0 maze.get(x - 1 + y * width).visted == false) {
maze.get(x + y * width).setLeft();
maze.get(x - 1 + y * width).setRight();
team.add(maze.get(x - 1 + y * width));
top++;
flag = true;
break ro1;
}
break;
case 1: // 右边
if ((x + 1) width maze.get(x + 1 + y * width).visted == false) {
maze.get(x + y * width).setRight();
maze.get(x + 1 + y * width).setLeft();
team.add(maze.get(x + 1 + y * width));
top++;
flag = true;
break ro1;
}
break;
case 2: // 上边
if ((y - 1) = 0 maze.get(x + (y - 1) * width).visted == false) {
maze.get(x + y * width).setUp();
maze.get(x + (y - 1) * width).setDown();
team.add(maze.get(x + (y - 1) * width));
top++;
flag = true;
break ro1;
}
break;
case 3: // 下边
if ((y + 1) height maze.get(x + (y + 1) * width).visted == false) {
maze.get(x + y * width).setDown();
maze.get(x + (y + 1) * width).setUp();
team.add(maze.get(x + (y + 1) * width));
top++;
flag = true;
break ro1;
}
break;
}
times += 1;
}
if (!flag) {
team.remove(top);
top -= 1;
}
}
return maze;
}
}
迷宫
[java] view plain copy
//作者:zhongZw
//邮箱:zhong317@126.com
package cn.zhongZw.model;
import java.util.*;
import java.lang.*;
public class MazePoint {
private int left = 0;
private int right = 0;
private int up = 0;
private int down = 0;
private int x;
private int y;
public boolean visted;
public MazePoint(int x, int y) {
this.x = x;
this.y = y;
}
public int getLeft() {
return left;
}
public void setLeft() {
this.left = 1;
}
public int getRight() {
return right;
}
public void setRight() {
this.right = 1;
}
public int getUp() {
return up;
}
public void setUp() {
this.up = 1;
}
public int getDown() {
return down;
}
public void setDown() {
this.down = 1;
}
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;
}
}
java老鼠迷宫代码难吗
非常难。思路:
1、设老鼠的行进路线都是优先选择下-右-上-左。
2、设老鼠很聪明,走过的路线走撒泡尿,表示鼠大爷到此一游,我们可以把数组的值改为3,表示走过,但走不通。
3、这是一个int[8][8]的二位数组,那么开始位置下标是1,1,结束位置是6,6。行和列分别用、j表示。
4、实际路线我们可以设置2表示,我们可以使用递归,让老鼠不断测试路线。
5、最后打印数组,看老鼠的实际路线。