Robot maze problem

topic

1. The room is composed of XY squares. For example, the picture below shows the size of 64. Each square is described by coordinates (x,y).
2. The robot starts from square (0, 0) and can only move east or north. The exit is fixed at the northeastern corner of the room, as shown in the figure below
Square(5,3). The use case ensures that the robot can walk from the entrance to the exit.
3. Some squares in the room are walls, such as (4,1), and the robot cannot pass there.
4. There are some places where you cannot walk to the exit once you arrive, such as the square marked B, which is called a trap square.
5. There are some places that the robot cannot reach, such as the square marked A, which is called an unreachable square. The unreachable square does not include walls
Location
6. In the example picture below, there are 2 trap squares and 3 unreachable squares.
7. Please implement the path planning function for the robot: given the size of the room and the location of the walls, please calculate how many trap squares and unreachable squares there are

Insert image description here

Input
1. The first-line x and y of the room (0 < x,y <= 1000 )
2. The first The second line is the number N of walls in the room (O <= N < x*Y)
3. Next, there will be N rows of coordinates of the walls

If there are multiple data in the same line separated by a space, the use case ensures that all input data are legal (no carriage return at the end)

output

1. The number of trap squares and unreachable squares, the two information are output in one line, separated by a space. (No carriage return and line break at the end)

Insert image description here

Java code

package day11;

import javax.print.attribute.standard.Chromaticity;
import java.util.HashSet;
import java.util.Objects;
import java.util.Scanner;
import java.util.Set;

public class MazeSolving {
    
    
    static int xLength;
    static int yLength;
    static class CheckModel{
    
    
        int x;
        int y;
        public CheckModel(int x,int y){
    
    
            this.x = x;
            this.y = y;
        }
        @Override
        public int hashCode(){
    
    
            return Objects.hash(x, y);
        }
        @Override
        public boolean equals(Object o){
    
    
            if(o==this){
    
    
                return true;
            }
            if(o==null||getClass()!=o.getClass()){
    
    
                return false;
            }
            CheckModel check = (CheckModel) o;
            return x == check.x && y==check.y;
        }
    }

    //wallSet代表墙壁坐标,checkSet用于存储在搜索路径过程中检查过的坐标,finishSet用于存储已经找到终点的坐标
    private static void findItOut(int x, int y, Set<CheckModel> wallSet, Set<CheckModel> checkSet, Set<CheckModel> finishSet) {
    
    
        if(yLength-1==y&&xLength-1==x){
    
    
            finishSet.add(new CheckModel(x,y));//检查当前坐标 (x, y) 是否是迷宫的终点
        }
        if(yLength<=y||x>=xLength){
    
    
            return;  //越界了
        }
        checkSet.add(new CheckModel(x,y));//否则添加到已检查坐标
        //北方向
        if(!wallSet.contains(new CheckModel(x,y+1))){
    
    
            findItOut(x,y+1,wallSet,checkSet,finishSet);
        }else{
    
    
            finishSet.add(new CheckModel(x,y));
        }
        //东方向
        if(!wallSet.contains(new CheckModel(x+1,y))){
    
    
            findItOut(x+1,y,wallSet,checkSet,finishSet);
        }else{
    
    
            finishSet.add(new CheckModel(x,y));
        }
    }
    public static void main(String[] args){
    
    
        try {
    
    
            Scanner sc = new Scanner(System.in);
            xLength = sc.nextInt();
            yLength = sc.nextInt();
            int size = sc.nextInt();
            int[][] values = new int[size][2];
            for(int i = 0; i < size; i++){
    
    
                values[i][0] = sc.nextInt();
                values[i][1] = sc.nextInt();
            }
            int trapCount = 0;
            int invalidCount = 0;
            Set<CheckModel> wallHashSet = new HashSet<>();
            for(int[] wall:values){
    
    
                wallHashSet.add(new CheckModel(wall[0],wall[1]));
            }
            Set<CheckModel> checksHashSet = new HashSet<>();
            Set<CheckModel> finshHashSet = new HashSet<>();
            findItOut(0,0,wallHashSet,checksHashSet,finshHashSet);
            invalidCount = xLength*yLength-checksHashSet.size()-wallHashSet.size();
            //整个迷宫中的格子数减去检查过的格子数和包含障碍物的格子数等于无法到达数量

            /*
            * 这里使用 finishHashSet 中的每个坐标作为起点,再次调用 findItOut 进行深度优先搜索。
            * 如果搜索得到的路径中不包含终点 (xLength - 1, yLength - 1),则说明这条路径是无效的,
            * trapCount 就会增加。这样,trapCount 表示的是无效的路径的数量。
            *
            * */
            for(CheckModel model:finshHashSet){
    
    
                Set<CheckModel> checksT = new HashSet<>();
                Set<CheckModel> finishT = new HashSet<>();
                findItOut(model.x, model.y, wallHashSet,checksT,finishT);
                if(!finishT.contains(new CheckModel(xLength-1,yLength-1))){
    
    
                    trapCount++;
                }
            }
            System.out.println(trapCount+" "+invalidCount);
        }catch (Exception e){
    
    
            e.printStackTrace();
            System.out.println("input error");
        }
    }

}

Guess you like

Origin blog.csdn.net/qq_45257495/article/details/134476355