๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 11403_๊ฒฝ๋กœ ์ฐพ๊ธฐ

๋ฟŒ์•ผ._. 2022. 4. 7. 16:30

Silver I

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

<๊ฒฝ๋กœ ์ฐพ๊ธฐ>

๋ฌธ์ œ 

 

๊ฐ€์ค‘์น˜ ์—†๋Š” ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„ G๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ชจ๋“  ์ •์  (i, j)์— ๋Œ€ํ•ด์„œ, i์—์„œ j๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

 

์ž…๋ ฅ

 

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ ์ค„์—๋Š” ๊ทธ๋ž˜ํ”„์˜ ์ธ์ ‘ ํ–‰๋ ฌ์ด ์ฃผ์–ด์ง„๋‹ค. i๋ฒˆ์งธ ์ค„์˜ j๋ฒˆ์งธ ์ˆซ์ž๊ฐ€ 1์ธ ๊ฒฝ์šฐ์—๋Š” i์—์„œ j๋กœ ๊ฐ€๋Š” ๊ฐ„์„ ์ด ์กด์žฌํ•œ๋‹ค๋Š” ๋œป์ด๊ณ , 0์ธ ๊ฒฝ์šฐ๋Š” ์—†๋‹ค๋Š” ๋œป์ด๋‹ค. i๋ฒˆ์งธ ์ค„์˜ i๋ฒˆ์งธ ์ˆซ์ž๋Š” ํ•ญ์ƒ 0์ด๋‹ค.

 

 

์ถœ๋ ฅ 

 

์ด N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ๋ฌธ์ œ์˜ ์ •๋‹ต์„ ์ธ์ ‘ ํ–‰๋ ฌ ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค. ์ •์  i์—์„œ j๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์žˆ์œผ๋ฉด i๋ฒˆ์งธ ์ค„์˜ j๋ฒˆ์งธ ์ˆซ์ž๋ฅผ 1๋กœ, ์—†์œผ๋ฉด 0์œผ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

 

 

๋ฌธ์ œ ํ’€์ด

   - my solution (Python)

import sys
from collections import deque

def bfs(graph, visited, start):
    queue=deque([start])
    result=[] # ๊ฒฝ๋กœ

    while queue:
        temp=queue.popleft()
        for i in graph[temp]:
            if visited[i]==0:
                queue.append(i)
                visited[i]=1
                result.append(i)
    return result

if __name__=='__main__':
    n=int(input()) # ์ •์ ์˜ ๊ฐœ์ˆ˜

    graph={}
    for i in range(n):
        graph[i]=[]

    for i in range(n): # ์ธ์ ‘ ํ–‰๋ ฌ : i -> j ๊ฒฝ๋กœ ํ‘œ์‹œ
        s=list(map(int, sys.stdin.readline().split()))
        for j in range(n):
            if s[j]==1:
                graph[i].append(j)

    answer=[[0]*n for i in range(n)]

    for i in range(n):
        visited = [0] * n # ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
        result=bfs(graph, visited, i)
        for j in result: # i -> j ๊ฒฝ๋กœ ์กด์žฌ
            answer[i][j]=1

    for i in answer:
        print(' '.join(map(str,i)))

 

- my solution (JAVA)

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

public class _11403_๊ฒฝ๋กœ์ฐพ๊ธฐ {
	static ArrayList<Integer> bfs(HashMap graph, int visited[], int start) {
		Queue<Integer> queue=new LinkedList<>();
		ArrayList<Integer> result=new ArrayList<>(); // ๊ฒฝ๋กœ
		
		queue.add(start);
		
		while(queue.size()!=0) {
			int temp=queue.poll();
			ArrayList<Integer> x=(ArrayList<Integer>) graph.get(temp);
			for(int i=0; i<x.size(); i++) {
				if(visited[x.get(i)] == 0) {
					queue.add(x.get(i));
					visited[x.get(i)]=1;
					result.add(x.get(i));
				}
			}
		}
		return result;
	}

	public static void main(String[] args) throws IOException {
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		
		int n=Integer.parseInt(bf.readLine()); // ์ •์ ์˜ ๊ฐœ์ˆ˜
		HashMap<Integer, ArrayList<Integer>> graph=new HashMap<>();
		
		
		for(int i=0; i<n; i++) {
			graph.put(i, new ArrayList<>());
		}
		
		int temp[]=new int[n];
		for(int i=0; i<n; i++) { // ์ธ์ ‘ ํ–‰๋ ฌ : i -> j ๊ฒฝ๋กœ ํ‘œ์‹œ
			String s=bf.readLine();
			for(int j=0; j<n; j++) {
				temp[j]=Integer.parseInt(s.split(" ")[j]);
			}
			for(int j=0; j<n; j++) {
				if(temp[j]==1) {
					ArrayList<Integer> x=graph.get(i);
					x.add(j);
					graph.put(i,x);
				}
			}
		}
		int answer[][]=new int[n][n];
		
		for(int i=0; i<n; i++) {
			int visited[]=new int[n]; // ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
			ArrayList<Integer> result=bfs(graph,visited,i);
			for(int j=0; j<result.size(); j++) { // i -> j ๊ฒฝ๋กœ ์กด์žฌ
				answer[i][result.get(j)]=1;
			}
		}
		for(int i=0; i<n; i++) {
			for(int j=0; j<n; j++) {
				System.out.print(answer[i][j]+" ");
			}
			System.out.println();
		}
	}

}

 

์ฃผ์–ด์ง„ ์ธ์ ‘ ํ–‰๋ ฌ์— ๋”ฐ๋ผ i -> j๋กœ ์—ฐ๊ฒฐ๋œ ๊ฒฝ๋กœ๋ฅผ dict, HashMap์œผ๋กœ ์ •๋ฆฌ ํ›„ ๊ฐ ์ •์ ์—์„œ bfs๋ฅผ ํ†ตํ•˜์—ฌ ๊ฐ„์„ ์˜ ์—ฌ๋ถ€๋ฅผ ์•Œ์•„๋‚ธ๋‹ค.

 

  • bfs ( graph, ๋ฐฉ๋ฌธ ์—ฌ๋ถ€, ์‹œ์ž‘ ์œ„์น˜)
    : result = ์‹œ์ž‘ ์œ„์น˜์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ •์ 
    : queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต -> ์ œ์ผ ์ฒ˜์Œ ๊ฐ’์„ pop -> pop ํ•œ ๊ฐ’์—์„œ ์—ฐ๊ฒฐ๋œ ์ •์ ์„ ์ˆœํ™˜ -> ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์œผ๋ฉด qeuue์— ์ถ”๊ฐ€ ๋ฐ ๋ฐฉ๋ฌธ check, result์— ์ถ”๊ฐ€
    : result ๋ฐ˜ํ™˜

  • main
    : n = ์ •์ ์˜ ๊ฐœ์ˆ˜
    : graph = ๊ฐ„์„  ์—ฐ๊ฒฐ
    : ์ฃผ์–ด์ง„ ์ธ์ ‘ ํ–‰๋ ฌ์„ ์ž…๋ ฅ๋ฐ›์•„ i -> j ๊ฒฝ๋กœ ํ‘œ์‹œ
    : answer = ์ •๋‹ต ( ์ธ์ ‘ ํ–‰๋ ฌ ํ˜•์‹ )
    : ๊ฐ ์ •์ ์—์„œ ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ›„ ๋ฐ˜ํ™˜ํ•œ ๊ฐ’์„ ํ™œ์šฉํ•˜์—ฌ ๊ฒฝ๋กœ ์กด์žฌํ•˜๋Š” ๊ณณ 1๋กœ ์ €์žฅ
    : answer ( ์ธ์ ‘ ํ–‰๋ ฌ ) ์ถœ๋ ฅ

์ƒ๊ฐ๐Ÿค”

 

ํ•œ ๋ฒˆ๋งŒ์— ํ†ต๊ณผํ•˜๋Š” ๊ฑด ๋„ˆ๋ฌด ๊ธฐ๋ถ„ ์ข‹์€ ์ผ!!!

ํฌ๊ฒŒ ์–ด๋ ต์ง€ ์•Š์•˜๋˜ ๋ฌธ์ œ์˜€๋‹ค.