Skip to content

Commit a6fa632

Browse files
committed
[Silver I] Title: 그림, Time: 424 ms, Memory: 43332 KB -BaekjoonHub
1 parent e0f9632 commit a6fa632

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [Silver I] 그림 - 1926
2+
3+
[문제 링크](https://www.acmicpc.net/problem/1926)
4+
5+
### 성능 요약
6+
7+
메모리: 43332 KB, 시간: 424 ms
8+
9+
### 분류
10+
11+
너비 우선 탐색, 깊이 우선 탐색, 그래프 이론, 그래프 탐색
12+
13+
### 제출 일자
14+
15+
2024년 12월 27일 11:20:36
16+
17+
### 문제 설명
18+
19+
<p>어떤 큰 도화지에 그림이 그려져 있을 때, 그 그림의 개수와, 그 그림 중 넓이가 가장 넓은 것의 넓이를 출력하여라. 단, 그림이라는 것은 1로 연결된 것을 한 그림이라고 정의하자. 가로나 세로로 연결된 것은 연결이 된 것이고 대각선으로 연결이 된 것은 떨어진 그림이다. 그림의 넓이란 그림에 포함된 1의 개수이다.</p>
20+
21+
### 입력
22+
23+
<p>첫째 줄에 도화지의 세로 크기 n(1 ≤ n ≤ 500)과 가로 크기 m(1 ≤ m ≤ 500)이 차례로 주어진다. 두 번째 줄부터 n+1 줄 까지 그림의 정보가 주어진다. (단 그림의 정보는 0과 1이 공백을 두고 주어지며, 0은 색칠이 안된 부분, 1은 색칠이 된 부분을 의미한다)</p>
24+
25+
### 출력
26+
27+
<p>첫째 줄에는 그림의 개수, 둘째 줄에는 그 중 가장 넓은 그림의 넓이를 출력하여라. 단, 그림이 하나도 없는 경우에는 가장 넓은 그림의 넓이는 0이다.</p>
28+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
5+
public class Main {
6+
static boolean visited[][];
7+
static int arr[][];
8+
static int N,M,answer;
9+
static int dx[] = {-1,1,0,0};
10+
static int dy[] = {0,0,-1,1};
11+
12+
public static void main(String[] args) throws IOException {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
17+
N = Integer.parseInt(st.nextToken());
18+
M = Integer.parseInt(st.nextToken());
19+
20+
arr = new int[N][M];
21+
22+
for(int i =0; i<N; i++) {
23+
st = new StringTokenizer(br.readLine());
24+
for(int j = 0; j<M; j++) {
25+
arr[i][j] = Integer.parseInt(st.nextToken());
26+
}
27+
}
28+
visited = new boolean[N][M];
29+
int countAnswer = 0;
30+
answer = 0;
31+
for(int i =0; i<N; i++) {
32+
for(int j = 0; j<M; j++) {
33+
if(arr[i][j] == 1 && visited[i][j] == false) {
34+
countAnswer++;
35+
bfs(i,j);
36+
}
37+
}
38+
}
39+
System.out.println(countAnswer);
40+
System.out.println(answer);
41+
42+
}
43+
44+
static void bfs(int x, int y) {
45+
46+
Queue<int[] > queue = new LinkedList<>();
47+
queue.add(new int[] {x,y});
48+
visited[x][y] = true;
49+
int count =1;
50+
51+
while(!queue.isEmpty()) {
52+
53+
int now[] = queue.poll();
54+
int nowx = now[0];
55+
int nowy = now[1];
56+
for(int i =0; i<4; i++) {
57+
int nx = nowx + dx[i];
58+
int ny = nowy + dy[i];
59+
60+
if(nx >=0 && nx<N && ny >=0 && ny <M) {
61+
if(visited[nx][ny]==false && arr[nx][ny]== 1) {
62+
count++;
63+
queue.add(new int[] { nx,ny});
64+
visited[nx][ny] = true;
65+
}
66+
}
67+
}
68+
}
69+
70+
71+
answer = Math.max(answer,count);
72+
}
73+
}
74+

0 commit comments

Comments
 (0)