Niu Ke-Out of the Labyrinth

Topic link

Xiao Ming is now playing a game, and the game has reached the teaching level. The maze is an N*M matrix.
Xiao Ming’s starting point is represented by "S" on the map, the end point is represented by "E", obstacles are represented by "#", and open spaces are represented by ".".
Obstacles cannot pass. If Xiao Ming is now at the point (x, y), then the next step can only go to one of the four adjacent grids: (x+1, y), (x-1, y), (x, y +1), (x, y-1);
Xiao Ming wants to know whether he can walk from the starting point to the ending point now.
Input description:
This question contains multiple sets of data.
Each group of data first enter two numbers N, M
followed by N lines, each line of M characters, indicating the state of the map.
Data range:
2<=N,M<=500.
Ensure that there is a starting point S and an end point E.
Output description:
Each group of data outputs one line. If Xiao Ming can walk from the start point to the end point, then output Yes, otherwise output No

bfs bar

import java.util.ArrayList;
import java.util.Scanner;
 import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Main {
    
    
    static int endx;
    static int endy;
    static int a[][] = {
    
     {
    
     1, 0 }, {
    
     -1, 0 }, {
    
     0, 1 }, {
    
     0, -1 } };
 
    static class point {
    
    
        int x, y;
 
        public point(int x, int y) {
    
    
            this.x = x;
            this.y = y;
        }
    }
 
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
    
    
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            scanner.nextLine();
            int sx = 0;
            int sy = 0;
            char c[][] = new char[n][m];
            for (int i = 0; i < n; i++) {
    
    
                String s1 = scanner.nextLine();
                for (int j = 0; j < m; j++) {
    
    
                    c[i][j] = s1.charAt(j);
                    if (c[i][j] == 'S') {
    
    
                        sx = i;
                        sy = j;
                    }
                    if (c[i][j] == 'E') {
    
    
                        endx = i;
                        endy = j;
                    }
                }
            }
            int t = bfs(c, sx, sy);
            if (t == 1)
                System.out.println("Yes");
            else {
    
    
                System.out.println("No");
            }
        }
    }
 
    public static boolean check(char[][] matrix, point a) {
    
    
        int n = matrix.length - 1, m = matrix[0].length - 1;
        if (a.x < 0 || a.x > n || a.y < 0 || a.y > m || matrix[a.x][a.y] == '#')
            return false;
        return true;
    }
 
    private static int bfs(char[][] c, int sx, int sy) {
    
    
        ArrayList<point> list = new ArrayList<>();
        list.add(new point(sx, sy));
        while (list.size() != 0) {
    
    
            point b = list.get(0);
            list.remove(0);// 删除该点
            if (b.x == endx && b.y == endy) {
    
    
                return 1;
            }
            for (int i = 0; i < 4; i++) {
    
    
                int x = b.x + a[i][0];
                int y = b.y + a[i][1];
                point p = new point(x, y);
                if (check(c, p)) {
    
    
                    list.add(p);
                    c[x][y] = '#';
                }
            }
        }
        return 0;
    }
}

Guess you like

Origin blog.csdn.net/jahup/article/details/105970928