๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1743_์Œ์‹๋ฌผ ํ”ผํ•˜๊ธฐ

๋ฟŒ์•ผ._. 2022. 4. 21. 14:42

Silver I

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

<์Œ์‹๋ฌผ ํ”ผํ•˜๊ธฐ>

๋ฌธ์ œ 

 

์ฝ”๋ ˆ์Šค์ฝ” ์ฝ˜๋„๋ฏธ๋‹ˆ์—„ 8์ธต์€ ํ•™์ƒ๋“ค์ด 3๋ผ์˜ ์‹์‚ฌ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๊ณต๊ฐ„์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ช‡๋ช‡ ๋น„์–‘์‹ฌ์ ์ธ ํ•™์ƒ๋“ค์˜ ๋งŒํ–‰์œผ๋กœ ์Œ์‹๋ฌผ์ด ํ†ต๋กœ ์ค‘๊ฐ„์ค‘๊ฐ„์— ๋–จ์–ด์ ธ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ์Œ์‹๋ฌผ๋“ค์€ ๊ทผ์ฒ˜์— ์žˆ๋Š” ๊ฒƒ๋ผ๋ฆฌ ๋ญ‰์น˜๊ฒŒ ๋ผ์„œ ํฐ ์Œ์‹๋ฌผ ์“ฐ๋ ˆ๊ธฐ๊ฐ€ ๋œ๋‹ค. 

์ด ๋ฌธ์ œ๋ฅผ ์ถœ์ œํ•œ ์„ ์ƒ๋‹˜์€ ๊ฐœ์ธ์ ์œผ๋กœ ์ด๋Ÿฌํ•œ ์Œ์‹๋ฌผ์„ ์‹ค๋‚ดํ™”์— ๋ฌปํžˆ๋Š” ๊ฒƒ์„ ์ •๋ง ์ง„์ •์œผ๋กœ ์‹ซ์–ดํ•œ๋‹ค. ์ฐธ๊ณ ๋กœ ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ•ด์•ผ ํ•  ๋‹ต์€ ์ด ๋ฌธ์ œ๋ฅผ ๋‚ธ ์กฐ๊ต๋ฅผ ๋งž์ถ”๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. 

ํ†ต๋กœ์— ๋–จ์–ด์ง„ ์Œ์‹๋ฌผ์„ ํ”ผํ•ด ๊ฐ€๊ธฐ๋ž€ ์‰ฌ์šด ์ผ์ด ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์„ ์ƒ๋‹˜์€ ๋–จ์–ด์ง„ ์Œ์‹๋ฌผ ์ค‘์— ์ œ์ผ ํฐ ์Œ์‹๋ฌผ๋งŒ์€ ํ”ผํ•ด ๊ฐ€๋ ค๊ณ  ํ•œ๋‹ค. 

์„ ์ƒ๋‹˜์„ ๋„์™€ ์ œ์ผ ํฐ ์Œ์‹๋ฌผ์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•ด์„œ “10ra"๋ฅผ ์™ธ์น˜์ง€ ์•Š๊ฒŒ ๋„์™€์ฃผ์ž.

 

 

์ž…๋ ฅ

 

์ฒซ์งธ ์ค„์— ํ†ต๋กœ์˜ ์„ธ๋กœ ๊ธธ์ด N(1 ≤ N ≤ 100)๊ณผ ๊ฐ€๋กœ๊ธธ์ด M(1 ≤ M ≤ 100) ๊ทธ๋ฆฌ๊ณ  ์Œ์‹๋ฌผ ์“ฐ๋ ˆ๊ธฐ์˜ ๊ฐœ์ˆ˜ K(1 ≤ K ≤ N×M)์ด ์ฃผ์–ด์ง„๋‹ค.  ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ K๊ฐœ์˜ ์ค„์— ์Œ์‹๋ฌผ์ด ๋–จ์–ด์ง„ ์ขŒํ‘œ (r, c)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
์ขŒํ‘œ (r, c)์˜ r์€ ์œ„์—์„œ๋ถ€ํ„ฐ, c๋Š” ์™ผ์ชฝ์—์„œ๋ถ€ํ„ฐ๊ฐ€ ๊ธฐ์ค€์ด๋‹ค. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ์ขŒํ‘œ๋Š” ์ค‘๋ณต๋˜์ง€ ์•Š๋Š”๋‹ค.

 

 

์ถœ๋ ฅ 

 

์ฒซ์งธ ์ค„์— ์Œ์‹๋ฌผ ์ค‘ ๊ฐ€์žฅ ํฐ ์Œ์‹๋ฌผ์˜ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•˜๋ผ.

 

 

๋ฌธ์ œ ํ’€์ด

   - my solution (Python)

import sys
from collections import deque

if __name__=='__main__':
    n,m,k=map(int,sys.stdin.readline().split()) # ์„ธ๋กœ ๊ธธ์ด, ๊ฐ€๋กœ ๊ธธ์ด, ๊ฐœ์ˆ˜

    graph=[[0]*m for i in range(n)]
    for i in range(k): # ์ขŒํ‘œ ์ž…๋ ฅ
        r,c=map(int,sys.stdin.readline().split())
        graph[r-1][c-1]=1
    
    area=[]
    dx=[-1,1,0,0] # ์ƒํ•˜์ขŒ์šฐ
    dy=[0,0,-1,1]
    
    for i in range(n):
        for j in range(m):
            if graph[i][j]==1:
                queue=deque([[i,j]])
                graph[i][j]=0
                cnt=1
                
                while queue:
                    temp=queue.popleft()
                    for r in range(4):
                        x=temp[0]+dx[r]
                        y=temp[1]+dy[r]
                        if x>=0 and x<n and y>=0 and y<m: # ์ขŒํ‘œ ๋ฒ”์œ„ ์•ˆ
                            if graph[x][y]==1:
                                graph[x][y]=0
                                queue.append([x,y])
                                cnt+=1 # ํฌ๊ธฐ +1
                area.append(cnt)
    print(max(area)) # ๊ฐ€์žฅ ํฐ ๊ฐ’ ์ถœ๋ ฅ

 

- my solution (JAVA)

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

public class _1743_์Œ์‹๋ฌผํ”ผํ•˜๊ธฐ {

	public static void main(String[] args) throws IOException {
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		
		String s=bf.readLine();
		int n=Integer.parseInt(s.split(" ")[0]); // ์„ธ๋กœ ๊ธธ์ด
		int m=Integer.parseInt(s.split(" ")[1]); // ๊ฐ€๋กœ ๊ธธ์ด
		int k=Integer.parseInt(s.split(" ")[2]); // ๊ฐœ์ˆ˜
		
		int graph[][]=new int[n][m];
		for(int i=0; i<k; i++) { // ์ขŒํ‘œ ์ž…๋ ฅ
			s=bf.readLine();
			int r=Integer.parseInt(s.split(" ")[0]);
			int c=Integer.parseInt(s.split(" ")[1]);
			graph[r-1][c-1]=1;
		}
		
		int dx[]= {-1,1,0,0}; // ์ƒํ•˜์ขŒ์šฐ
		int dy[]= {0,0,-1,1};
		int max_area=0; // ๊ฐ€์žฅ ํฐ ๊ฐ’
		
		for(int i=0; i<n; i++) {
			for(int j=0; j<m; j++) {
				if(graph[i][j]==1) {
					Queue<int[]>queue=new LinkedList<>();
					queue.add(new int[] {i,j});
					graph[i][j]=0;
					int cnt=1;
					
					while(queue.size()!=0) {
						int temp[]=queue.poll();
						for(int r=0; r<4; r++) {
							int x=temp[0]+dx[r];
							int y=temp[1]+dy[r];
							if(x>=0 && x<n && y>=0 && y<m) { // ์ขŒํ‘œ ๋ฒ”์œ„ ์•ˆ
								if(graph[x][y]==1) {
									graph[x][y]=0;
									cnt+=1;
									queue.add(new int[] {x,y});
								}
							}
						}
					}
					if(max_area<cnt) {
						max_area=cnt;
					}
				}
			}
		}
		System.out.println(max_area);
	}
}

 

 

์Œ์‹๋ฌผ์ด ๋–จ์–ด์ง„  ์ขŒํ‘œ์— ๋งž๊ฒŒ 1๋กœ ํ‘œ์‹œํ•ด์ค๋‹ˆ๋‹ค. ๊ทธ ํ›„ ์ขŒํ‘œ๋ฅผ ๋‹ค ํƒ์ƒ‰ํ•˜์—ฌ ์Œ์‹๋ฌผ์ด ๋–จ์–ด์ ธ ์žˆ๋‹ค๋ฉด ( = ๊ฐ’์ด 1์ด๋ผ๋ฉด ) ๊ทธ ์ฃผ๋ณ€์„ ํƒ์ƒ‰ํ•˜๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค. ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ์Œ์‹๋ฌผ์ด ๋–จ์–ด์ ธ ์žˆ๋‹ค๋ฉด ๊ณ„์†ํ•ด์„œ ํƒ์ƒ‰ํ•˜์—ฌ ์ฃผ๋ณ€์— ์Œ์‹๋ฌผ์ด ์—†์„ ๋•Œ๊นŒ์ง€ ํ™•์ธํ•ด์ค๋‹ˆ๋‹ค. ์Œ์‹๋ฌผ์˜ ํฌ๊ธฐ ์ค‘์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ถœ๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

 

  • ์„ธ๋กœ ๊ธธ์ด (n), ๊ฐ€๋กœ๊ธธ์ด (m), ์Œ์‹๋ฌผ ์“ฐ๋ ˆ๊ธฐ์˜ ๊ฐœ์ˆ˜ (k) ์ž…๋ ฅ
  • graph = ์ขŒํ‘œ ( ์Œ์‹๋ฌผ ์“ฐ๋ ˆ๊ธฐ ํ‘œ์‹œ )
  • area = ์Œ์‹๋ฌผ์˜ ํฌ๊ธฐ ์ €์žฅ
  • ์ขŒํ‘œ๋ฅผ ํ™•์ธํ•˜๋ฉฐ graph ์นธ์ด 1์ด๋ผ๋ฉด ์Œ์‹๋ฌผ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๊ทธ ์ฃผ๋ณ€์„ ํ™•์ธ -> ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํ™•์ธํ•˜์—ฌ ์ขŒํ‘œ ๋ฒ”์œ„ ์•ˆ์ด๋ฉฐ ๊ทธ ์นธ์—๋„ ์Œ์‹๋ฌผ ์“ฐ๋ ˆ๊ธฐ๊ฐ€ ์žˆ๋‹ค๋ฉด ํฌ๊ธฐ + 1 -> ๊ณ„์†ํ•ด์„œ ํ™•์ธํ•˜์—ฌ ๋ชจ๋“  ์ขŒํ‘œ๋ฅผ ํ™•์ธํ•˜๋„๋ก ํ•ด์ค€๋‹ค.
  • ์Œ์‹๋ฌผ ์“ฐ๋ ˆ๊ธฐ์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•œ ๊ฐ’์„ area์— ์ €์žฅ
  • area์— ์ €์žฅํ•œ ๊ฐ’ ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ถœ๋ ฅ

 


์ƒ๊ฐ๐Ÿค”