import java.util.LinkedList;
/**
* 广度搜索,查找最小距离
*/
public class BFS {
static int map[][] = new int[4][4];
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
map[i][j] = 0;
}
}
new BFS().bfs(3, 3);
}
public void bfs(int end_r, int end_c) {
int next[][] = { { 0, 1 }, { 1, 0 }, { -1, 0 }, { 0, -1 } };// 4个方向
LinkedList<Node> q = new LinkedList<Node>();// 队列存储
Node start = new Node(0, 0, 0);
q.offer(start);
while (!q.isEmpty()) {
Node temp = q.poll();
if (temp.row == end_r && temp.cloumm == end_r) {
System.out.println(temp.round);
break;
}
for (int i = 0; i < 4; i++) {
int r = temp.row + next[i][0];
int c = temp.cloumm + next[i][1];
if (r > 3 || c > 3 || r < 0 || c < 0 || map[r][c] == 1) {
continue;
}
map[r][c] = 1;
q.offer(new Node(r, c, temp.round + 1));
}
}
}
class Node {
int row;
int cloumm;
int round;
public Node(int row, int cloumm, int round) {
super();
this.row = row;
this.cloumm = cloumm;
this.round = round;
}
}
}
java实现广度搜索(bfs)最短路径
原文作者:BFS
原文地址: https://blog.csdn.net/u013967628/article/details/79844138
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/u013967628/article/details/79844138
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。