๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 2178_๋ฏธ๋กœ ํƒ์ƒ‰

๋ฟŒ์•ผ._. 2022. 3. 31. 14:59

Silver I

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/2178)

<๋ฏธ๋กœ ํƒ์ƒ‰>

๋ฌธ์ œ 

 

N×Mํฌ๊ธฐ์˜ ๋ฐฐ์—ด๋กœ ํ‘œํ˜„๋˜๋Š” ๋ฏธ๋กœ๊ฐ€ ์žˆ๋‹ค.

 

1 0 1 1 1 1
1 0 1 0 1 0
1 0 1 0 1 1
1 1 1 0 1 1

 

๋ฏธ๋กœ์—์„œ 1์€ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์นธ์„ ๋‚˜ํƒ€๋‚ด๊ณ , 0์€ ์ด๋™ํ•  ์ˆ˜ ์—†๋Š” ์นธ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฏธ๋กœ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, (1, 1)์—์„œ ์ถœ๋ฐœํ•˜์—ฌ (N, M)์˜ ์œ„์น˜๋กœ ์ด๋™ํ•  ๋•Œ ์ง€๋‚˜์•ผ ํ•˜๋Š” ์ตœ์†Œ์˜ ์นธ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ํ•œ ์นธ์—์„œ ๋‹ค๋ฅธ ์นธ์œผ๋กœ ์ด๋™ํ•  ๋•Œ, ์„œ๋กœ ์ธ์ ‘ํ•œ ์นธ์œผ๋กœ๋งŒ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.

์œ„์˜ ์˜ˆ์—์„œ๋Š” 15์นธ์„ ์ง€๋‚˜์•ผ (N, M)์˜ ์œ„์น˜๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. ์นธ์„ ์…€ ๋•Œ์—๋Š” ์‹œ์ž‘ ์œ„์น˜์™€ ๋„์ฐฉ ์œ„์น˜๋„ ํฌํ•จํ•œ๋‹ค.

 

 

์ž…๋ ฅ

 

์ฒซ์งธ ์ค„์— ๋‘ ์ •์ˆ˜ N, M(2 ≤ N, M ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” M๊ฐœ์˜ ์ •์ˆ˜๋กœ ๋ฏธ๋กœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ๊ฐ์˜ ์ˆ˜๋“ค์€ ๋ถ™์–ด์„œ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค.

 

 

์ถœ๋ ฅ 

 

์ฒซ์งธ ์ค„์— ์ง€๋‚˜์•ผ ํ•˜๋Š” ์ตœ์†Œ์˜ ์นธ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ํ•ญ์ƒ ๋„์ฐฉ ์œ„์น˜๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋งŒ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค.

 

 

๋ฌธ์ œ ํ’€์ด

   - my solution (Python)

import sys
from collections import deque

def maze(graph, start, depth):
    dx=[-1,1,0,0] # ์ƒํ•˜์ขŒ์šฐ
    dy=[0,0,-1,1]

    visited = [[0] * len(graph[0]) for i in range(len(graph))] # ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
    visited[start[0]][start[1]]=1
    queue=deque([start])

    while queue:
        temp=queue.popleft()
        for i in range(4):
            x=temp[0]+dx[i]
            y=temp[1]+dy[i]
            if  x>=0 and x<len(graph) and y>=0 and y<len(graph[0]): # ๋ฏธ๋กœ ๋ฒ”์œ„ ์•ˆ์ด๋ผ๋ฉด
                if graph[x][y]==1 and visited[x][y]==0: # ์ด๋™ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด
                    queue.append([x,y])
                    visited[x][y]=1
                    depth[x][y]=depth[temp[0]][temp[1]]+1
if __name__=='__main__':
    n,m=map(int, sys.stdin.readline().split())

    graph=[]
    for i in range(n):
        graph.append(list(map(int, sys.stdin.readline().strip())))

    depth = [[0] * m for i in range(n)]
    start=[0,0] # ์ถœ๋ฐœ

    maze(graph, start, depth)

    print(depth[n-1][m-1]+1)

ํ•œ ๋ฒˆ์— ํ†ต๊ณผํ•˜๊ณ  ์‹ถ์–ด์„œ ์›๋ž˜ list ์“ฐ๋˜ ๊ฑฐ deque ์ผ๋‹ค!

 

 

- my solution (JAVA)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class _2178_๋ฏธ๋กœํƒ์ƒ‰ {
	
	static void maze(int graph[][], int start[], int depth[][]) {
		int dx[]= {-1,1,0,0}; //์ƒํ•˜์ขŒ์šฐ
		int dy[]= {0,0,-1,1};
		
		int visited[][]=new int[graph.length][graph[0].length]; // ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
		visited[start[0]][start[1]]=1;
		
		Queue<int []> queue=new LinkedList<>();
		queue.add(start);
		
		while(queue.size()!=0) {
			int[] temp=queue.poll();
			for(int i=0; i<4; i++) {
				int x=temp[0]+dx[i];
				int y=temp[1]+dy[i];
				if(x>=0&& x<graph.length && y>=0 && y<graph[0].length) { //๋ฏธ๋กœ ๋ฒ”์œ„ ์•ˆ์ด๋ผ๋ฉด
					if(graph[x][y]==1 && visited[x][y]==0) { // ์ด๋™ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด
						queue.add(new int[] {x,y});
						visited[x][y]=1;
						depth[x][y]=depth[temp[0]][temp[1]]+1;
					}
				}
			}
		}
	}

	public static void main(String[] args) throws IOException{
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		
		String str=bf.readLine();
		int n=Integer.parseInt(str.split(" ")[0]);
		int m=Integer.parseInt(str.split(" ")[1]);
		
		int graph[][]=new int[n][m];
		for(int i=0; i<n; i++) {
			str=bf.readLine();
			for(int j=0; j<m; j++) {
				graph[i][j]=Integer.parseInt(str.split("")[j]);
			}
		}
		
		int depth[][]=new int[n][m];
		int start[]= {0,0}; //์ถœ๋ฐœ
		
		maze(graph, start, depth);
		
		System.out.println(depth[n-1][m-1]+1);
	}

}

 

(0,0)๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํ™•์ธํ•˜์—ฌ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ์ด๋ฉด ๊ณ„์†ํ•ด์„œ ๋‚˜์•„๊ฐ€๋ฉฐ depth๋ฅผ ์ €์žฅํ•ด์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค.

  • maze
    : dx, dy = ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰
    : visited = ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
    : queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต -> ์ œ์ผ ์ฒ˜์Œ ๊ฐ’์„ pop -> ์ƒํ•˜์ขŒ์šฐ ํ™•์ธํ•˜์—ฌ ๋ฏธ๋กœ ๋ฒ”์œ„ ์•ˆ์ด๋ฉฐ ์ด๋™ํ•  ์ˆ˜ ์žˆ๊ณ , ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด queue์— ์ถ”๊ฐ€ ๋ฐ ๋ฐฉ๋ฌธ check, depth ์ €์žฅ
    (์ด๋™ํ•  ์œ„์น˜์˜ depth = ํ˜„์žฌ ์œ„์น˜์˜ depth +1)
  • main
    : n, m ์ž…๋ ฅ
    : ๋ฏธ๋กœ ์ž…๋ ฅ
    : depth = ์ง€๋‚˜์•ผ ํ•˜๋Š” ์นธ ์ˆ˜ ์ €์žฅ
    : ํ•จ์ˆ˜ ํ˜ธ์ถœ
    : answer = ์‹œ์ž‘ ์œ„์น˜์™€ ๋„์ฐฉ ์œ„์น˜๋„ ํฌํ•จ ์ด๋ฏ€๋กœ depth [n-1][m-1] +1 

 


์ƒ๊ฐ๐Ÿค”

 

๋ณด์ž๋งˆ์ž ํ›„๋‹ค๋‹ฅ ์ฝ”๋“œ ๊ตฌํ˜„ํ•ด์„œ ์„ฑ๊ณต -!

ํ•œ ๋ฒˆ์— ํ†ต๊ณผํ•ด์„œ ๋ฟŒ๋“ฏ-!

JAVA ๋นจ๋ฆฌ.. ์ต์ˆ™ํ•ด์ ธ์•ผ๊ฒ ๋‹ค. ๊นŒ๋จน์€ ๋‚ด์šฉ์ด ๋„ˆ๋ฌด ๋งŽ์•„์„œ...