문제
코레스코 콘도미니엄 8층은 학생들이 3끼의 식사를 해결하는 공간이다. 그러나 몇몇 비양심적인 학생들의 만행으로 음식물이 통로 중간 중간에 떨어져 있다. 이러한 음식물들은 근처에 있는 것끼리 뭉치게 돼서 큰 음식물 쓰레기가 된다.
이 문제를 출제한 선생님은 개인적으로 이러한 음식물을 실내화에 묻히는 것을 정말 진정으로 싫어한다. 참고로 우리가 구해야 할 답은 이 문제를 낸 조교를 맞추는 것이 아니다.
통로에 떨어진 음식물을 피해가기란 쉬운 일이 아니다. 따라서 선생님은 떨어진 음식물 중에 제일 큰 음식물만은 피해 가려고 한다.
선생님을 도와 제일 큰 음식물의 크기를 구해서 “10ra"를 외치지 않게 도와주자.
입력
첫째 줄에 통로의 세로 길이 N(1 ≤ N ≤ 100)과 가로 길이 M(1 ≤ M ≤ 100) 그리고 음식물 쓰레기의 개수 K(1 ≤ K ≤ N×M)이 주어진다. 그리고 다음 K개의 줄에 음식물이 떨어진 좌표 (r, c)가 주어진다.
좌표 (r, c)의 r은 위에서부터, c는 왼쪽에서부터가 기준이다. 입력으로 주어지는 좌표는 중복되지 않는다.
출력
첫째 줄에 음식물 중 가장 큰 음식물의 크기를 출력하라.
DFS
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int[] dx = {0,1,0,-1};
static int[] dy = {1,0,-1,0};
static int col, row, trash_cnt;
static int max,count = 0;
static int[][] arr;
static boolean[][] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
col = Integer.parseInt(st.nextToken());
row = Integer.parseInt(st.nextToken());
trash_cnt = Integer.parseInt(st.nextToken());
arr = new int[col][row];
visited = new boolean[col][row];
for(int i = 0; i < trash_cnt; i++){
st = new StringTokenizer(br.readLine(), " ");
int x = Integer.parseInt(st.nextToken())-1;
int y = Integer.parseInt(st.nextToken())-1;
arr[x][y] = 1;
}
for(int i = 0; i < col; i++){
for(int j = 0; j < row; j++){
if(visited[i][j] == false && arr[i][j] == 1){
count = 0;
dfs(i,j);
if(count > max) max = count;
}
}
}
System.out.println(max);
}
public static void dfs(int x, int y){
visited[x][y] = true;
count++;
for(int i = 0; i < 4; i++){
int next_x = x + dx[i];
int next_y = y + dy[i];
if(is_vaild_range(next_x, next_y)){
if(visited[next_x][next_y] == false && arr[next_x][next_y] == 1){
dfs(next_x,next_y);
}
}
}
}
public static boolean is_vaild_range(int x, int y){
return (x >= 0 && x < col && y >= 0 && y < row);
}
}
BFS
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int[] dx = {0,1,0,-1};
static int[] dy = {1,0,-1,0};
static int col, row, trash_cnt;
static int max,count = 0;
static int[][] arr;
static boolean[][] visited;
static Queue<int[]> que = new LinkedList<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
col = Integer.parseInt(st.nextToken());
row = Integer.parseInt(st.nextToken());
trash_cnt = Integer.parseInt(st.nextToken());
arr = new int[col][row];
visited = new boolean[col][row];
for(int i = 0; i < trash_cnt; i++){
st = new StringTokenizer(br.readLine(), " ");
int x = Integer.parseInt(st.nextToken())-1;
int y = Integer.parseInt(st.nextToken())-1;
arr[x][y] = 1;
}
for(int i = 0; i < col; i++){
for(int j = 0; j < row; j++){
if(visited[i][j] == false && arr[i][j] == 1){
count = 0;
bfs(i,j);
if(count > max) max = count;
}
}
}
System.out.println(max);
}
public static void bfs(int x, int y){
visited[x][y] = true;
que.offer(new int[]{x,y});
count++;
while(!que.isEmpty()){
int[] curr_idx = que.poll();
for(int i = 0; i < 4; i++){
int next_x = curr_idx[0] + dx[i];
int next_y = curr_idx[1] + dy[i];
if(is_vaild_range(next_x, next_y)){
if(visited[next_x][next_y] == false && arr[next_x][next_y] == 1){
visited[next_x][next_y] = true;
que.offer(new int[]{next_x,next_y});
System.out.println("x = " +next_x+" , y = "+next_y);
count++;
}
}
}
}
}
public static boolean is_vaild_range(int x, int y){
return (x >= 0 && x < col && y >= 0 && y < row);
}
}
https://www.acmicpc.net/problem/1743
2583번: 영역 구하기
첫째 줄에 M과 N, 그리고 K가 빈칸을 사이에 두고 차례로 주어진다. M, N, K는 모두 100 이하의 자연수이다. 둘째 줄부터 K개의 줄에는 한 줄에 하나씩 직사각형의 왼쪽 아래 꼭짓점의 x, y좌표값과 오
www.acmicpc.net
[백준] 1697 숨바꼭질 (JAVA) (0) | 2023.07.31 |
---|---|
[백준] 2583 영역 구하기 (JAVA) (0) | 2023.07.28 |
[백준] 2667 단지번호붙이기 / 그래프 탐색 - DFS, BFS (0) | 2023.07.28 |
[백준] 1182 부분수열의 합 (JAVA) (0) | 2023.07.27 |
댓글,
ella_travel