模拟最短路径寻路(JAVA界面)

时间:2022-12-16 22:45:13

大二的时候,数据结构老师留的第二个作业,总的来说收获很大,其中包含了bfs和Dijkstra。

首先是Map类:

package hjc;

public class Map {

final static int[][] MyMap =
{
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1 },
{ 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1 },
{ 1, 1, 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1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1 },
{ 1, 1, 0, 0, 0, 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, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
};

static int[] start = {15, 15};
static int[] aim={21, 24};

}
其中1和0分别代表了不可走和可走。

下面是MainView类:

package hjc;

import java.awt.BasicStroke;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;

import javax.swing.JOptionPane;

public class MainView extends Frame implements ActionListener, KeyListener {

/**
*
*/
private static final long serialVersionUID = 1L;

public static final int ScreenWidth = Toolkit.getDefaultToolkit()
.getScreenSize().width;
public static final int ScreenHeight = Toolkit.getDefaultToolkit()
.getScreenSize().height;
public static final int ExeWidth = 700;
public static final int ExeHeight = 560;
public static final int a = 15 ;

private Image offScreenImage = null;
private Image beijing = Toolkit.getDefaultToolkit().getImage(
"image/beijing.jpg");
private Image hua = Toolkit.getDefaultToolkit().getImage("image/hua.png");
Button Rand = new Button("生成新地图");
Button Randoa = new Button("自动随机起始点");
Button FindForGdyx = new Button("广度优先寻路");
Button FindForDijstra = new Button("迪杰斯特拉寻路");
Button suodingo = new Button("锁定");
Button suodinga = new Button("锁定");
Button clear = new Button("清空障碍");
TextField ta1 = new TextField("0");
TextField ta2 = new TextField("0");
TextField ta3 = new TextField("0");
TextField ta4 = new TextField("0");

int span = 20;
boolean isFind1 = false;
boolean isFind2 = false;
boolean isFinding = false;
int count1 = 0;
int count2 = 0;
int[] origin = Map.start;
int[] aim = Map.aim;
int[][] map = Map.MyMap;
int[][] length = new int[Map.MyMap.length][Map.MyMap[0].length];
int[][] visited = new int[Map.MyMap.length][Map.MyMap[0].length];
Queue<int[][]> queue = new LinkedList<int[][]>();
ArrayList<int[][]> searchProcess = new ArrayList<int[][]>();
HashMap<String, int[][]> searchResult1 = new HashMap<String, int[][]>();
HashMap<String, int[][]> searchResult2 = new HashMap<String, int[][]>();

int[][] dir1 = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
int[][] dir2 = { { 1, 1 }, { 1, -1 }, { -1, -1 }, { -1, 1 } };
int[][] dir3 = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 }, { 1, 1 },
{ 1, -1 }, { -1, -1 }, { -1, 1 } };

public static void main(String[] args) {

new MainView();

}

public void init() {

count1 = 0;
count2 = 0;
isFind1 = false;
isFind2 = false;
origin = Map.start;
aim = Map.aim;
queue = new LinkedList<int[][]>();
searchProcess = new ArrayList<int[][]>();
searchResult1 = new HashMap<String, int[][]>();
searchResult2 = new HashMap<String, int[][]>();
visited = new int[Map.MyMap.length][Map.MyMap[0].length];
length = new int[Map.MyMap.length][Map.MyMap[0].length];

repaint();

}

public MainView() {

this.setTitle("最短路径");
this.setLayout(null);
Image icon = Toolkit.getDefaultToolkit().getImage("image/icon.png");
this.setIconImage(icon);
this.setBounds((ScreenWidth - ExeWidth) / 2,
(ScreenHeight - ExeHeight) / 2, ExeWidth, ExeHeight);

Rand.setBounds(550, 100, 100, 30);
Randoa.setBounds(550, 350, 100, 30);
FindForDijstra.setBounds(550, 425, 100, 30);
FindForGdyx.setBounds(550, 500, 100, 30);
suodingo.setBounds(620, 180, 50, 20);
suodinga.setBounds(620, 250, 50, 20);
clear.setBounds(550, 300, 70, 20);
ta1.setBounds(540, 150, 50, 20);
ta2.setBounds(620, 150, 50, 20);
ta3.setBounds(540, 220, 50, 20);
ta4.setBounds(620, 220, 50, 20);

Rand.setBackground(Color.CYAN);
Randoa.setBackground(Color.CYAN);
FindForDijstra.setBackground(Color.ORANGE);
FindForGdyx.setBackground(Color.ORANGE);
suodingo.setBackground(Color.PINK);
suodinga.setBackground(Color.PINK);
clear.setBackground(Color.GRAY);

Rand.addActionListener(this);
Randoa.addActionListener(this);
FindForDijstra.addActionListener(this);
FindForGdyx.addActionListener(this);
suodingo.addActionListener(this);
suodinga.addActionListener(this);
clear.addActionListener(this);
ta1.addKeyListener(this);
ta2.addKeyListener(this);
ta3.addKeyListener(this);
ta4.addKeyListener(this);

this.add(ta1);
this.add(ta2);
this.add(ta3);
this.add(ta4);
this.add(FindForDijstra);
this.add(FindForGdyx);
this.add(Rand);
this.add(Randoa);
this.add(suodingo);
this.add(suodinga);
this.add(clear);

this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY() - 30;
System.out.println(x + "----" + y);
}
});

this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});

this.setResizable(false);
this.setVisible(true);

}

public void update(Graphics g) {

if (offScreenImage == null) {
offScreenImage = this.createImage(ExeWidth, ExeHeight);
} else {
offScreenImage = null;
offScreenImage = this.createImage(ExeWidth, ExeHeight);
}
Graphics goff = offScreenImage.getGraphics();
paint(goff);
goff.dispose();
g.drawImage(offScreenImage, 0, 0, null);

}

public void paint(Graphics g) {

Color c = g.getColor();

Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.BLACK);

map[origin[0]][origin[1]] = 3;
map[aim[0]][aim[1]] = 4;
count1 = 0;
count2 = 0;

//g2d.drawImage(beijing, 0, 0, this);
//g2d.drawImage(hua, 250, 200, this);

for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
if (Map.MyMap[i][j] == 0) {
g2d.setColor(Color.BLACK);
g2d.drawRect(20 + j * a, 50 + i * a, a, a);
} else if (Map.MyMap[i][j] == 1) {
g2d.setColor(Color.GRAY);
g2d.fillRect(20 + j * a, 50 + i * a, a, a);
} else if (Map.MyMap[i][j] == 2) {
g2d.setColor(Color.RED);
g2d.fillRect(20 + j * a, 50 + i * a, a, a);
} else if (Map.MyMap[i][j] == 3) {
g2d.setColor(Color.RED);
g2d.fillRect(20 + j * a, 50 + i * a, a, a);
} else if (Map.MyMap[i][j] == 4) {
g2d.setColor(Color.BLUE);
g2d.fillRect(20 + j * a, 50 + i * a, a, a);
}
}
}

for (int k = 0; k < searchProcess.size(); k++) {
int[][] edge = searchProcess.get(k);
int[] point1 = getPosition(edge[0][1], edge[0][0]);
int[] point2 = getPosition(edge[1][1], edge[1][0]);
g2d.setColor(Color.BLACK);
//float lineWidth = 1.5f;
//g2d.setStroke(new BasicStroke(lineWidth));
g2d.drawLine(point1[0], point1[1], point2[0], point2[1]);
}

int[] temp1 = aim;
while (isFind1) {
try {
count1++;
int[][] tempA = searchResult1.get(temp1[0] + ":" + temp1[1]);
int[] point1 = getPosition(tempA[0][1], tempA[0][0]);
int[] point2 = getPosition(tempA[1][1], tempA[1][0]);
g2d.setColor(Color.RED);
float lineWidth = 3.0f;
g2d.setStroke(new BasicStroke(lineWidth));
g2d.drawLine(point2[0], point2[1], point1[0], point1[1]);
if (tempA[1][0] == origin[0] && tempA[1][1] == origin[1]) {
break;
}
temp1 = tempA[1];
} catch (Exception e) {
break;
}

}

int[] temp2 = aim;
while (isFind2) {
try {
int[][] tempA = searchResult2.get(temp2[0] + ":" + temp2[1]);

if (tempA[1][0] == origin[0] && tempA[1][1] == origin[1]) {
break;
}
int[] point1 = getPosition(tempA[0][1], tempA[0][0]);
int[] point2 = getPosition(tempA[1][1], tempA[1][0]);
g2d.setColor(Color.RED);
float lineWidth = 3.0f;
g2d.setStroke(new BasicStroke(lineWidth));
g2d.drawLine(point2[0], point2[1], point1[0], point1[1]);
count2++;
temp2 = tempA[0];
} catch (Exception e) {
break;
}

}

g2d.setColor(Color.BLACK);
g2d.drawString("搜索次数:" + searchProcess.size(), 570, 50);
if (isFind1) {
g2d.drawString("最短长度:" + count1, 570, 80);
}
if (isFind2) {
g2d.drawString("最短长度:" + count2, 570, 80);
}

g.setColor(c);

}

public void dijstra() {

new Thread() {

public void run() {

isFinding = true;

for (int i = 0; i < length.length; i++) {
for (int j = 0; j < length[0].length; j++) {
length[i][j] = 10000;
}
}

boolean flag = true;
int[] start = { origin[0], origin[1] };
visited[origin[1]][origin[0]] = 1;

for (int[] a : dir1) {

int row = start[0] + a[0];
int col = start[1] + a[1];

if (row < 0 || row >= map.length || col < 0
|| col >= map[0].length)
continue;
if (map[row][col] == 1)
continue;

length[row][col] = 2;

String key = row + ":" + col;
int[][] al = new int[][] { { start[0], start[1] },
{ row, col } };
searchResult2.put(key, al);

searchProcess.add(new int[][] { { start[0], start[1] },
{ row, col } });

}

for (int[] b : dir2) {

int row = start[0] + b[0];
int col = start[1] + b[1];

if (row < 0 || row >= map.length || col < 0
|| col >= map[0].length)
continue;
if (map[row][col] == 1 || map[row][col - b[1]] == 1
|| map[row - b[0]][col] == 1)
continue;

length[row][col] = 3;

String key = row + ":" + col;
int[][] al = new int[][] { { start[0], start[1] },
{ row, col } };
searchResult2.put(key, al);

searchProcess.add(new int[][] { { start[0], start[1] },
{ row, col } });

}

repaint();

loop: while (flag) {

try {
int[] temp = new int[2];
boolean is = false;
int minLen = 10000;
for (int i = 0; i < visited.length; i++) {
for (int j = 0; j < visited[0].length; j++) {
if (visited[i][j] == 0) {
if (minLen > length[i][j]) {
minLen = length[i][j];
temp[0] = i;
temp[1] = j;
is = true;
}
}
}
}

if (map[temp[0]][temp[1]] == 1 || is == false) {
JOptionPane.showMessageDialog(null, "对不起,找不到路径!",
"错误", JOptionPane.ERROR_MESSAGE);
break loop;
}

visited[temp[0]][temp[1]] = 1;

repaint();

int length1 = length[temp[0]][temp[1]];

int index = 0;
for (int[] a1 : dir3) {

index++;
int row = temp[0] + a1[0];
int col = temp[1] + a1[1];

if (row < 0 || row >= map.length || col < 0
|| col >= map[0].length)
continue;
if (index <= 4 && map[row][col] == 1)
continue;
if (index > 4 && map[row][col] == 1
|| map[row][col - a1[1]] == 1
|| map[row - a1[0]][col] == 1)
continue;

int length2 = length[row][col];
int lengthSum = 0;
if (index <= 4) {
lengthSum = length1 + 2;
} else if (index > 4) {
lengthSum = length1 + 3;
}

if (length2 > lengthSum) {

String key = row + ":" + col;
searchResult2.put(key, new int[][] {
{ temp[0], temp[1] }, { row, col } });

length[row][col] = lengthSum;

if (length2 == 10000) {


searchProcess
.add(new int[][] {
{ temp[0], temp[1] },
{ row, col } });

}

}

if (row == aim[0] && col == aim[1]) {

repaint();
break loop;

}
}

Thread.sleep(span);

} catch (Exception e) {
JOptionPane.showMessageDialog(null, "对不起,找不到路径!", "错误",
JOptionPane.ERROR_MESSAGE);
break loop;
}
}

isFind2 = true;
isFinding = false;

}

}.start();

}

public void gdyx() {

new Thread() {

public void run() {

isFinding = true;
boolean flag = true;
int[][] start = { { origin[0], origin[1] },
{ origin[0], origin[1] } };
queue.offer(start);

loop: while (flag) {

try {
int[][] currentEdge = queue.poll();
int[] tempTarget = currentEdge[1];

if (visited[tempTarget[1]][tempTarget[0]] == 1) {
continue;
}

visited[tempTarget[1]][tempTarget[0]] = 1;

searchProcess.add(currentEdge);

searchResult1.put(tempTarget[0] + ":" + tempTarget[1],
new int[][] { currentEdge[1], currentEdge[0] });

repaint();
Thread.sleep(span);

if (tempTarget[0] == aim[0] && tempTarget[1] == aim[1]) {
break;
}

int currCol = tempTarget[1];
int currRow = tempTarget[0];

for (int[] a : dir1) {

int i = a[0];
int j = a[1];

if (i == 0 && j == 0) {
continue;
}

if (currRow + i >= 0 && currRow + i < map.length
&& currCol + j >= 0
&& currCol + j < map[0].length
&& map[currRow + i][currCol + j] != 1) {

int[][] tempEdge = {
{ tempTarget[0], tempTarget[1] },
{ currRow + i, currCol + j } };

queue.offer(tempEdge);

}
}

for (int[] b : dir2) {

int i = b[0];
int j = b[1];

if (i == 0 && j == 0) {
continue;
}
if (currRow + i >= 0 && currRow + i < map.length
&& currCol + j >= 0
&& currCol + j < map[0].length
&& map[currRow + i][currCol + j] != 1
&& map[currRow][currCol + j] != 1
&& map[currRow + i][currCol] != 1) {

int[][] tempEdge = {
{ tempTarget[0], tempTarget[1] },
{ currRow + i, currCol + j } };

queue.offer(tempEdge);

}
}

} catch (Exception e) {

JOptionPane.showMessageDialog(null, "对不起,找不到路径!", "错误",
JOptionPane.ERROR_MESSAGE);
break loop;

}

}

isFind1 = true;
isFinding = false;
repaint();

}

}.start();

}

public int[] getPosition(int row, int col) {

int xOffset = (row + 1) * 15 + 20 - 7;
int yOffset = (col + 1) * 15 + 50 - 7;

return new int[] { xOffset, yOffset };

}

public int getRand() {

Random rand = new Random();
int i = rand.nextInt(32);
return i;
}

public void changeOA(int[] oa) {

int x = getRand();
int y = getRand();
if (map[x][y] != 1 && map[x][y] != 3 && map[x][y] != 4) {
oa[0] = x;
oa[1] = y;
}
loop: while (map[x][y] == 1 || map[x][y] == 3 || map[x][y] == 4) {
x = getRand();
y = getRand();
if (map[x][y] != 1 && map[x][y] != 3 && map[x][y] != 4) {
oa[0] = x;
oa[1] = y;
break loop;
}
}

}

public void createMap() {

for (int i = 0; i < 380; i++) {
int y = getRand();
int x = getRand();
while (map[y][x] == 1) {
y = getRand();
x = getRand();
}
map[y][x] = 1;
}

}

public void suodingo() {

for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
if (map[i][j] == 3) {
map[i][j] = 0;
}
}
}
int x = Integer.parseInt("0" + ta1.getText());
int y = Integer.parseInt("0" + ta2.getText());
if (x >= 0 && x <= 31 && map[x][y] == 0) {
origin[0] = x;
origin[1] = y;
} else {
JOptionPane.showMessageDialog(null, "对不起,输入有误!", "错误",
JOptionPane.ERROR_MESSAGE);
}

}

public void suodinga() {

for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
if (map[i][j] == 4) {
map[i][j] = 0;
}
}
}
int x = Integer.parseInt("0" + ta3.getText());
int y = Integer.parseInt("0" + ta4.getText());
if (x >= 0 && x <= 31 && map[x][y] == 0) {
aim[0] = x;
aim[1] = y;
} else {
JOptionPane.showMessageDialog(null, "对不起,输入有误!", "错误",
JOptionPane.ERROR_MESSAGE);
}

}

public void setText() {

ta1.setText(origin[0] + "");
ta2.setText(origin[1] + "");
ta3.setText(aim[0] + "");
ta4.setText(aim[1] + "");

}

public void clear() {

init();
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
if (map[i][j] == 1) {
map[i][j] = 0;
}
}
}

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == FindForGdyx && isFinding == false) {

init();
setText();
gdyx();

} else if (e.getSource() == FindForDijstra && isFinding == false) {
init();
setText();
dijstra();
} else if (e.getSource() == Rand && isFinding == false) {

for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
map[i][j] = 0;
}
}
init();
createMap();

repaint();

} else if (e.getSource() == Randoa && isFinding == false) {

for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
if (map[i][j] == 3 || map[i][j] == 4) {
map[i][j] = 0;
}
}
}
init();
changeOA(origin);
changeOA(aim);
setText();

repaint();

} else if (e.getSource() == suodingo && isFinding == false) {

init();
suodingo();
repaint();

} else if (e.getSource() == suodinga && isFinding == false) {

init();
suodinga();
repaint();

} else if (e.getSource() == clear && isFinding == false) {

clear();
repaint();

}

}

public void keyPressed(KeyEvent e) {

int keyChar = e.getKeyChar();
if (keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9) {

} else {
e.consume();
}

}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

}

}

源码下载地址:http://pan.baidu.com/s/1jIcvoGI

模拟最短路径寻路(JAVA界面)