Algorithm exercise - maze problem (Java) bfs wide search

Description of the problem:
Xiao Ming is in a maze, please help Xiao Ming find the shortest distance from the starting point to the end point.
Xiao Ming can only move in four directions: up, down, left, and right.
Input
The input contains multiple sets of test data. The first line of input is an integer T, indicating that there are T sets of test data.
The first line of each input is two integers N and M (1<=N, M<=100).
In the next N lines, enter M characters in each line, and each character represents a small square in the maze.
The characters have the following meanings:

'S': starting point
'E': end point
'-': open space, passable
'#': obstacle, impossible to pass

The input data is guaranteed to have one and only one starting point and one ending point.
Output
For each set of inputs, output the shortest path from the start point to the end point, and output -1 if there is no path from the start point to the end point.
sample input
insert image description here
sample output

9

Original topic https://www.dotcpp.com/oj/problem1672.html

Problem solution:
problem-solving idea: breadth-first search (BFS)

public class Main{
    
    
	
    public static class Q{
    
    
        int x;
        int y;
        int dept;//当前步数
    }
	
	static int[][] move = {
    
    {
    
    0,1},{
    
    0,-1},{
    
    1,0},{
    
    -1,0}};

	static public void bfs(int[][] vis,int sx,int sy,LinkedList<Q> que,int gx,int gy,int n,int m,char[][] arr) {
    
    

		 Q q = new Q();
		 q.x = sx;
		 q.y = sy;
		 q.dept = 0;
		 que.add(q);//添加
		 int finish=0,result=0;
		 
		 while(que.size() != 0) {
    
    
			 Q first = que.poll(); //取出对头,并删除
			 if(first.x == gx && first.y == gy) {
    
    
				 finish =1;
				 result = first.dept;
				 break;
			 }
			 for(int i=0 ; i< 4 ; i++) {
    
    
				 int tx = first.x + move[i][0];
				 int ty = first.y + move[i][1];
				 //边界判定和判定是否走过
				 if(tx >=0 && tx <n && ty>=0 && ty<m && arr[tx][ty]!='#' && vis[tx][ty]==0) {
    
    
					 vis[tx][ty] = 1;
					 Q temp = new Q();
					 temp.x = tx;
					 temp.y = ty;
					 temp.dept=first.dept+1;
					 que.add(temp);
				 }
			 }
		 }//while
		 
		 //输出结果
         if (result==0) {
    
    
             System.out.println("-1");
         }else {
    
    
             System.out.println(result);
         }
	}
	public static void main(String[] args) {
    
    
		
		Scanner sc =new Scanner(System.in);
		int t = sc.nextInt();
		
		while( t-- > 0 ) {
    
    
			int n = sc.nextInt();
			int m = sc.nextInt();
			
			char[][] arr = new char[n][m];
			int sx=0,sy=0;
			int gx=0,gy=0;
			int[][] vis = new int[n][m];
			LinkedList<Q> que = new LinkedList<Q>();
			//
			for(int i=0 ; i < n ;i++) {
    
    
				String str=sc.next();
				for(int j=0 ; j< m  ;j++) {
    
    
					arr[i][j] = str.charAt(j);
					if(arr[i][j] == 'S') {
    
    
						sx=i;
						sy=j;
					}
					if(arr[i][j] == 'E') {
    
    
						gx=i;
						gy=j;
					}
				}
			}
			vis[sx][sy]=1;//标记开始坐标
			//广搜
			bfs(vis,sx,sy,que,gx,gy,n,m,arr);
		}//while
	}
}

Guess you like

Origin blog.csdn.net/weixin_44732379/article/details/115329168