(JAVA) ๋ฐฑ์ค€ 10026๋ฒˆ : ์ ๋ก์ƒ‰์•ฝ

https://www.acmicpc.net/problem/10026

 

10026๋ฒˆ: ์ ๋ก์ƒ‰์•ฝ

์ ๋ก์ƒ‰์•ฝ์€ ๋นจ๊ฐ„์ƒ‰๊ณผ ์ดˆ๋ก์ƒ‰์˜ ์ฐจ์ด๋ฅผ ๊ฑฐ์˜ ๋А๋ผ์ง€ ๋ชปํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์ด ๋ณด๋Š” ๊ทธ๋ฆผ์€ ์•„๋‹Œ ์‚ฌ๋žŒ์ด ๋ณด๋Š” ๊ทธ๋ฆผ๊ณผ๋Š” ์ข€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ํฌ๊ธฐ๊ฐ€ N×N์ธ ๊ทธ๋ฆฌ๋“œ์˜ ๊ฐ ์นธ์— R(๋นจ๊ฐ•), G(์ดˆ๋ก)

www.acmicpc.net

๋งž๋‹ค... ์ด๋ฒˆ์— ๋”๊ธ€๋กœ๋ฆฌ ์‹œ์ฆŒ 2๊นŒ์ง€ ๋‹ค ๋ณด๊ณ  ์ด ๋ฌธ์ œ ์ œ๋ชฉ๋ณด๋‹ˆ ๋„์ €ํžˆ ์ง€๋‚˜์น ์ˆ˜ ์—†์—ˆ๋‹ค..

 

์ฒซ๋ฒˆ์งธ ์‹œ๋„ (์‹คํŒจ)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.InputStreamReader;
import java.io.BufferedReader;
 
 
public class B_10026 {
    static boolean[][] vis;
    static int N;
    static char[][] G,D;
    static int[][] dic = {{1,0},{0,1},{-1,0},{0,-1}};
    static boolean check(int a , int b) {
        if  (a>=N||a<0return false;
        if (b>=N||b<0return false;
        return true;
    }
    static void bfs(int a,int b,char[][] G) {
        char k = G[a][b];
        int[][] que = new int[100000000][2];
        int front=0,rear=0;
        que[rear][0]=a;que[rear++][1]=b;
        vis[a][b] = true;
        while(front!=rear) {
            int x=que[front][0],y=que[front++][1];
            for (int i=0;i<4;i++) {
                int nx=x+dic[i][0],ny=y+dic[i][1];
                if (check(nx,ny)==true) {
                    if (G[nx][ny]!=k) continue;
                    if (vis[nx][ny]==truecontinue;
                    que[rear][0]=nx;que[rear++][1]=ny;
                    vis[nx][ny]=true;
                }
            }
        }
    }
    static int Ans(char[][] G) {
        int h=0;
        for (int i=0;i<N;i++
            for (int j=0;j<N;j++
                if (vis[i][j]!=true) { 
                    bfs(i,j,G); h+=1;
                }
 
        return h;
    }
    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        G = new char[N][N];D = new char[N][N];
        vis = new boolean[N][N];
        for (int i=0;i<N;i++) {
            String t = br.readLine();
            for (int j=0;j<N;j++) {
                G[i][j]=t.charAt(j);
                if (G[i][j]=='G') D[i][j]='R';
                else D[i][j]=G[i][j];
            }
        }
        int aa =Ans(G);
        
        vis = new boolean[N][N];
        System.out.println(aa+" "+Ans(D));
    }
 
}
 
cs

์ด๊ฒŒ ์ผ๋‹จ ์ฒซ๋ฒˆ์งธ ์‹œ๋„์ด๋‹ค. ์ผ๋‹จ ๊ตฌ์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๊ฒƒ์ด๊ธฐ์— BFS๋ฅผ ์ด์šฉํ•ด๋ณด์•˜๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ....

 

๋‘๋ฒˆ์งธ ์‹œ๋„ (์„ฑ๊ณต.....)

์•„...๋ญ ๋งž๊ธฐ์•ผ ํ–ˆ๋‹ค...๊ทผ๋ฐ ๋ฉ”๋ชจ๋ฆฌ 225968kb์— 552ms ......... ์‹ฌ์ง€์–ด ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด que์˜ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ์ค„์ด๊ธฐ๋งŒ ํ–ˆ๋‹ค.......๋” ๋‚˜์€ ์ฝ”๋“œ๊ฐ€ ์žˆ์ง€ ์•Š์„๊นŒ์‹ถ์œผ๋‹ˆ ์ข€ ๋” ๊ณ ์ณ๋ณด์ž...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.io.InputStreamReader;
import java.io.BufferedReader;
 
 
public class B_10026 {
    static boolean[][] vis;
    static int N;
    static char[][] G;
    static boolean check(int a , int b) {
        if  (a>=N||a<0return false;
        if (b>=N||b<0return false;
        return true;
    }
    
    static void bfs(int a,int b) {
        char k = G[a][b];
        int[][] que = new int[10000][2];
        int[][] dic = {{1,0},{0,1},{-1,0},{0,-1}};
        int front=0,rear=0;
        que[rear][0]=a;que[rear++][1]=b;
        vis[a][b] = true;
        while(front!=rear) {
            int x=que[front][0],y=que[front++][1];
            for (int i=0;i<4;i++) {
                int nx=x+dic[i][0],ny=y+dic[i][1];
                if (check(nx,ny)==true) {
                    if (G[nx][ny]!=k) continue;
                    if (vis[nx][ny]==truecontinue;
                    que[rear][0]=nx;que[rear++][1]=ny;
                    vis[nx][ny]=true;
                }
            }
        }
    }
    static int Ans() {
        int h=0;
        for (int i=0;i<N;i++
            for (int j=0;j<N;j++
                if (vis[i][j]!=true) { 
                    bfs(i,j); h+=1;
                }
 
        return h;
    }
    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        G = new char[N][N];
        vis = new boolean[N][N];
        for (int i=0;i<N;i++) {
            String t = br.readLine();
            for (int j=0;j<N;j++) {
                G[i][j]=t.charAt(j);
            }
        }
        int aa =Ans();
        for (int i=0;i<N;i++)
            for(int j=0;j<N;j++) {
                if (G[i][j]=='G') G[i][j]='R';
            }
        vis = new boolean[N][N];
        System.out.println(aa+" "+Ans());
    }
 
}
 
cs

์•„์ง๋„ ์ค„์ผ ๋ฐฉ๋ฒ•์„ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค... ๋‚ด์ผ ๋‹ค์‹œ ํ•ด๋ด์•ผ๋ ๋“ฏ....