怎么樣使用java算法BFS來求迷宮出口短路徑-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)怎么樣使用java算法BFS來求迷宮出口短路徑的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、網(wǎng)站制作、薩嘎網(wǎng)絡(luò)推廣、小程序開發(fā)、薩嘎網(wǎng)絡(luò)營銷、薩嘎企業(yè)策劃、薩嘎品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供薩嘎建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:muchs.cn

隊(duì)列的建立

static Queue r = new LinkedList(); //創(chuàng)建隊(duì)列

隊(duì)列的基本方法

r.offer(); 入隊(duì)尾

r.poll(); 出隊(duì)首

r.peek(); 隊(duì)首的內(nèi)容

代碼實(shí)現(xiàn):

全局變量設(shè)置

package Two;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class BFS {
  static int a[][] = new int [100][100]; //輸入迷宮
  static int v[][] = new int [100][100]; //走過的標(biāo)記為1
  static int startx,starty;    //輸入起點(diǎn)位置
  static int p,q;      //輸入要到達(dá)的坐標(biāo)位置
  static int dx[] = {0,1,0,-1};  //方向數(shù)組
  static int dy[] = {1,0,-1,0}; 
  
  static Queue<point> r = new LinkedList<point>();  //創(chuàng)建隊(duì)列
  
  
  static class point{     //建立類坐標(biāo)屬性
	  int x;
	  int y;
	  int step;
	
  }

輸入迷宮和起始位置,目標(biāo)位置

public static void main(String[] args) {
  Scanner in = new Scanner(System.in);
  int m = in.nextInt();
  int n = in .nextInt();
  for(int i=1;i<=m;i++)            //輸入迷宮
  	for(int j=1;j<=n;j++)
  		a[i][j] = in.nextInt();
  
  startx = in.nextInt();
  starty = in.nextInt();    //輸入目標(biāo)和起始位置
  p = in.nextInt();
  q = in.nextInt();

BFS算法開始

1、設(shè)置隊(duì)首

  //BFS
  point start = new point();   //定義一個(gè)初始類作為隊(duì)首
  start.x = startx;
  start.y = starty;
  start.step = 0;
  r.offer(start);
  v[startx][starty]=1;

2、進(jìn)入循環(huán)體

  while(!r.isEmpty()) {        //當(dāng)隊(duì)列為空時(shí)跳出循環(huán)
  	
  	int x = r.peek().x;      //把隊(duì)首的屬性賦值
  	int y = r.peek().y;
  	int step = r.peek().step;
  	
  	
  	if(x==p && y==q) {           //到達(dá)目的地,退出循環(huán)
  		System.out.println(step);
  		break;
  	}
  	
  	for(int i=0;i<4;i++) {       //廣度遍歷,右下左上分別入隊(duì)
  		int tx= x+dx[i];
  		int ty= y+dy[i];
  	

  		if(a[tx][ty] == 1 && v[tx][ty]==0) {   //判斷是否可以入隊(duì)
  			//入隊(duì)
  			point temp = new point();    //建立一個(gè)臨時(shí)類
  			temp.x = tx;
  			temp.y = ty;
  			temp.step = r.peek().step +1;
  			
  	
  			r.offer(temp);     //入隊(duì)
  			v[tx][ty]=1;       //標(biāo)記為1
  		}
  	}
  	
  	r.poll(); //拓展完了需要隊(duì)首出隊(duì)
  
  	
  }
  

}
}

感謝各位的閱讀!關(guān)于怎么樣使用java算法BFS來求迷宮出口短路徑就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

本文名稱:怎么樣使用java算法BFS來求迷宮出口短路徑-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://muchs.cn/article44/deioee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、用戶體驗(yàn)、靜態(tài)網(wǎng)站、電子商務(wù)、做網(wǎng)站、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司