BFS算法(Java)

题目描述

迷宫由 n 行 m 列的单元格组成,每个单元格要么是空地,要么是障碍物。其中1表示空地,可以走通,2表示障碍物。给定起点坐标startx,starty以及终点坐标endx,endy。现请你找到一条从起点到终点的最短路径长度。
输入

第一行包含两个整数n,m(1<=n,m<=1000)。接下来 n 行,每行包含m个整数(值1或2),用于表示这个二维迷宫。接下来一行包含四个整数startx,starty,endx,endy,分别表示起点坐标和终点坐标。
输出

如果可以从给定的起点到终点,输出最短路径长度,否则输出-1。
测试数据

输入

5 4
1 1 2 1
1 1 1 1
1 1 2 1
1 2 1 1
1 1 1 2
输出

7

 

import java.util.Queue;
import java.util.Scanner;
import java.util.LinkedList;
class point {
	int x,y,step;
	public point(int x,int y,int step){
		this.x=x;
		this.y=y;
		this.step=step;
	}
}
public class Bfs {
	static int[][] a=new int[50][30];//地图
	static int[][] b=new int[50][30];
	static int[][] t= {{ 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 }};
	static int startx,starty,endsx,endsy;
	public static void bfs(int x,int y,int step) {
		point p=new point(x,y,step);
		LinkedList<point> q=new LinkedList<point>();
		q.add(p);
		b[x][y]=1;
		boolean flag=false;
		while(!q.isEmpty()) {
			point f=q.peek();
			if(f.x==endsx&&f.y==endsy) {
				flag=true;
				System.out.println(f.step);
				break;
			}
			for(int i=0;i<4;i++) {
				int newx=f.x+t[i][0];
				int newy=f.y+t[i][1];
				int newstep=f.step+1;
				if(a[newx][newy]==0&&b[newx][newy]!=1&&newx>=1&&newy>=1) {
					point newp=new point(newx,newy,newstep);
					q.add(newp);
					b[newx][newy]=1;
				}
			}
			q.remove();
		}
		if(flag==false) {
			System.out.print(-1);
		}
	}
	
//	1 1 
//	4 3
//	0 0 1 0
//	0 0 0 0
//	0 0 1 0
//	0 1 0 0
//	0 0 0 1
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		startx=scan.nextInt();
		starty=scan.nextInt();
		endsx=scan.nextInt();
		endsy=scan.nextInt();
		for(int i=1;i<6;i++) {
			for(int j=1;j<5;j++) {
				a[i][j]=scan.nextInt();
			}
		}
		scan.close();
		bfs(startx,starty,0);
	}
}

体悟:数组一直越界,搞了很长时间才弄明白(中午也没睡觉,现在十分的困倦),原来是犯了在写DFS的错误,数组的默认值为0,开始写的条件之中写的是不为1,即没有访问过就可以往上下左右四个方向移动,但是地图边界也为0,当访问点在边界上的上下左右的时候就数组越界了,所以解决这个问题也可以用下列种形式

import java.util.Queue;
import java.util.Scanner;
import java.util.LinkedList;
class point {
	int x,y,step;
	public point(int x,int y,int step){
		this.x=x;
		this.y=y;
		this.step=step;
	}
}
public class Bfs {
	static int[][] a=new int[50][30];//地图
	static int[][] b=new int[50][30];
	static int[][] t= {{ 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 }};
	static int startx,starty,endsx,endsy;
	public static void bfs(int x,int y,int step) {
		point p=new point(x,y,step);
		LinkedList<point> q=new LinkedList<point>();
		q.add(p);
		b[x][y]=2;
		boolean flag=false;
		while(!q.isEmpty()) {
			point f=q.peek();
			if(f.x==endsx&&f.y==endsy) {
				flag=true;
				System.out.println(f.step);
				break;
			}
			for(int i=0;i<4;i++) {
				int newx=f.x+t[i][0];
				int newy=f.y+t[i][1];
				int newstep=f.step+1;
				if(a[newx][newy]==0&&b[newx][newy]==1) {
					point newp=new point(newx,newy,newstep);
					q.add(newp);
					b[newx][newy]=2;
				}
			}
			q.remove();
		}
		if(flag==false) {
			System.out.print(-1);
		}
	}
	
//1 1 
//4 3
//0 0 1 0
//0 0 0 0
//0 0 1 0
//0 1 0 0
//0 0 0 1
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		startx=scan.nextInt();
		starty=scan.nextInt();
		endsx=scan.nextInt();
		endsy=scan.nextInt();
		for(int i=1;i<6;i++) {
			for(int j=1;j<5;j++) {
				a[i][j]=scan.nextInt();
				b[i][j]=a[i][j]+1;
			}
		}
		scan.close();
		bfs(startx,starty,0);
	}
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值