給你代碼,你給出的那兩個類,不能滿足,我的需要,我就沒有使用。
為海北州等地區用戶提供了全套網頁設計制作服務,及海北州網站建設行業解決方案。主營業務為做網站、成都網站設計、海北州網站設計,以傳統方式定制建設網站,并提供域名空間備案等一條龍服務,秉承以專業、用心的態度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
你看一下吧。
----------------------------------------
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++;
}
ListPoint route = new ArrayListPoint();
route.add(startPoint);
findNext(startPoint);
puzzle(maze, startPoint, route);
System.out.println(route);
}
private static void puzzle(int[][] maze, Point p, ListPoint route) {
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;
}
}
public class Maze { private int[][] maze = null;
private int[] xx = { 1, 0, -1, 0 };
private int[] yy = { 0, 1, 0, -1 };
private Queue queue = null; public Maze(int[][] maze) {
this.maze = maze;
queue = new Queue(maze.length * maze.length);
} public void go() {
Point outPt = new Point(maze.length - 1, maze[0].length - 1);
Point curPt = new Point(0, 0);
Node curNode = new Node(curPt, null);
maze[curPt.x][curPt.y] = 2;
queue.entryQ(curNode); while (!queue.isEmpty()) {
curNode = queue.outQ();
for (int i = 0; i xx.length; ++i) {
Point nextPt = new Point();
nextPt.x = (curNode.point).x + xx[i];
nextPt.y = (curNode.point).y + yy[i];
if (check(nextPt)) {
Node nextNode = new Node(nextPt, curNode);
queue.entryQ(nextNode);
maze[nextPt.x][nextPt.y] = 2;
if (nextPt.equals(outPt)) {
java.util.StackNode stack = new java.util.StackNode();
stack.push(nextNode);
while ((curNode = nextNode.previous) != null) {
nextNode = curNode;
stack.push(curNode);
}
System.out.println("A Path is:");
while (!stack.isEmpty()) {
curNode = stack.pop();
System.out.println(curNode.point);
}
return;
}
}
}
}
System.out.println("Non solution!");
} private boolean check(Point p) {
if (p.x 0 || p.x = maze.length || p.y 0 || p.y = maze[0].length) {
return false;
}
if (maze[p.x][p.y] != 0) {
return false;
}
return true;
} public static void main(String[] args) {
int[][] maze = {
{ 0, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 0, 1, 1, 1, 0, 0, 0, 1, 0 },
{ 0, 1, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 1, 1 },
{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 },
{ 0, 1, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 1, 0, 1, 0, 0 }
};
new Maze(maze).go();
} private class Queue { Node[] array = null;
int size = 0;
int len = 0;
int head = 0;
int tail = 0; public Queue(int n) {
array = new Node[n + 1];
size = n + 1;
} public boolean entryQ(Node node) {
if (isFull()) {
return false;
}
tail = (tail + 1) % size;
array[tail] = node;
len++;
return true;
} public Node outQ() {
if (isEmpty()) {
return null;
}
head = (head + 1) % size;
len--;
return array[head];
} public boolean isEmpty() {
return (len == 0 || head == tail) ? true : false;
} public boolean isFull() {
return ((tail + 1) % size == head) ? true : false;
}
} private class Node { Point point = null;
Node previous = null; public Node() {
this(null,null);
} public Node(Point point, Node node) {
this.point = point;
this.previous = node;
}
} private class Point { int x = 0;
int y = 0; public Point() {
this(0, 0);
} public Point(int x, int y) {
this.x = x;
this.y = y;
} public boolean equals(Point p) {
return (x == p.x) (y == p.y);
} @Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
}
我這是用c寫的。你可以看看,希望能幫助到你。
#include"stdlib.h"
#include"stdio.h"
#define N 50
#define M 50
int X;
int maze[N+2][M+2];
struct point{
int row,col,predecessor;
}queue[512];
int head=0,tail=0;
void shoudong_maze(int m,int n){
int i,j;
printf("\n\n");
printf("請按行輸入迷宮,0表示通路,1表示障礙:\n\n");
for(i=0;im;i++)
for(j=0;jn;j++)
scanf("%d",maze[i][j]);
}
void zidong_maze(int m,int n){
int i,j;
printf("\n迷宮生成中……\n\n");
system("pause");
for(i=0;im;i++)
for(j=0;jn;j++)
maze[i][j]=rand()%2;
//由于rand()產生的隨機數是從0到RAND_MAX
//RAND_MAX是定義在stdlib.h中的,其值至少為32767)
//要產生從X到Y的數,只需要這樣寫:k=rand()%(Y-X+1)+X;
}
void print_maze(int m,int n){
int i,j;
printf("\n迷宮生成結果如下:\n\n");
printf("迷宮入口\n");
printf("↓");
for(i=0;im;i++)
{printf("\n");
for(j=0;jn;j++)
{if(maze[i][j]==0) printf("□");
if(maze[i][j]==1) printf("■");}
}
printf("→迷宮出口\n");
}
void result_maze(int m,int n)
{ int i,j;
printf("迷宮通路(用☆表示)如下所示:\n\t");
for(i=0;im;i++)
{ printf("\n");
for(j=0;jn;j++)
{if(maze[i][j]==0||maze[i][j]==2) printf("□");
if(maze[i][j]==1) printf("■");
if(maze[i][j]==3) printf("☆");
}
}
}
void enqueue(struct point p)
{ queue[tail]=p;
tail++;
}
struct point dequeue()
{ head++;
return queue[head-1];
}
int is_empty()
{ return head==tail;
}
void visit(int row,int col,int maze[52][52])
{ struct point visit_point={row,col,head-1};
maze[row][col]=2;
enqueue(visit_point);
}
int mgpath(int maze[52][52],int m,int n)
{ X=1;
struct point p={0,0,-1};
if(maze[p.row][p.col]==1)
{ printf("\n===============================================\n");
printf("此迷宮無解\n\n");X=0;return 0;}
maze[p.row][p.col]=2;
enqueue(p);
while(!is_empty())
{p=dequeue();
if((p.row==m-1)(p.col==n-1)) break;
if((p.col+1n)(maze[p.row][p.col+1]==0)) visit(p.row,p.col+1,maze);
if((p.row+1m)(maze[p.row+1][p.col]==0)) visit(p.row+1,p.col,maze);
if((p.col-1=0)(maze[p.row][p.col-1]==0)) visit(p.row,p.col-1,maze);
if((p.row-1=0)(maze[p.row-1][p.col]==0)) visit(p.row-1,p.col,maze);
}
if(p.row==m-1p.col==n-1)
{printf("\n==================================================================\n");
printf("迷宮路徑為:\n");
printf("(%d,%d)\n",p.row,p.col);
maze[p.row][p.col]=3;
while(p.predecessor!=-1)
{p=queue[p.predecessor];
printf("(%d,%d)\n",p.row,p.col);
maze[p.row][p.col]=3;
}
}
else {printf("\n=============================================================\n");
printf("此迷宮無解!\n\n");X=0;}
return 0;
}
int main()
{int i,m,n,cycle=0;
while(cycle!=(-1))
{
printf("********************************************************************************\n");
printf(" ☆歡迎進入迷宮求解系統☆\n");
printf(" 設計者:尹旭 林靜波(信息2班)\n");
printf("********************************************************************************\n");
printf(" 手動生成迷宮 請按:1\n");
printf(" 自動生成迷宮 請按:2\n");
printf(" 退出 請按:3\n\n");
printf("********************************************************************************\n");
printf("\n");
printf("請選擇你的操作:\n");
scanf("%d",i);
switch(i)
{case 1:printf("\n請輸入行數:");
scanf("%d",m);
printf("\n");
printf("請輸入列數:");
scanf("%d",n);
while((m=0||m50)||(n=0||n50))
{ printf("\n抱歉,你輸入的行列數超出預設范圍(0-50,0-50),請重新輸入:\n\n");
printf("請輸入行數:");
scanf("%d",m);
printf("\n");
printf("請輸入列數:");
scanf("%d",n);
}
shoudong_maze(m,n);
print_maze(m,n);
mgpath(maze,m,n);
if(X!=0)
result_maze(m,n);
printf("\n\nPress Enter Contiue!\n");
getchar();
while(getchar()!='\n');
break;
case 2:printf("\n請輸入行數:");
scanf("%d",m);
printf("\n");
printf("請輸入列數:");
scanf("%d",n);
while((m=0||m50)||(n=0||n50))
{printf("\n抱歉,你輸入的行列數超出預設范圍(0-50,0-50),請重新輸入:\n\n");
printf("請輸入行數:");
scanf("%d",m);
printf("\n");
printf("請輸入列數:");
scanf("%d",n);
}
zidong_maze(m,n);
print_maze(m,n);
mgpath(maze,m,n);
if(X!=0)
result_maze(m,n);
printf("\n\nPress Enter Contiue!\n");getchar();while(getchar()!='\n');break;
case 3:cycle=(-1);
break;
default:printf("\n");
printf("你的輸入有誤!\n");
printf("\nPress Enter Contiue!\n");
getchar();
while(getchar()!='\n');break;
}
}
}
//作者: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;
}
}
package 走迷宮;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
// 迷宮
public class Maze extends JFrame implements ActionListener {
private JPanel panel;
private JPanel northPanel;
private JPanel centerPanel;
private MazeGrid grid[][];
private JButton restart;
private JButton dostart;
private int rows;// rows 和cols目前暫定只能是奇數
private int cols;
private ListString willVisit;
private ListString visited;
private LinkedListString comed;
private long startTime;
private long endTime;
public Maze() {
rows = 25;
cols = 25;
willVisit = new ArrayListString();
visited = new ArrayListString();
comed = new LinkedListString();
init();
this.setTitle("回溯法--走迷宮");
this.add(panel);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init() {
panel = new JPanel();
northPanel = new JPanel();
centerPanel = new JPanel();
panel.setLayout(new BorderLayout());
restart = new JButton("重新生成迷宮");
dostart = new JButton("開始走迷宮");
grid = new MazeGrid[rows][cols];
centerPanel.setLayout(new GridLayout(rows, cols, 1, 1));
centerPanel.setBackground(new Color(0, 0, 0));
northPanel.add(restart);
northPanel.add(dostart);
dostart.addActionListener(this);
restart.addActionListener(this);
for (int i = 0; i grid.length; i++)
for (int j = 0; j grid[i].length; j++) {
if (j % 2 == 0 i % 2 == 0)
grid[i][j] = new MazeGrid(true, 20, 20);
else
grid[i][j] = new MazeGrid(false, 20, 20);
}
grid[0][0].setVisited(true);
grid[0][0].setPersonCome(true);
grid[0][0].setStart(true);
visited.add("0#0");
grid[rows - 1][cols - 1].setEnd(true);
grid = createMap(grid, 0, 0);
for (int i = 0; i grid.length; i++)
for (int j = 0; j grid[i].length; j++) {
grid[i][j].repaint();
centerPanel.add(grid[i][j]);
}
panel.add(northPanel, BorderLayout.NORTH);
panel.add(centerPanel, BorderLayout.CENTER);
}
/**
* 生成迷宮
*
* @param mazeGrid
* @param x
* @param y
* @return
*/
public MazeGrid[][] createMap(MazeGrid mazeGrid[][], int x, int y) {
int visitX = 0;
int visitY = 0;
if (x - 2 = 0) {
if (!mazeGrid[x - 2][y].isVisited()) {
willVisit.add((x - 2) + "#" + y);
}
}
if (x + 2 cols) {
if (!mazeGrid[x + 2][y].isVisited()) {
willVisit.add((x + 2) + "#" + y);
}
}
if (y - 2 = 0) {
if (!mazeGrid[x][y - 2].isVisited()) {
willVisit.add(x + "#" + (y - 2));
}
}
if (y + 2 rows) {
if (!mazeGrid[x][y + 2].isVisited()) {
willVisit.add(x + "#" + (y + 2));
}
}
if (!willVisit.isEmpty()) {
int visit = (int) (Math.random() * willVisit.size());
String id = willVisit.get(visit);
visitX = Integer.parseInt(id.split("#")[0]);
visitY = Integer.parseInt(id.split("#")[1]);
mazeGrid[(visitX + x) / 2][(visitY + y) / 2].setMark(true);
mazeGrid[visitX][visitY].setVisited(true);
if (!visited.contains(id)) {// 將這個點加到已訪問中去
visited.add(id);
}
willVisit.clear();
createMap(mazeGrid, visitX, visitY);
} else {
if (!visited.isEmpty()) {
String id = visited.remove(visited.size() - 1);// 取出最后一個元素
visitX = Integer.parseInt(id.split("#")[0]);
visitY = Integer.parseInt(id.split("#")[1]);
mazeGrid[visitX][visitY].setVisited(true);
createMap(mazeGrid, visitX, visitY);
}
}
return mazeGrid;
}
/**
* 走迷宮
*
* @param mazeGrid
* @param x
* @param y
*/
public String goMaze(MazeGrid mazeGrid[][], int x, int y) {
int comeX = 0;
int comeY = 0;
// left
if (x - 1 = 0) {
if (mazeGrid[x - 1][y].isMark()) {
if (!comed.contains((x - 1) + "#" + y))
willVisit.add((x - 1) + "#" + y);
}
}
// right
if (x + 1 cols) {
if (mazeGrid[x + 1][y].isMark()) {
if (!comed.contains((x + 1) + "#" + y))
willVisit.add((x + 1) + "#" + y);
}
}
// up
if (y - 1 = 0) {
if (mazeGrid[x][y - 1].isMark()) {
if (!comed.contains(x + "#" + (y - 1)))
willVisit.add(x + "#" + (y - 1));
}
}
// down
if (y + 1 rows) {
if (mazeGrid[x][y + 1].isMark()) {
if (!comed.contains(x + "#" + (y + 1)))
willVisit.add(x + "#" + (y + 1));
}
}
if (!willVisit.isEmpty()) {
int visit = (int) (Math.random() * willVisit.size());
String id = willVisit.get(visit);
comeX = Integer.parseInt(id.split("#")[0]);
comeY = Integer.parseInt(id.split("#")[1]);
mazeGrid[x][y].setPersonCome(false);
mazeGrid[comeX][comeY].setPersonCome(true);
mazeGrid[x][y].repaint();
mazeGrid[comeX][comeY].repaint();
willVisit.clear();
comed.add(x + "#" + y);
} else {
if (!comed.isEmpty()) {
String id = comed.removeLast();
comeX = Integer.parseInt(id.split("#")[0]);
comeY = Integer.parseInt(id.split("#")[1]);
mazeGrid[x][y].setPersonCome(false);
mazeGrid[comeX][comeY].setPersonCome(true);
mazeGrid[x][y].repaint();
mazeGrid[comeX][comeY].repaint();
comed.addFirst(x + "#" + y);
}
}
return comeX + "#" + comeY;
}
int comeX = 0;
int comeY = 0;
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("重新生成迷宮")) {
refreshMap(grid);
} else if (e.getActionCommand().equals("開始走迷宮")) {
startTime = System.currentTimeMillis();
dostart.setVisible(false);
restart.setText("禁止刷新");
int delay = 1000;
int period = 500;// 循環間隔
java.util.Timer timer = new java.util.Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
if (grid[rows - 1][cols - 1].isPersonCome()) {
endTime = System.currentTimeMillis();
JOptionPane.showMessageDialog(null, "已經走出迷宮,耗時"
+ (endTime - startTime) / 1000 + "秒", "消息提示",
JOptionPane.ERROR_MESSAGE);
this.cancel();
restart.setText("重新生成迷宮");
} else {
String id = goMaze(grid, comeX, comeY);
comeX = Integer.parseInt(id.split("#")[0]);
comeY = Integer.parseInt(id.split("#")[1]);
}
}
}, delay, period);
}
}
/**
* 刷新地圖
*/
public void refreshMap(MazeGrid mazeGrid[][]) {
comeX = 0;
comeY = 0;
willVisit.clear();
visited.clear();
comed.clear();
this.remove(panel);
init();
this.add(panel);
this.pack();
this.setVisible(true);
}
public static void main(String args[]) {
long start = System.currentTimeMillis();
new Maze();
long end = System.currentTimeMillis();
System.out.println("使用ArrayList生成迷宮耗時:" + (end - start) + "毫秒");
}
}
#includestdio.h
#includeconio.h
#includewindows.h
#includetime.h
#define?height?31?//高度為奇數
#define?width?25?//寬度為奇數
#define?Wall?1
#define?Road?0
#define?Start?2
#define?End?3
#define?Esc?5
#define?Up?1
#define?Down?2
#define?Left?3
#define?Right?4
int?map[height+2][width+2];
void?gotoxy(int?x,int?y)//移動坐標
{
COORD?coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void?hidden()//隱藏光標
{
HANDLE?hOut=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO?cci;
GetConsoleCursorInfo(hOut,cci);
cci.bVisible=0;//賦1為顯示?,賦0為隱藏
SetConsoleCursorInfo(hOut,cci);
}
void?create(int?x,int?y)//隨機生成迷宮
{
int?c[4][2]={0,1,1,0,0,-1,-1,0};//四個方向
int?i?,j,t;//將方向打亂
for(i=0;i4;i++)
{???
j=rand()%4;
t=c[i][0];c[i][0]=c[j][0];c[j][0]=t;
t=c[i][1];c[i][1]=c[j][1];c[j][1]=t;
}?
map[x][y]=Road;
for(i=0;i4;i++)
if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall)
{
map[x+c[i][0]][y+c[i][1]]=Road;
create(x+2*c[i][0],y+2*c[i][1]);
}
}
int?get_Key()//接受按鍵
{
char?c;
while(c=getch())
{
if(c==27)return?Esc;//Esc
if(c!=-32)continue;
c=getch();
if(c==72)return?Up;//上
if(c==80)return?Down;//下
if(c==75)return?Left;//左
if(c==77)return?Right;//右
}
return?0;
}
void?paint(int?x,int?y)//畫迷宮?
{
gotoxy(2*y-2,x-1);
switch(map[x][y])
{
case?Start:
printf("入");break;//畫入口?
case?End:
printf("出");break;//畫出口?
case?Wall:
printf("$");break;//畫檣
case?Road:
printf("");break;//畫路
}?
}
void?game()
{
int?x=2,y=1;//玩家開始位置
int?c;//接受按鍵
while(1)
{
gotoxy(2*y-1,x-1);
printf("※");//畫出玩家位置
if(map[x][y]==End)//判斷是否到達出口?
{
gotoxy(30,24);
printf("到達終點,按任意鍵結束");
getch();
break;
}
c=get_Key();
if(c==Esc)
{
gotoxy(0,24);
break;
}
switch(c)
{
case?Up://向上走
if(map[x-1][y]!=Wall)
{
paint(x,y);
x--;?
}
break;
case?Down://向下走
if(map[x+1][y]!=Wall)
{
paint(x,y);
x++;?
}?
break;
case?Left://向左走
if(map[x][y-1]!=Wall)
{
paint(x,y);
y--;
}?
break;
case?Right://向右走
if(map[x][y+1]!=Wall)
{
paint(x,y);
y++;
}?
break;
}
}
}
int?main()
{
int?i,j;
srand((unsigned)time(NULL));//初始化隨即種子?
hidden();//隱藏光標?
for(i=0;iheight+1;i++)
for(j=0;jheight+1;j++)?
if(i==0||i==(height+1)||j==0||j==(width+1))//初始化迷宮
map[i][j]=Road;
else?map[i][j]=Wall;
create(2*(rand()%(height/2)+1),2*(rand()%(width/2)+1));//從隨即一個點生成迷宮
for(i=0;i=height+1;i++)//邊界處理
{
map[i][0]=Wall;
map[i][width+1]=Wall;
}?
for(j=0;jwidth+1;j++)//邊界處理
{
map[0][j]=Wall;
map[height+1][j]=Wall;
}?
map[2][1]=Start;//給定入口
map[height-1][width]=End;//給定出口
for(i=1;iheight;i++)
for(j=1;j=width;j++)//畫出迷宮
paint(i,j);
game();//開始游戲?
getch();
return?0;
}
文章標題:java生成迷宮代碼,java走迷宮代碼解析
鏈接地址:http://vcdvsql.cn/article24/heoice.html
成都網站建設公司_創新互聯,為您提供、App設計、搜索引擎優化、網站排名、Google、做網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯