[BOJ 1303] ์ „์Ÿ-์ „ํˆฌ

 

1. ๋ฌธ์ œ : Link

๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์•„๊ตฐ ์ ๊ตฐ ํž˜ ๋น„๊ต

 

2. ํ’€์ด

DFS ํ’€์ด

* ์ธ๋ฑ์Šค ์ ‘๊ทผํ•  ๋•Œ ํ•ญ์ƒ ์ฃผ์˜ํ•˜๊ธฐ!

(๊ฐ€๋กœ ์„ธ๋กœ ํ—ท๊ฐˆ๋ฆฌ๊ธฐ ์‰ฌ์›€)

 

3. ์ฝ”๋“œ

package baekjoon;

import java.io.IOException;
import java.util.*;
public class ์ „์Ÿ์ „ํˆฌ {
    static int N,M;
    static char[][] map;
    static boolean[][] visited;
    static int[] xx = {0,0,1,-1};
    static int[] yy = {1,-1,0,0};
    static int wVal = 0;
    static int bVal = 0;
    static int cnt = 0;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        M = sc.nextInt();

        map = new char[M][N];
        visited = new boolean[M][N];

        for(int i=0; i<M; i++){
            String temp  = sc.next();
            for(int j=0; j<N; j++){
                map[i][j] = temp.charAt(j);
            }
        }

        for(int i=0; i<M; i++){
            for(int j=0; j<N; j++){
                if(!visited[i][j] && map[i][j] == 'W'){
                    dfs(i,j,'W');
                    wVal += cnt*cnt;
                }else if(!visited[i][j] && map[i][j] == 'B'){
                    dfs(i,j,'B');
                    bVal += cnt*cnt;
                }
                cnt = 0;
            }
        }

        System.out.print(wVal + " " + bVal);

    }

    public static void dfs(int x, int y, char color) {
        visited[x][y] = true;
        cnt++;

        for(int i=0; i<4; i++){
            int ix = x + xx[i];
            int iy = y + yy[i];

            if(ix >= 0 && ix < M && iy >= 0 && iy<N && !visited[ix][iy] && map[ix][iy] == color){
                dfs(ix,iy,color);
            }
        }
    }
}

 

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ