๐Ÿƒ๐Ÿปโ€โ™€๏ธ[์ปค๋ฎค๋Ÿฌ๋‹/1๊ธฐ] 2์ฃผ์ฐจB - BFS

์ด๋‚˜์˜ยท2021๋…„ 11์›” 8์ผ
0

์ปค๋ฎค๋Ÿฌ๋‹1๊ธฐ

๋ชฉ๋ก ๋ณด๊ธฐ
6/8
post-thumbnail

๐ŸŽฏ2์ฃผ์ฐจB "๊ฒŒ์ž„ ๋งต ์ตœ๋‹จ๊ฑฐ๋ฆฌ"

๐ŸŽƒ๋ฌธ์ œ์„ค๋ช…

๐ŸŽฎROR ๊ฒŒ์ž„์€ ๋‘ ํŒ€์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ง„ํ–‰ํ•˜๋ฉฐ, ์ƒ๋Œ€ ํŒ€ ์ง„์˜์„ ๋จผ์ € ํŒŒ๊ดดํ•˜๋ฉด ์ด๊ธฐ๋Š” ๊ฒŒ์ž„์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ๊ฐ ํŒ€์€ ์ƒ๋Œ€ ํŒ€ ์ง„์˜์— ์ตœ๋Œ€ํ•œ ๋นจ๋ฆฌ ๋„์ฐฉํ•˜๋Š” ๊ฒƒ์ด ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

์ง€๊ธˆ๋ถ€ํ„ฐ ๋‹น์‹ ์€ ํ•œ ํŒ€์˜ ํŒ€์›์ด ๋˜์–ด ๊ฒŒ์ž„์„ ์ง„ํ–‰ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ 5 x 5 ํฌ๊ธฐ์˜ ๋งต์—, ๋‹น์‹ ์˜ ์บ๋ฆญํ„ฐ๊ฐ€ (ํ–‰: 1, ์—ด: 1) ์œ„์น˜์— ์žˆ๊ณ , ์ƒ๋Œ€ ํŒ€ ์ง„์˜์€ (ํ–‰: 5, ์—ด: 5) ์œ„์น˜์— ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

์œ„ ๊ทธ๋ฆผ์—์„œ ๊ฒ€์€์ƒ‰ ๋ถ€๋ถ„์€ ๋ฒฝ์œผ๋กœ ๋ง‰ํ˜€์žˆ์–ด ๊ฐˆ ์ˆ˜ ์—†๋Š” ๊ธธ์ด๋ฉฐ, ํฐ์ƒ‰ ๋ถ€๋ถ„์€ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ์ž…๋‹ˆ๋‹ค. ์บ๋ฆญํ„ฐ๊ฐ€ ์›€์ง์ผ ๋•Œ๋Š” ๋™, ์„œ, ๋‚จ, ๋ถ ๋ฐฉํ–ฅ์œผ๋กœ ํ•œ ์นธ์”ฉ ์ด๋™ํ•˜๋ฉฐ, ๊ฒŒ์ž„ ๋งต์„ ๋ฒ—์–ด๋‚œ ๊ธธ์€ ๊ฐˆ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
์•„๋ž˜ ์˜ˆ์‹œ๋Š” ์บ๋ฆญํ„ฐ๊ฐ€ ์ƒ๋Œ€ ํŒ€ ์ง„์˜์œผ๋กœ ๊ฐ€๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ๋‚˜ํƒ€๋‚ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ 11๊ฐœ์˜ ์นธ์„ ์ง€๋‚˜์„œ ์ƒ๋Œ€ ํŒ€ ์ง„์˜์— ๋„์ฐฉํ–ˆ์Šต๋‹ˆ๋‹ค.

๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ 15๊ฐœ์˜ ์นธ์„ ์ง€๋‚˜์„œ ์ƒ๋Œ€ํŒ€ ์ง„์˜์— ๋„์ฐฉํ–ˆ์Šต๋‹ˆ๋‹ค.

์œ„ ์˜ˆ์‹œ์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•๋ณด๋‹ค ๋” ๋น ๋ฅด๊ฒŒ ์ƒ๋Œ€ํŒ€ ์ง„์˜์— ๋„์ฐฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—†์œผ๋ฏ€๋กœ, ์ด ๋ฐฉ๋ฒ•์ด ์ƒ๋Œ€ ํŒ€ ์ง„์˜์œผ๋กœ ๊ฐ€๋Š” ๊ฐ€์žฅ ๋น ๋ฅธ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

๋งŒ์•ฝ, ์ƒ๋Œ€ ํŒ€์ด ์ž์‹ ์˜ ํŒ€ ์ง„์˜ ์ฃผ์œ„์— ๋ฒฝ์„ ์„ธ์›Œ๋‘์—ˆ๋‹ค๋ฉด ์ƒ๋Œ€ ํŒ€ ์ง„์˜์— ๋„์ฐฉํ•˜์ง€ ๋ชปํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์— ๋‹น์‹ ์˜ ์บ๋ฆญํ„ฐ๋Š” ์ƒ๋Œ€ ํŒ€ ์ง„์˜์— ๋„์ฐฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๊ฒŒ์ž„ ๋งต์˜ ์ƒํƒœ maps๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์บ๋ฆญํ„ฐ๊ฐ€ ์ƒ๋Œ€ ํŒ€ ์ง„์˜์— ๋„์ฐฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ง€๋‚˜๊ฐ€์•ผ ํ•˜๋Š” ์นธ์˜ ๊ฐœ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ๋‹จ, ์ƒ๋Œ€ ํŒ€ ์ง„์˜์— ๋„์ฐฉํ•  ์ˆ˜ ์—†์„ ๋•Œ๋Š” -1์„ return ํ•ด์ฃผ์„ธ์š”.


๐Ÿ”’์ œํ•œ์‚ฌํ•ญ

mapsanswer
[[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

๐Ÿ’พ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…์ถœ๋ ฅ ์˜ˆ #1
์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์บ๋ฆญํ„ฐ๊ฐ€ ์  ํŒ€์˜ ์ง„์˜๊นŒ์ง€ ์ด๋™ํ•˜๋Š” ๊ฐ€์žฅ ๋น ๋ฅธ ๊ธธ์€ ๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ด 11์นธ์„ ์บ๋ฆญํ„ฐ๊ฐ€ ์ง€๋‚˜๊ฐ”์œผ๋ฏ€๋กœ 11์„ return ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2
๋ฌธ์ œ์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์œผ๋ฉฐ, ์ƒ๋Œ€ ํŒ€ ์ง„์˜์— ๋„๋‹ฌํ•  ๋ฐฉ๋ฒ•์ด ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ -1์„ return ํ•ฉ๋‹ˆ๋‹ค.



๐ŸŒŸ๋ฌธ์ œ ์ดํ•ด ๋ฐ ํ’€์ด๊ณ„ํš

โœ๏ธ์ฒ˜์Œ์—๋Š” DFS๋กœ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ๊ทธ๋Ÿฌ๋ฉด ๋ฐฑํŠธ๋ž˜ํ‚น์„ ํ•ด์„œ ๋‹ค์‹œ ๋Œ์•„๊ฐ€์•ผ ํ•˜๋‚˜? ํ•˜๊ณ  ์ƒ๊ฐ์ด ๋ณต์žกํ–ˆ์—ˆ๋‹ค. ์•ˆ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ์ตœ๋Œ€ํ•œ ๋นจ๋ฆฌ ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” BFS๊ฐ€ ๋” ์ข‹์€ ๋ฐฉ๋ฒ• ์ธ ๊ฒƒ ๊ฐ™๋‹ค.
โœ๏ธํšจ์œจ์„ฑ ๋ฉด์—์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๊ธธ๋ž˜ visited๋ฐฐ์—ด์„ ๋”ฐ๋กœ ๋‘์ง€ ์•Š์œผ๋ ค๊ณ  ์ง€๋‚˜์˜จ ๊ธธ์„ 0์œผ๋กœ ๋งŒ๋“ค์–ด ์คฌ๋Š”๋ฐ ์™œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ ๊นŒ ์ƒ๊ฐํ–ˆ๋‹ค. ๋ฌธ์ œ๋Š” ํ์— ๋“ค์–ด๊ฐ€๋Š” ๊ธธ์€ ์•„์ง ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๋ณต์œผ๋กœ ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์ด ๋ฌธ์ œ์˜€๋‹ค.
=> ๊ฒฐ๊ตญ visited๋ฐฐ์—ด์„ ๋‘์–ด ํ์— ๋“ค์–ด๊ฐ€๋Š” ๊ธธ์„ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ ํ•˜์—ฌ ์ค‘๋ณต์œผ๋กœ ํ์— ๋“ค์–ด๊ฐ€์ง€ ์•Š๋„๋ก ์ฒ˜๋ฆฌํ–ˆ๋”๋‹ˆ ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ–ˆ๋‹ค.


import java.util.*;

class Solution {
    static boolean[][] visited;
    
    // ๋‚˜์˜ ์œ„์น˜์ •๋ณด, ํ˜„์žฌ๊นŒ์ง€ ์ด๋™๊ฑฐ๋ฆฌ 
    public static class Location {
    	int x, y, move;

	public Location(int x, int y, int move) {
    	    this.x = x;
            this.y = y;
            this.move = move;
        }
    }
    
    public int solution(int[][] maps) {
        int n = maps.length; // ํ–‰์˜ ๊ธธ์ด
        int m = maps[0].length; // ์—ด์˜ ๊ธธ์ด
        
        return bfs(maps, n, m);
    }
    
    public static int bfs(int[][] maps, int n, int m) {
    	Queue<Location> q = new LinkedList<Location>();
        visited = new boolean[n][m];
        int cur_x = 0;
        int cur_y = 0;
        int cur_move = 1; // ํ˜„์žฌ๊นŒ์ง€ ์ด๋™ ๊ฑฐ๋ฆฌ
        q.add(new Location(cur_x, cur_y, cur_move));
		
        while(!q.isEmpty()) {
           Location location = (Location) q.poll();
           cur_x = location.x;
           cur_y = location.y;
           cur_move = location.move;
           System.out.println(cur_x + " " + cur_y);
			
            // ๋ฐฉ๋ฌธ ์ฒดํฌ // ์ง€๋‚˜๊ฐ„ ๊ธธ์€ 0์œผ๋กœ ๋งŒ๋“ค์–ด ์ค€๋‹ค.
            maps[cur_x][cur_y] = 0; 
            visited[cur_x][cur_y] = true;
			
            for(int i=0; i<4; i++) {
                if(cur_x + dx[i] >= 0 && cur_x + dx[i] < n && cur_y + dy[i] >= 0 && cur_y + dy[i] < m && !visited[cur_x + dx[i]][cur_y + dy[i]]) { // ๋งต์˜ ๋ฒ”์œ„ ์•ˆ์— ์žˆ์„ ๋•Œ
                    // ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ์ผ ๋•Œ
                    if(maps[cur_x+dx[i]][cur_y+dy[i]] == 1) {
                        if(cur_x+dx[i] == n-1 && cur_y+dy[i] == m-1) { // ์ƒ๋Œ€ํŒ€ ์ง„์˜์— ๋„์ฐฉ ์‹œ
                            cur_move += 1;
                            return cur_move;
                        }
                        visited[cur_x + dx[i]][cur_y + dy[i]] = true;
                        q.add(new Location(cur_x+dx[i], cur_y+dy[i], cur_move+1));
                    }
                }
            }
    	}
		
    	return -1;
    }
}

๐Ÿญ๊ฐ•์˜๋‚ด์šฉ

โœ”๏ธ๋„ˆ๋น„์šฐ์„ ํƒ์ƒ‰(BFS)์˜ ์ „ํ˜•์ ์ธ ๋ฌธ์ œ
โœ”๏ธ์‚ฌ๋ฐฉ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ์„ ๋ชจ๋‘ ํƒ์ƒ‰ํ•ด ๋ณด๋Š” ๊ฒƒ. (๋ฒฝ๊ณผ ๋‹ค๋…€์˜จ ๊ธธ ์ œ์™ธ)
โœ”๏ธ๋‚˜๋Š” ๊ทœ์น™์„ ๋งŒ๋“ค๊ณ , ์—ฐ์‚ฐ์€ ์ปดํ“จํ„ฐ๊ฐ€!


โœ๐Ÿป๊ฐ•์˜ ์ฝ”๋“œ

import java.util.*;

class Solution {
    
    class Position { // ํ˜„์žฌ ์œ„์น˜ ํด๋ž˜์Šค
        int x, y;
        
        Position(int x, int y) {
            this.x = x;
            this.y = y;
        }
        
        boolean isValid(int width, int height) {
            if(x < 0 || x >= width) return false;
            if(y < 0 || y >= height) return false;
            return true;
        }
    }
    
    public int solution(int[][] maps) {
        int mapHeight = maps.length;
        int mapWidth = maps[0].length;
        
        // BFS : Queue // ์ธํ„ฐํŽ˜์ด์Šค๋กœ๋งŒ ์ œ๊ณต. ํ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์—ฌ๋Ÿฌ ์˜ค๋ธŒ์ ํŠธ ์ค‘ LinkedList๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
        Queue<Position> queue = new LinkedList<>();
        int[][] count = new int[mapHeight][mapWidth];
        boolean[][] visited = new boolean[mapHeight][mapWidth];
        
        queue.offer(new Position(0, 0));
        count[0][0] = 1;
        visited[0][0] = true;
        
        while(!queue.isEmpty()) {
            Position current = queue.poll();
            
            int currentCount = count[current.y][current.x];
            
            // 4๊ฐ€์ง€ ๊ฒฝ์šฐ 
            final int[][] moving = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
            for(int i=0; i<moving.length; i++){
                // ์ƒ์„ฑ์ž ํ•„์š”!
                Position moved = new Position(current.x + moving[i][0], current.y + moving[i][1]);
                
                if(!moved.isValid(mapWidth, mapHeight)) continue;
                if(visited[moved.y][moved.x]) continue;
                if(maps[moved.y][moved.x] == 0) continue; // 0: ๋ฒฝ, 1: ๊ธธ
                
                count[moved.y][moved.x] = currentCount + 1;
                visited[moved.y][moved.x] = true;
                queue.offer(moved);
            }
            
        }
        
        int answer = count[mapHeight-1][mapWidth-1];
        if(answer == 0) return -1;

        return answer;
    }
}

๐Ÿ’ก๋‚˜์˜ ํ’€์ด๋Š” dx, dy๋ฅผ ๋”ฐ๋กœ ๋‘์–ด ๊ฐ๊ฐ ๋”ํ•ด์คฌ๋Š”๋ฐ moving๋ฐฐ์—ด๋กœ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ธ ๊ฒƒ ๊ฐ™๋‹ค!
๐Ÿ’กx, y์™€ mapWidth, mapHeight์˜ ๊ฐ๊ฐ์˜ ์œ„์น˜๊ฐ€ ํ—ท๊น”๋ฆฌ๋Š”๋ฐ ์ฝ”๋“œ ์ž‘์„ฑ ์‹œ ์ •ํ™•ํžˆ ์–ด๋Š ์œ„์น˜์— ๋“ค์–ด๊ฐ€์•ผ ํ•˜๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.

profile
์†Œํ†ตํ•˜๋Š” ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž๋กœ ์„ฑ์žฅํ•˜๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€