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<0) return false;
if (b>=N||b<0) return 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]==true) continue;
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<0) return false;
if (b>=N||b<0) return 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]==true) continue;
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 |
์์ง๋ ์ค์ผ ๋ฐฉ๋ฒ์ ์ ๋ชจ๋ฅด๊ฒ ๋ค... ๋ด์ผ ๋ค์ ํด๋ด์ผ๋ ๋ฏ....
'Coding > ๋ฐฑ์ค ๋ฌธ์ ํ์ด' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| (JAVA) ๋ฐฑ์ค 3197๋ฒ : ๋ฐฑ์กฐ์ ํธ์ (0) | 2024.10.07 |
|---|---|
| (JAVA) ๋ฐฑ์ค 3085๋ฒ : ์ฌํ ๊ฒ์ (0) | 2024.03.12 |
| (ํ์ด์ฌ) ๋ฐฑ์ค 2839๋ฒ : ์คํ ๋ฐฐ๋ฌ (0) | 2022.07.21 |
| (ํ์ด์ฌ) ๋ฐฑ์ค 2775๋ฒ : ๋ถ๋ ํ์ฅ์ด ๋ ํ ์ผ (0) | 2022.07.06 |
| (ํ์ด์ฌ) ๋ฐฑ์ค 10250๋ฒ: ACMํธํ (0) | 2022.07.06 |
Comment