广度优先算法BFS
                            
                            
                            package myalgorithm;import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
/*BFS用于記錄的位置和值的結構*/
class node
{node(int xparam,int yparam,int valparam){this.x = xparam;this.y = yparam;this.value = valparam;}int x,y,value;
}
public class ShortPath {/*全局最短路徑*/public int stepnum = 999;/*構建11*11的迷宮,英雄H在(1,1)的位置出發,去解救美女M(6,8)*/char[][] graph = {{'#','#','#','#','#','#','#','#','#','#','#'},{'#','H','_','_','*','_','_','*','_','_','#'},{'#','_','_','_','_','_','_','_','_','_','#'},{'#','_','*','_','_','_','*','_','_','_','#'},{'#','_','_','_','*','_','_','_','_','*','#'},{'#','_','_','_','_','_','_','*','_','*','#'},{'#','_','*','_','_','_','_','_','M','_','#'},{'#','*','_','_','*','_','_','_','_','_','#'},{'#','_','_','_','_','_','_','_','_','_','#'},{'#','_','_','_','*','_','_','_','_','_','#'},{'#','#','#','#','#','#','#','#','#','#','#'},};/*初始標記數組都為0*/public int[][] mark = new int[graph.length][graph.length];/*每一個位置有四種選擇:右下左上*/public int[][] choose = {{0,1},{1,0},{0,-1},{-1,0}};/*BFS算法*/public void BFS(node startPoint){//起始點裝入隊列Queue<node> queue = new LinkedList<node>();startPoint.value = 1;//確保起始步數為1
        queue.offer(startPoint);node t1;int tx,ty;top:while(!queue.isEmpty()){//取隊首,出隊后不再入隊,value也自此固定t1 = queue.poll();mark[t1.x][t1.y] = t1.value;//標記步數for(int i=0;i<4;i++){tx = t1.x + choose[i][0];ty = t1.y + choose[i][1];//找到美女,肯定是最短的,可以立即返回if(graph[tx][ty] == 'M'){stepnum = t1.value + 1;//下一步可到mark[tx][ty] = stepnum;break top;}//繼續接著找,把空路徑添加到隊列末尾//不是炸彈和圍墻,并且沒有被標記if(graph[tx][ty] != '#' && graph[tx][ty] != '*'&&mark[tx][ty] == 0){queue.offer(new node(tx,ty,t1.value+1));}}}}/*BFS回溯路徑*/private void getPath(node target) {int beforex = 0,beforey = 0;int x = target.x;int y = target.y;for(int i=0;i<4;i++){beforex = x + choose[i][0];beforey = y + choose[i][1];//找到英雄的出發點,函數結束if (beforex == 1 && beforey == 1){return;}if(mark[x][y] - 1 == mark[beforex][beforey]){System.out.print("("+beforex+","+beforey+")<--");break;}}getPath(new node(beforex,beforey,0));return;}
/*main函數*/public static void main(String[] args) {ShortPath my = new ShortPath();long start = System.currentTimeMillis();my.BFS(new node(1,1,0));long end = System.currentTimeMillis();System.out.println("BFS step: " + my.stepnum + " time:" + (end-start));//打印標記結果for (int m = 0;m<my.graph.length;m++)System.out.println(Arrays.toString(my.mark[m]));//打印巡回路徑my.getPath(new node(6,8,0)); }}
                        
                        
                        /*main函數*/public static void main(String[] args) {ShortPath my = new ShortPath();long start = System.currentTimeMillis();my.BFS(new node(1,1,0));long end = System.currentTimeMillis();System.out.println("BFS step: " + my.stepnum + " time:" + (end-start));//打印標記結果for (int m = 0;m<my.graph.length;m++)System.out.println(Arrays.toString(my.mark[m]));//打印巡回路徑my.getPath(new node(6,8,0)); }}
BFS step: 13 time:4
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 0, 7, 8, 0, 10, 11, 0]
[0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0]
[0, 3, 0, 5, 6, 7, 0, 9, 10, 11, 0]
[0, 4, 5, 6, 0, 8, 9, 10, 11, 0, 0]
[0, 5, 6, 7, 8, 9, 10, 0, 12, 0, 0]
[0, 6, 0, 8, 9, 10, 11, 0, 13, 0, 0]
[0, 0, 10, 9, 0, 11, 0, 0, 0, 0, 0]
[0, 0, 11, 10, 11, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
(5,8)<--(4,8)<--(4,7)<--(4,6)<--(4,5)<--(3,5)<--(3,4)<--(3,3)<--(2,3)<--(2,2)<--(2,1)
?
擴展:
PhotoShop中的魔術棒選擇工具的原理,就是從鼠標選中的點作為種子,并加入隊列;依次查找周邊的點,如果顏色與種子顏色相近,則加入隊列;對隊列中的元素依次進行類似擴展,這樣就選取了與最初種子顏色相同的點的集合。
轉載于:https://www.cnblogs.com/mingziday/p/4827796.html
總結
                            
                        - 上一篇: 程序一启动检查网络,如果没有网络就退出程
 - 下一篇: Android下强制打开软键盘