本文目录一览:
- 1、急需一java高手帮忙写一迷宫程序
- 2、关于Java走迷宫的问题。我已经有相关代码了,但是我看不懂。麻烦高手帮忙注释一下,然后再修改点儿。
- 3、JAVA走迷宫小游戏
- 4、求Java关于迷宫的算法(用栈实现)
- 5、Java迷宫算法问题(用栈实现)有算法简述
- 6、java里的awt是什么意思?
急需一java高手帮忙写一迷宫程序
给你代码,你给出的那两个类,不能满足,我的需要,我就没有使用。
你看一下吧。
----------------------------------------
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;
}
}
关于Java走迷宫的问题。我已经有相关代码了,但是我看不懂。麻烦高手帮忙注释一下,然后再修改点儿。
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) + "毫秒");
}
}
JAVA走迷宫小游戏
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
class JPanelEa1 extends JFrame implements MenuListener,ActionListener
{
JPanel mainp,p1,p2,p3,p4;
JTextField jt1;
JMenuItem mnuCopy;
JMenuItem mnuPaste;
JButton bM;
boolean isDouble = false;// 是否为实数
int opFlag = -1;
static double t1 = 0, t2 = 0, t3 = 0, result = 0;
static int opflag1 = -1, opflag2 = -1, flag = 0, resflag = 1;
int preOp, currentOp = 0;// 标准位
double op1 = 0, op2 = 0;// 操作数
double n3;
StringBuffer buf = new StringBuffer(20);
StringBuffer copyBoard = new StringBuffer(20);// 剪贴板
StringBuffer memory = new StringBuffer(20);// M系列
StringBuffer str = new StringBuffer();
public JPanelEa1()
{
p1=new JPanel();
p1.setLayout(new GridLayout(2,1,10,10));
JMenuBar mnuNotepad=new JMenuBar();
JMenu mnuEdit=new JMenu("编辑(E)");
mnuEdit.setMnemonic(KeyEvent.VK_E);
JMenu mnuCheck=new JMenu("查看(V)");
mnuCheck.setMnemonic(KeyEvent.VK_V);
JMenu mnuHelp=new JMenu("帮助(H)");
mnuCopy=new JMenuItem("复制(C)");
mnuPaste=new JMenuItem("粘贴(P)");
JMenuItem mnuVisit=new JMenuItem("查看帮助(V)");
JMenuItem mnuAbout=new JMenuItem("关于计算器(A)");
JSeparator sep=new JSeparator();
jt1=new JTextField("0.");
jt1.setEnabled(false);
jt1.setHorizontalAlignment(JTextField.RIGHT);
mnuEdit.addMenuListener(this);
mnuCheck.addMenuListener(this);
mnuHelp.addMenuListener(this);
mnuCopy.addActionListener(this);
mnuPaste.addActionListener(this);
mnuVisit.addActionListener(this);
mnuAbout.addActionListener(this);
mnuNotepad.add(mnuEdit);
mnuNotepad.add(mnuCheck);
mnuNotepad.add(mnuHelp);
mnuEdit.add(mnuCopy);
mnuEdit.add(mnuPaste);
mnuEdit.add(sep);
mnuHelp.add(mnuVisit);
mnuHelp.add(sep);
mnuHelp.add(mnuAbout);
p1.add(mnuNotepad);
p1.add(jt1);
p2=new JPanel();
p2.setLayout(new FlowLayout(FlowLayout.CENTER));
bM = new JButton(" ");
bM.setEnabled(false);
p2.add(bM);
Button b25=new Button("Backspace");
b25.addActionListener(this);
Button b26=new Button("CE");
b26.addActionListener(this);
Button b27=new Button("C");
b27.addActionListener(this);
p2.add(b25);
p2.add(b26);
p2.add(b27);
p3=new JPanel();
p3.setLayout(new GridLayout(4,6));
Button button1=new Button("MC");
button1.addActionListener(this);
Button button2=new Button("7");
button2.addActionListener(this);
Button button3=new Button("8");
button3.addActionListener(this);
Button button4=new Button("9");
button4.addActionListener(this);
Button button5=new Button("/");
button5.addActionListener(this);
Button button6=new Button("sqrt");
button6.addActionListener(this);
Button button7=new Button("MR");
button7.addActionListener(this);
Button button8=new Button("4");
button8.addActionListener(this);
Button button9=new Button("5");
button9.addActionListener(this);
Button button10=new Button("6");
button10.addActionListener(this);
Button button11=new Button("*");
button11.addActionListener(this);
Button button12=new Button("%");
button12.addActionListener(this);
Button button13=new Button("MS");
button13.addActionListener(this);
Button button14=new Button("1");
button14.addActionListener(this);
Button button15=new Button("2");
button15.addActionListener(this);
Button button16=new Button("3");
button16.addActionListener(this);
Button button17=new Button("-");
button17.addActionListener(this);
Button button18=new Button("1/x");
button18.addActionListener(this);
Button button19=new Button("M+");
button19.addActionListener(this);
Button button20=new Button("0");
button20.addActionListener(this);
Button button21=new Button("+/-");
button21.addActionListener(this);
Button button22=new Button(".");
button22.addActionListener(this);
Button button23=new Button("+");
button23.addActionListener(this);
Button button24=new Button("=");
button24.addActionListener(this);
p3.add(button1);p3.add(button2);
p3.add(button3);p3.add(button4);
p3.add(button5);p3.add(button6);
p3.add(button7);p3.add(button8);
p3.add(button9);p3.add(button10);
p3.add(button11);p3.add(button12);
p3.add(button13);p3.add(button14);
p3.add(button15);p3.add(button16);
p3.add(button17);p3.add(button18);
p3.add(button19);p3.add(button20);
p3.add(button21);p3.add(button22);
p3.add(button23);p3.add(button24);
setSize(250,150);
mainp=new JPanel();
mainp.setLayout(new GridLayout(3,1));
mainp.add(p1);
mainp.add(p2);
mainp.add(p3);
setContentPane(mainp);
setTitle("计算器");
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String s= e.getActionCommand();
if (s.equals("复制(C)")) {
String temp = jt1.getText().trim();
copyBoard.replace(0, copyBoard.length(), temp);
mnuPaste.setEnabled(true);
} else if (s.equals("粘贴(p)")) {
jt1.setText(copyBoard.toString());
} else if (s.equals("CE")) {
// 如果是CE则清除文本框
jt1.setText("0.");
} else if (s.equals("Backspace")) {
if (!jt1.getText().trim().equals("0.")) {
// 如果文本框中有内容
if (str.length() != 1 str.length() != 0) {
jt1.setText(str.delete(str.length() - 1, str.length())
.toString());
} else {
jt1.setText("0.");
str.setLength(0);
}
}
op2 = Double.parseDouble(jt1.getText().trim());
} else if (s.equals("C")) {
// 如果是C删除当前计算
jt1.setText("0.");
op1 = op2 = 0;
str.replace(0, str.length(), " ");
preOp = currentOp = 0;
} else if (s.equals("MC")) {
// 如果是MC则清除缓冲区
String temp = "";
memory.replace(0, memory.length(), temp);
bM.setText(" ");
} else if (s.equals("MR")) {
// 如果按键为MR则恢复缓冲区的数到文本框
jt1.setText(memory.toString());
} else if (s.equals("MS")) {
// 如果按键为MS则将文本框的数存入缓冲区
String s1 = jt1.getText().trim();
memory.replace(0, memory.length(), s1);
bM.setText("M");
} else if (s.equals("M+")) {
// 如果按键为MS则将文本框值与缓冲区的数相加但不显示结果
String temp1 = jt1.getText().trim();
double dtemp = Double.parseDouble(temp1);
String temp2 = memory.toString();
dtemp += Double.parseDouble(temp2);
temp1 = String.valueOf(dtemp);
memory.replace(0, memory.length(), temp1);
} else if (s.equals("1/x")) {
// 如果按键为1/x则将文本框中的数据为它的倒数
String temp = jt1.getText().trim();
double dtemp = Double.parseDouble(temp);
jt1.setText("" + 1 / dtemp);
} else if (s.equals("sqrt")) {
// 如果按键为sqrt则将文本框中的内容求平方根
String temp = jt1.getText().trim();
double dtemp = Double.parseDouble(temp);
jt1.setText("" + Math.sqrt(dtemp));
} else if (s.equals("+")) {
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 1;
op2 = 0;
jt1.setText("" + op1);
} else {
currentOp = preOp;
preOp = 1;
switch (currentOp) {
case 1:
op1 += op2;
jt1.setText("" + op1);
break;
case 2:
op1 -= op2;
jt1.setText("" + op1);
break;
case 3:
op1 *= op2;
jt1.setText("" + op1);
break;
case 4:
op1 /= op2;
jt1.setText("" + op1);
break;
}
}
} else if (s.equals("-")) {
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 2;// op1=op2;op2=0;
jt1.setText("" + op1);
} else {
currentOp = preOp;
preOp = 2;
switch (currentOp) {
case 1:
op1 = op1 + op2;
jt1.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
jt1.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
jt1.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
jt1.setText("" + op1);
break;
}
}
} else if (s.equals("*"))// *
{
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 3;// op1=op2;op2=1;
jt1.setText("" + op1);// op1=op2;
} else {
currentOp = preOp;
preOp = 3;
switch (currentOp) {
case 1:
op1 = op1 + op2;
jt1.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
jt1.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
jt1.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
jt1.setText("" + op1);
break;
}
}
} else if (s.equals("/"))// /
{
str.setLength(0);
if (currentOp == 0) {
preOp = currentOp = 4;// op2=1;
jt1.setText("" + op1);// op1=op2;
} else {
currentOp = preOp;
preOp = 4;
switch (currentOp) {
case 1:
op1 = op1 + op2;
jt1.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
jt1.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
jt1.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
jt1.setText("" + op1);
break;
}
}
} else if (s.equals("="))// =
{
if (currentOp == 0) {
str.setLength(0);
jt1.setText("" + op2);
} else {
str.setLength(0);
currentOp = preOp;
switch (currentOp) {
case 1:
op1 = op1 + op2;
jt1.setText("" + op1);
break;
case 2:
op1 = op1 - op2;
jt1.setText("" + op1);
break;
case 3:
op1 = op1 * op2;
jt1.setText("" + op1);
break;
case 4:
op1 = op1 / op2;
jt1.setText("" + op1);
break;
}
currentOp = 0;
op2 = 0;
}
} else if (s.equals(".")) {
isDouble = true;
if (jt1.getText().trim().indexOf('.') != -1)
;
else {
if (jt1.getText().trim().equals("0")) {
str.setLength(0);
jt1.setText((str.append("0" + s)).toString());
}
// else
// if(jt1.getText().trim().equals("")){}//如果初时显示为空则不做任何操作
else {
jt1.setText((str.append(s)).toString());
}
}
} else if (s.equals("0"))// 如果选择的是"0"这个数字键
{
if (jt1.getText().trim().equals("0.")) {
} else {
jt1.setText(str.append(s).toString());
op2 = Double.parseDouble(jt1.getText().trim());
}
} else {
jt1.setText(str.append(s).toString());
op2 = Double.parseDouble(jt1.getText().trim());
if (currentOp == 0)
op1 = op2;
}
}
public static void main(String args[])
{
JPanelEa1 g=new JPanelEa1();
}
@Override
public void menuSelected(MenuEvent e) {
// TODO Auto-generated method stub
}
@Override
public void menuDeselected(MenuEvent e) {
// TODO Auto-generated method stub
}
@Override
public void menuCanceled(MenuEvent e) {
// TODO Auto-generated method stub
}
}
求Java关于迷宫的算法(用栈实现)
package com.Albert.LabyringhStack;
public class Point {
int x;
int y;
int direction; //direction指向此点附近的一个点 应该有四个 编号为1 2 3 4
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;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public void addDirection(){
this.direction++;
}
public Point() {
}
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
this.direction = 1;
}
public Point(int x, int y, int direction) {
super();
this.x = x;
this.y = y;
this.direction = direction;
}
}
package com.Albert.LabyringhStack;
import java.util.*;
public class LabyringhStack {
public Point S;
public Point F;
char[][] mazemap;
StackPoint path;
public LabyringhStack() {
}
public LabyringhStack(char[][] ma) { //初始化 存入数组
this.mazemap = new char[ma.length][ma[0].length];
for(int i=0;ima.length;i++){
for(int j=0;jma[0].length;j++){ //mazemap[0]必须有元素 不可为空
this.mazemap[i][j] = ma[i][j];
}
}
S = returnPlace('S');
F = returnPlace('F');
}
public Point returnPlace(char s){ //返回数组中字符的位置
Point point = new Point();
for(int i=0;ithis.mazemap.length;i++){
for(int j=0;jthis.mazemap[0].length;j++){ //mazemap[0]必须有元素 不可为空
if(this.mazemap[i][j]==s)
{ point.setX(i);
point.setY(j);
point.setDirection(1);
}
}
}
return point;
}
public char returnChar(Point point){
if(point.getX()=0point.getY()=0)
return this.mazemap[point.getX()][point.getY()];
else
return '#';
}
public void replacePlace(Point point, char s){ //更改特定位置处的字符
mazemap[point.getX()][point.getY()] = s;
}
public void printPath(){
StackPoint tempPath = new StackPoint();
while(!path.empty()){ //对栈进行反序
tempPath.push(path.pop());
}
while(!tempPath.empty()){
System.out.print("("+tempPath.peek().getX()+","+tempPath.pop().getY()+")");
}
}
public boolean getPath(){ //取得路径的算法 如果有路径就返回真
path = new StackPoint();
S.setDirection(1);
path.push(S);
replacePlace(S, 'X');
while(!path.empty()){
Point nowPoint = path.peek(); //取得当前位置
if(nowPoint.getX()==F.getX()nowPoint.getY()==F.getY()){
//printPath();
return true;
}
Point temp = new Point(); //存放下一个可走的位置
int find = 0; //标志 是否可向下走
while(nowPoint.getDirection()5find==0){
switch(nowPoint.getDirection()){
case 1:temp = new Point(nowPoint.getX(),nowPoint.getY()-1,1); break; //取得当前位置左边的位置
case 2:temp = new Point(nowPoint.getX()+1,nowPoint.getY(),1); break;//取得当前位置下边的位置
case 3:temp = new Point(nowPoint.getX(),nowPoint.getY()+1,1); break;//取得当前位置右边的位置
case 4:temp = new Point(nowPoint.getX()-1,nowPoint.getY(),1); break;//取得当前位置上边的位置
}
nowPoint.addDirection(); //指向下一个需要验证的点
if(returnChar(temp)=='O'||returnChar(temp)=='F') find = 1; //如果能向下走则置为1
}
if(find==1){ //如果可走就进栈
replacePlace(temp, 'X'); //设置成X 防止回走
// printArr();
path.push(temp);
}else{ //如果不可走就退栈
replacePlace(nowPoint, 'O');
path.pop();
}
}
return false;
}
public void printArr(){
for(int i=0;imazemap.length;i++){
for(int j=0;jmazemap[0].length;j++){ //mazemap[0]必须有元素 不可为空
System.out.print(mazemap[i][j]);
}
System.out.println();
}
System.out.println();
}
}
package com.Albert.LabyringhStack;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
char[][] mazemap = {
{'M','M','M','M','M','M','M','M'},
{'M','S','O','O','M','M','M','M'},
{'M','M','M','O','M','M','M','M'},
{'M','M','O','O','O','O','M','M'},
{'M','M','M','M','M','F','M','M'},
{'M','M','M','M','M','M','M','M'},
{'M','M','M','M','M','M','M','M'}
};
LabyringhStack solution = new LabyringhStack(mazemap);
if(solution.getPath()){
System.out.print("迷宫路径如下:");
solution.printPath();
}
else {
System.out.println("没有可走的路");
}
}
}
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
WOOOWWWWWWWWWWWWOOWWWWWWWWWWWWOOOOOOOOOOOOWWWWWWWWWOOOOOWW
WOOWWWWWWWWWWWWWOOWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWWOOOW
WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWOOW
WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW
WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWWFW
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW
WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOWWWWWWWWWWWWWOOOOOOW
WWWWWWOOOOOOOOOOOOWWWWWWWOOOOOOOOOOOOWWWWWWWWWWWWWWWWOOOOW
WOOOOOOWWWWWWWWWWWWWWOOOOOOOOOOOWWWWWWWWWWWWWWWWWWWWWWWWWW
WOOOOWWWWWWWOOOOOOWWWWOOOOOOWWWWWWWWWWWOOOOOOOOOOOOOOOOOWW
WOOOWWWWWWWWWWWWOOWWWWWWWWWWWWOOOOOOOOOOOOWWWWWWWWWOOOOOWW
WOOWWWWWWWWWWWWWOOWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWWOOOW
WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWOOW
WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW
WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWWFW
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
java里的awt是什么意思?
AWT是Abstract Windowing Toolkit 的缩写 意思是:Java抽象窗口工具,通过这组类你只需进行一次代码开发,就可以移植到许多平台。要使用AWT,在工程的开头使用import java.awt.*语句将其导入。
这将允许你使用TextComponent组件,例如:Buttons、Scrollbars、Canvas、CheckBoxes、Lists和Label。Microsoft Visual J++近来宣布的应用程序基本类(AFC)也是作为AWT的一个扩展,从而使Java开发环境与带有微软基本类(MFC)库的Microsoft Visual C++更加接近。