用二维数组去模拟迷宫 java实现,如题。在线等。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是一段vc的代码。可是我看的不是很懂。哪位高手能帮我改成java的。谢谢。分不是问题。数组可以初始化成5行5列的。谢谢void mazePath(int* maze[],int* direction[],int x1,int y1,int x2,int y2)/* 迷宫maze[M][N]中求从入口maze[x1][y1]到出口maze[x2][y2]的一条路径其中 1<=x1,x2<=M-2 , 1<=y1,y2<=N-2 */{ int i,j,k,g,h; PSeqStack st; DataType element;st = createEmptyStack_seq( ); maze[x1][y1] = 2;pushtostack(st, x1, y1, -1); /* 入口点进栈 */while (! isEmptyStack_seq(st)) /* 走不通时,一步步回退 */{ element = top_seq(st); pop_seq(st);i = element.x; j = element.y; k = element.d + 1;while (k<=3) /* 依次试探每个方向 */{ g = i + direction[k][0]; h = j + direction[k][1];if (g==x2 && h==y2 && maze[g][h]==0) /* 走到出口点 */{ printpath(st, g, h, i, j); /* 打印路径 */return; }if (maze[g][h]==0) /* 走到没走过的点 */{ maze[g][h] = 2; /* 作标记 */pushtostack(st, i, j, k); /* 进栈 */i = g; j = h; k = -1; /* 下一点转换成当前点 */}k = k + 1; } }printf("The path has not been found.\n"); /* 栈退完,未找到路径 */}源程序的主函数#define M 8 /* maze数组的行数 */#define N 11 /* maze数组的列数 */#define MAXNUM 54 /*栈中保存元素个数,最多为(M-2)*(N-2)*//*假设maze[M][N]和direction[4][2]是全局数组,调用mazePath的主程序可以写成以下形式:*/int main(){ int *m[M] ; int * d[4];int i;for (i=0; i<M; i++)m[i]=maze[i]; /* m[i]中是maze第i行的开始地址 */for (i=0; i<4; i++)d[i]=direction[i];mazePath(m,d,1,1,6,9); /* mazePath要修改数组maze的值 */return 0;} 不知道楼主要模拟到什么程度,我先开个头,大家一起来讨论下吧。最简单的想法是用二维整数数组来模拟,如果用object数组来模拟,如果矩阵大了,而且反复引用计算时会非常慢。一个例子是我设计的“机器人守卫”题时,同学用C++和整数数组实现比我用object(自己设计的房间)数组(java的)快了很多,他的可以比我的多算一个维度。然后是数值设置,迷宫我想一般就设为“墙壁”和“通路”就可以了吧,用2个值表示,比如0和1。然后算法就一般的搜索算法就行了,向三个方向试探,如果是墙就返回,如此递归即可。 import java.io.*;import java.awt.*;import javax.swing.*;import java.math.*;import java.awt.event.*;//窗体class migong extends JFrame { public migong(String s) { super(s); this.getContentPane().add(new Table(), BorderLayout.CENTER); } public static void main(String args[]) { migong nf = new migong("迷宫"); nf.setSize(370, 315); // 使窗体无法改变大小 nf.setResizable(false); // 设置窗体在屏幕的中心 nf.setLocation(200, 100); nf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); nf.setVisible(true); }}// 面板class Table extends JPanel implements ActionListener { int way = 0; // //路 int wall = 1; // /墙 int east = 2; // /方向东 int south = 3; // /方向南 int west = 4; // //方向西 int north = 5; // //方向北 int no_way = 6; // /没有路 int have_way = 7;// /有路 boolean end = true;// 表示能够走通 static int x = 0; int record_way[] = new int[100]; // int map[][]=new int[15][15]; int map[][] = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 }, { 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1 }, { 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1 }, { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 }, { 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1 }, { 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1 }, { 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1 }, { 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }; // new三个按钮 JButton btnStart = new JButton("Start"); // 开始 JButton btnExit = new JButton("Exit"); // 退出 static int i; static int j; Image imgwall, imgeast, imgsouth, imgwest, imgno_way, imgnorth; // 构造函数 public Table() { // 空布局 this.setLayout(null); // 设置背景色(雪色) this.setBackground(new Color(255, 250, 250)); btnStart.setBounds(270, 40, 80, 20); // 开始按钮 btnExit.setBounds(270, 80, 80, 20); // 退出按钮 add(btnStart); btnStart.addActionListener(this); add(btnExit); btnExit.addActionListener(this); String imgwallname = "Image/wall.jpg";// String imgeastname = "Image/east.jpg"; String imgsouthname = "Image/south.jpg"; String imgwestname = "Image/west.jpg"; String imgno_wayname = "Image/no_way.jpg"; String imgnorthname = "Image/north.jpg"; imgwall = Toolkit.getDefaultToolkit().getImage(imgwallname); imgeast = Toolkit.getDefaultToolkit().getImage(imgeastname); imgsouth = Toolkit.getDefaultToolkit().getImage(imgsouthname); imgwest = Toolkit.getDefaultToolkit().getImage(imgwestname); imgno_way = Toolkit.getDefaultToolkit().getImage(imgno_wayname); imgnorth = Toolkit.getDefaultToolkit().getImage(imgnorthname); // 加提示 btnStart.setToolTipText("开始"); btnExit.setToolTipText("退出"); } // 绘画 public void paint(Graphics g) { super.paint(g); // 必须重写父类的这个方法,否则图形无法消除 g.setColor(Color.pink); for (i = 0; i < 15; i++) { for (j = 0; j < 15; j++) { switch (map[j][i]) // 根据标志画图 { case 0: break; case 1: g.drawImage(imgwall, 10 + i * (15 + 2) + 2, 15 + j * (15 + 2) + 2, this); break; case 2: g.drawImage(imgeast, 10 + i * (15 + 2) + 2, 15 + j * (15 + 2) + 2, this); break; case 3: g.drawImage(imgsouth, 10 + i * (15 + 2) + 2, 15 + j * (15 + 2) + 2, this); break; case 4: g.drawImage(imgwest, 10 + i * (15 + 2) + 2, 15 + j * (15 + 2) + 2, this); break; case 5: g.drawImage(imgnorth, 10 + i * (15 + 2) + 2, 15 + j * (15 + 2) + 2, this); break; case 6: g.drawImage(imgno_way, 10 + i * (15 + 2) + 2, 15 + j * (15 + 2) + 2, this); break; default: break; } } } } // 定时器的监听事件 public void actionPerformed(ActionEvent e) { // 如果是开始按钮 if (e.getSource() == btnStart) { start(); for (int jj = 0; jj < 99; jj++) // /打印结果!! { System.out.print(record_way[jj] + " "); System.out.println(record_way[jj++]); } } if (e.getActionCommand() == "Exit") // /退出 { System.exit(0); } } public void start() { go(1, 1, east); } public int go(int n, int m, int direction) // 坐标位置(n,m) { int record_noway = 0; int back; if (n == 13 && m == 13) { end = false; return have_way; } else {// //////////////////////////////////////////////东方向 if (map[n][m + 1] == way && direction != west && end) { map[n][m + 1] = east; repaint(); back = go(n, m + 1, east); record_noway += direction(n, m + 1, back); } else { record_noway++; } // /////////////////////////////////////////南方向 if (map[n + 1][m] == way && direction != north && end) { map[n + 1][m] = south; repaint(); back = go(n + 1, m, south); record_noway += direction(n + 1, m, back); } else { record_noway++; } // //////////////////////////////////////////////////////西方向 if (map[n][m - 1] == way && direction != east && end) { map[n][m - 1] = west; repaint(); back = go(n, m - 1, west); record_noway += direction(n, m - 1, back); } else { record_noway++; } // ///////////////////////////////////////////////////北方向 if (map[n - 1][m] == way && direction != south && end) { map[n - 1][m] = north; repaint(); back = go(n - 1, m, north); record_noway += direction(n - 1, m, back); } else { record_noway++; } // //////////////////////////////////////////////////////// return record_noway >= 3 ? no_way : have_way; } } public int direction(int n, int m, int back) // { if (back == no_way) { map[n][m] = no_way; repaint(); return 1; } else { record_way[x] = n; record_way[x + 1] = m; x = x + 2; return -2;// /有路与 return record_noway>=3?no_way:have_way;相应 } }}网上找的 你看看对你有用吗? myeclipse 配置tomcat中JDK问题 Random.nextInt() 如果用纯JDBC做DAO层的话? 求助如何用LinkedList实现堆栈功能? 一个递归的面试题有答案,可看不懂,请高手解析下原理 THINK IN JAVA 2对J2SE的学习帮助有多大? 简单问题: 谁能给我答案,我给500分,君子无戏言,否则我就别在这个坛子混了 jfreechart 柱状图怎么显示柱条的值? 我是一个刚刚学习java的菜鸟,写了一点程序就是不能执行,编译通过,看了老半天也没有看出毛病来! 救命贴:关于算法 为什么我在SWT程序中,使用JNI就不能找到对应的Dll文件阿
数组可以初始化成5行5列的。谢谢void mazePath(int* maze[],int* direction[],int x1,int y1,int x2,int y2)/* 迷宫maze[M][N]中求从入口maze[x1][y1]到出口maze[x2][y2]的一条路径其中 1<=x1,x2<=M-2 , 1<=y1,y2<=N-2 */{ int i,j,k,g,h; PSeqStack st; DataType element;st = createEmptyStack_seq( ); maze[x1][y1] = 2;pushtostack(st, x1, y1, -1); /* 入口点进栈 */while (! isEmptyStack_seq(st)) /* 走不通时,一步步回退 */{ element = top_seq(st); pop_seq(st);i = element.x; j = element.y; k = element.d + 1;while (k<=3) /* 依次试探每个方向 */{ g = i + direction[k][0]; h = j + direction[k][1];if (g==x2 && h==y2 && maze[g][h]==0) /* 走到出口点 */{ printpath(st, g, h, i, j); /* 打印路径 */return; }if (maze[g][h]==0) /* 走到没走过的点 */{ maze[g][h] = 2; /* 作标记 */pushtostack(st, i, j, k); /* 进栈 */i = g; j = h; k = -1; /* 下一点转换成当前点 */}k = k + 1; } }printf("The path has not been found.\n"); /* 栈退完,未找到路径 */}源程序的主函数#define M 8 /* maze数组的行数 */#define N 11 /* maze数组的列数 */#define MAXNUM 54 /*栈中保存元素个数,最多为(M-2)*(N-2)*//*假设maze[M][N]和direction[4][2]是全局数组,调用mazePath的主程序可以写成以下形式:*/int main(){ int *m[M] ; int * d[4];int i;for (i=0; i<M; i++)m[i]=maze[i]; /* m[i]中是maze第i行的开始地址 */for (i=0; i<4; i++)d[i]=direction[i];mazePath(m,d,1,1,6,9); /* mazePath要修改数组maze的值 */return 0;}
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.math.*;
import java.awt.event.*;//窗体
class migong extends JFrame {
public migong(String s) {
super(s);
this.getContentPane().add(new Table(), BorderLayout.CENTER);
} public static void main(String args[]) {
migong nf = new migong("迷宫");
nf.setSize(370, 315);
// 使窗体无法改变大小
nf.setResizable(false);
// 设置窗体在屏幕的中心 nf.setLocation(200, 100);
nf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
nf.setVisible(true);
}
}// 面板class Table extends JPanel implements ActionListener {
int way = 0; // //路 int wall = 1; // /墙 int east = 2; // /方向东 int south = 3; // /方向南 int west = 4; // //方向西 int north = 5; // //方向北 int no_way = 6; // /没有路 int have_way = 7;// /有路 boolean end = true;// 表示能够走通 static int x = 0; int record_way[] = new int[100]; // int map[][]=new int[15][15]; int map[][] = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 },
{ 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1 },
{ 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, }; // new三个按钮
JButton btnStart = new JButton("Start"); // 开始 JButton btnExit = new JButton("Exit"); // 退出 static int i; static int j; Image imgwall, imgeast, imgsouth, imgwest, imgno_way, imgnorth; // 构造函数
public Table() {
// 空布局
this.setLayout(null);
// 设置背景色(雪色)
this.setBackground(new Color(255, 250, 250));
btnStart.setBounds(270, 40, 80, 20); // 开始按钮
btnExit.setBounds(270, 80, 80, 20); // 退出按钮
add(btnStart);
btnStart.addActionListener(this);
add(btnExit);
btnExit.addActionListener(this);
String imgwallname = "Image/wall.jpg";//
String imgeastname = "Image/east.jpg";
String imgsouthname = "Image/south.jpg";
String imgwestname = "Image/west.jpg";
String imgno_wayname = "Image/no_way.jpg";
String imgnorthname = "Image/north.jpg"; imgwall = Toolkit.getDefaultToolkit().getImage(imgwallname);
imgeast = Toolkit.getDefaultToolkit().getImage(imgeastname);
imgsouth = Toolkit.getDefaultToolkit().getImage(imgsouthname);
imgwest = Toolkit.getDefaultToolkit().getImage(imgwestname);
imgno_way = Toolkit.getDefaultToolkit().getImage(imgno_wayname);
imgnorth = Toolkit.getDefaultToolkit().getImage(imgnorthname); // 加提示
btnStart.setToolTipText("开始");
btnExit.setToolTipText("退出"); } // 绘画
public void paint(Graphics g) { super.paint(g); // 必须重写父类的这个方法,否则图形无法消除 g.setColor(Color.pink); for (i = 0; i < 15; i++) {
for (j = 0; j < 15; j++) { switch (map[j][i]) // 根据标志画图
{
case 0:
break;
case 1:
g.drawImage(imgwall, 10 + i * (15 + 2) + 2, 15 + j
* (15 + 2) + 2, this);
break;
case 2:
g.drawImage(imgeast, 10 + i * (15 + 2) + 2, 15 + j
* (15 + 2) + 2, this);
break;
case 3:
g.drawImage(imgsouth, 10 + i * (15 + 2) + 2, 15 + j
* (15 + 2) + 2, this);
break;
case 4:
g.drawImage(imgwest, 10 + i * (15 + 2) + 2, 15 + j
* (15 + 2) + 2, this);
break;
case 5:
g.drawImage(imgnorth, 10 + i * (15 + 2) + 2, 15 + j
* (15 + 2) + 2, this);
break;
case 6:
g.drawImage(imgno_way, 10 + i * (15 + 2) + 2, 15 + j
* (15 + 2) + 2, this);
break; default:
break;
} }
} } // 定时器的监听事件
public void actionPerformed(ActionEvent e) {
// 如果是开始按钮
if (e.getSource() == btnStart) { start();
for (int jj = 0; jj < 99; jj++) // /打印结果!!
{
System.out.print(record_way[jj] + " ");
System.out.println(record_way[jj++]);
}
}
if (e.getActionCommand() == "Exit") // /退出
{
System.exit(0);
}
} public void start() {
go(1, 1, east); } public int go(int n, int m, int direction) // 坐标位置(n,m)
{
int record_noway = 0;
int back;
if (n == 13 && m == 13) {
end = false;
return have_way;
} else {// //////////////////////////////////////////////东方向
if (map[n][m + 1] == way && direction != west && end) { map[n][m + 1] = east;
repaint();
back = go(n, m + 1, east);
record_noway += direction(n, m + 1, back); } else {
record_noway++; } // /////////////////////////////////////////南方向
if (map[n + 1][m] == way && direction != north && end) { map[n + 1][m] = south;
repaint(); back = go(n + 1, m, south);
record_noway += direction(n + 1, m, back);
} else {
record_noway++; }
// //////////////////////////////////////////////////////西方向 if (map[n][m - 1] == way && direction != east && end) { map[n][m - 1] = west;
repaint();
back = go(n, m - 1, west);
record_noway += direction(n, m - 1, back);
} else {
record_noway++; }
// ///////////////////////////////////////////////////北方向
if (map[n - 1][m] == way && direction != south && end) { map[n - 1][m] = north;
repaint();
back = go(n - 1, m, north);
record_noway += direction(n - 1, m, back);
} else {
record_noway++; }
// ////////////////////////////////////////////////////////
return record_noway >= 3 ? no_way : have_way; } } public int direction(int n, int m, int back) //
{ if (back == no_way) {
map[n][m] = no_way;
repaint();
return 1;
} else {
record_way[x] = n;
record_way[x + 1] = m;
x = x + 2;
return -2;// /有路与 return record_noway>=3?no_way:have_way;相应
} }
}网上找的 你看看对你有用吗?