Blue Bridge Cup Sprint Maze and Trap [Ninth Session] [Final] [Group C] BFS

Xiao Ming is playing a maze game. In the game, he needs to control his character to leave a 2D maze composed of NxN grids.
  Xiao Ming's starting position is in the upper left corner, and he needs to reach the grid in the lower right corner to leave the maze.
  At each step, he can move to the adjacent grids up, down, left, and right (provided that the target grid can pass through).
  Some cells in the maze that Xiao Ming can pass through, we use '.' to indicate;
  some cells are walls, Xiao Ming cannot pass through, we use '#' to represent.
  Also, some grids have traps, which we denote with 'X'. Unless Xiao Ming is in an invincible state, he cannot pass through.

  Some grids have invincible props, we use '%' to indicate.
  When Xiao Ming reaches the grid for the first time, he will automatically get invincible, and the invincible state will last for K steps.
  After that, if you reach the grid again, you will not gain invincibility.

  When in the invincible state, you can pass through the grid with traps, but the traps will not be dismantled/destroyed, that is, the traps will still prevent non-invincible characters from passing through.

  Given a maze, please calculate how many steps Xiaoming can leave the maze at least

input format

  ----
  The first line contains two integers N and K. (1 <= N <= 1000 1 <= K <= 10)
  The following N rows contain an NxN matrix.
  The matrix guarantees that the upper left and lower right corners are '.'.

output format

  ----
  An integer represents the answer. If Xiao Ming cannot leave the maze, output -1.

sample input

5 3
...XX
##%#.
...#.
.###.
.....

sample output

10

Topic link: https://blog.csdn.net/qq_46311811/article/details/123769924

At the beginning, my idea was to use bfs to store traps in a list. When encountering invincibility, traverse the list of traps. If you can maintain the invincible state and walk to the position, store the position of the trap in the stack, but in this way the stack will There are many more steps, so when popping the stack, it is necessary to judge whether the stacking position meets the current number of steps. If it does not match, then push it into the stack and skip this cycle.

But this only gets 30 points, small data can pass, big data will time out.

Later, refer to the code of the big guy

import java.util.*;

public class 迷宫与陷阱 {
    static int n, k, vis[][][];
    static char[][] c;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        k = sc.nextInt();
        vis = new int[n][n][11];
        c = new char[n][n];
        sc.nextLine();
        for (int i = 0; i < n; i++) {
            c[i] = sc.nextLine().toCharArray();
        }
        System.out.println(bfs());
    }

    public static int bfs() {
        Queue<Node> q = new LinkedList<>();
        int[][] dir = {
  
  {-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        int ans=0;
        q.offer(new Node(0,0,0));
        vis[0][0][0]=1;
        while (!q.isEmpty()){
            int len=q.size();
            while (len-->0){
                Node head=q.poll();
                if (head.x==n-1&&head.y==n-1){
                    return ans;
                }
                for (int i = 0; i < 4; i++) {
                    int x=head.x+dir[i][0];
                    int y=head.y+dir[i][1];
                    if (x>=0&&x<n&&y>=0&&y<n&&c[x][y]!='#'&&vis[x][y][head.w]!=1){
                        if (c[x][y]=='.'){
                            q.offer(new Node(x,y,head.w==0?0:head.w-1));
                            vis[x][y][head.w]=1;
                        }
                        else if (c[x][y]=='%'){
                            q.offer(new Node(x,y,k));
                            vis[x][y][k]=1;
                            c[x][y]='.';
                        }
                        else if (c[x][y]=='X'){
                            if (head.w>0){
                                q.offer(new Node(x,y,head.w-1));
                                vis[x][y][head.w-1]=1;
                            }
                        }
                    }
                }
            }
            ans++;
        }
        return -1; 
    } 

    static class Node {int 
        x, y, w; 

        public Node(int x, int y,int w) { 
this.x 
= 
            x; 
            this.y = y; 
    this.w 
            = w 
        ; 】Tieba team cheating? In response to the trend of online exams and double filling, how should we prepare for the exam? _Stubborn Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/TerryProgrammer/article/details/123953491
Recommended