Home
Posts
Categories
Series
Tags
About
High Score Kit: DFS&BFS#2
Game Map
postedOn: 2024-4-11   updatedOn: 2024-4-11   includedIn: HighScoreKit
wordsCount: 570   readingTime: 3 mins   viewers:

Problem

In the ROR game, you play on a team that competes to destroy the opposing team’s base first, making it advantageous to reach the enemy base as quickly as possible. Suppose you’re a team member and are located at position (row: 1, column: 1) on a 5x5 map, while the enemy base is at (row: 5, column: 5). The game map is comprised of cells that can either be passable or blocked by walls. Your character can move up, down, left, or right, and cannot move outside the boundaries of the map.

Given the state of the game map maps, represented as a 2D array where 1s are passable cells and 0s are walls, implement a function solution that returns the minimum number of cells your character must pass through to reach the enemy base located at the bottom-right corner of the map. If it’s impossible to reach the enemy base due to walls blocking the way, return -1.

Constraints

  • maps is an n x m 2D array, where both n and m are natural numbers between 1 and 100.
  • n and m could be the same or different, but the case where both n and m are 1 will not be provided as input.
  • The character starts at the top-left corner of the map, and the enemy base is at the bottom-right corner.
  • The map only contains 0s and 1s, where 0 indicates a wall and 1 indicates a passable cell.

Example

maps answer
[[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,1],[0,0,0,0,1]] 11
[[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,0],[0,0,0,0,1]] -1

Explanation of Examples

  • Example 1:
    • The character can reach the enemy base by traversing 11 cells, following a path that circumvents walls effectively.
  • Example 2:
    • Walls block all possible paths to the enemy base, making it impossible to reach, thus the function returns -1.

How to Approach?

Step1: Initialize variables in loop

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.ArrayDeque;
import java.util.Deque;

public class Solution {
    class Point {
        int x, y, count;
        
        Point(int x, int y, int count) {
            this.x = x;
            this.y = y;
            this.count = count;
        }
    }
    
    public int solution(int[][] maps) {
        int n = maps.length;
        int m = maps[0].length;

        // Directions for moving right, left, down, up
        int[] dx = {1, -1, 0, 0};
        int[] dy = {0, 0, 1, -1};

        Deque<Point> deque = new ArrayDeque<>();
        boolean[][] visited = new boolean[n][m];

        // Start BFS from the top-left corner
        deque.offer(new Point(0, 0, 1));
        visited[0][0] = true;

        while (!deque.isEmpty()) {
            Point current = deque.poll();
            
            // Check if reached the bottom-right corner
            if (current.x == n - 1 && current.y == m - 1) {
                return current.count;
            }

            // Explore four possible movements
            for (int i = 0; i < 4; i++) {
                int nx = current.x + dx[i];
                int ny = current.y + dy[i];

                // Ensure the move is within boundaries, on a walkable path, and not visited
                if (nx >= 0 && nx < n && ny >= 0 && ny < m && maps[nx][ny] == 1 && !visited[nx][ny]) {
                    visited[nx][ny] = true;
                    deque.offer(new Point(nx, ny, current.count + 1));
                }
            }
        }

        // If there is no path to the destination, return -1
        return -1;
    }
}
Aaron Oh
고려대코딩개발협동조합 창단 멤버
Table of Contents
Related Posts
High Score Kit: Greedy#1
Problem Given a number, your task is to remove k digits from the number in such a way that the remaining digits form the largest possible number. The number is provided as a string number and the integer k represents the number of digits to remove. Your function solution should return the resulting largest number as a string.
2024-4-11
High Score Kit: DFS&BFS#1
Problem You are given an array of non-negative integers and a target number. Your task is to find how many ways you can add and subtract the given numbers to achieve the target number. The order of numbers in the array must be preserved. For example, with the array [1, 1, 1, 1, 1], there are five ways to reach the target number 3:
2024-4-11
High Score Kit: Brute Force#2
Problem In a power grid network composed of n transmission towers connected in a tree structure via wires, you are tasked with dividing the network into two parts by cutting one of the wires. The goal is to make the number of transmission towers in each part as equal as possible.
2024-4-11
High Score Kit: Brute Force#1
Problem In the XX game, there is a fatigue system represented by non-negative integers, allowing players to explore dungeons by using a certain amount of fatigue. Each dungeon requires a “minimum required fatigue” to start exploring and consumes a certain amount of “fatigue” upon completion. For example, to explore a dungeon with a “minimum required fatigue” of 80 and a “consumption fatigue” of 20, a player’s current remaining fatigue must be at least 80, and it will decrease by 20 after exploring the dungeon.
2024-4-11
High Score Kit: Stack&Queue#2
Problem The Programmers team is currently performing feature improvement tasks. Each feature can only be released when its progress reaches 100%. However, since the development speed of each feature varies, it is possible for features developed later to be completed earlier. In such cases, a feature that is developed later will be released together with the feature in front of it that is scheduled for earlier release.
2024-4-10
High Score Kit: Stack&Queue#1
Problem Given an array arr consisting of integers ranging from 0 to 9, the task is to remove consecutive duplicates while preserving the order of the remaining elements. For instance, if arr = [1, 1, 3, 3, 0, 1, 1], the result should be [1, 3, 0, 1]. Similarly, for arr = [4, 4, 4, 3, 3], the result should be [4, 3].
2024-4-10