15721
문제
1012
접근
- 보통 이렇게 그래프 형식이라면 DFS, BFS이다.
- 인접한 배추의 그룹은 지렁이 갯수와 같다.
- 깊이우선과 너비우선 둘다 풀어도 될듯.
가정
- 깊이우선, 너비우선 모두 가능한 문제인거 같다.
- 너비우선으로 풀어본다.
- 전체 그래프를 저장한다.
- 배추의 위치를 저장한다.
풀어보기
package org.example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
private static int[][] graph;
private static boolean[][] visited;
private static int rows;
private static int cols;
private static final int[] dx = {-1, 1, 0, 0};
private static final int[] dy = {0, 0, -1, 1};
public static void bfs(int x, int y){
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{x, y});
visited[x][y] = true;
while(!queue.isEmpty()){
int[] position = queue.poll();
int curX = position[0];
int curY = position[1];
for(int i = 0; i < dx.length; i++){
int nextX = curX + dx[i];
int nextY = curY + dy[i];
if(returnTrueIfOutOfBound(nextX, nextY)){continue;}
if(graph[curX][curY] == 0){continue;}
if(!visited[nextX][nextY]){
visited[nextX][nextY] = true;
queue.offer(new int[]{nextX, nextY});
}
}
}
}
public static boolean returnTrueIfOutOfBound(int nextX, int nextY){
if(nextX < 0 ){
return true;
}
if(nextY < 0 ){
return true;
}
if(nextX >= rows) {
return true;
}
return nextY >= cols;
}
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int game = Integer.parseInt(reader.readLine());
for(int k = 0; k < game; k++){
String MNK = (reader.readLine());
String[] arrMNK = MNK.split(" ");
rows =Integer.parseInt(arrMNK[0]);
cols = Integer.parseInt(arrMNK[1]);
int wormCount = Integer.parseInt(arrMNK[2]);
graph = new int[rows][cols];
visited = new boolean[rows][cols];
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
graph[i][j] = 0;
}
}
for(int i = 0; i < wormCount; i++){
String[] arrXY = (reader.readLine()).split(" ");
int x = Integer.parseInt(arrXY[0]);
int y = Integer.parseInt(arrXY[1]);
graph[x][y] = 1;
}
int groupCount = 0;
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(graph[i][j] == 0){continue;}
if(visited[i][j]){continue;}
bfs(i, j);
groupCount++;
}
}
System.out.println(groupCount);
}
}
}
시행착오
참고자료
import java.util.*;
public class BFSExample {
private static int[][] graph;
private static boolean[][] visited;
private static int rows, cols;
private static final int[] dx = {-1, 1, 0, 0};
private static final int[] dy = {0, 0, -1, 1};
private static void bfs(int x, int y) {
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{x, y});
visited[x][y] = true;
while (!queue.isEmpty()) {
int[] pos = queue.poll();
int currX = pos[0];
int currY = pos[1];
for (int i = 0; i < 4; i++) {
int nx = currX + dx[i];
int ny = currY + dy[i];
if (nx >= 0 && nx < rows && ny >= 0 && ny < cols) {
if (graph[nx][ny] == 1 && !visited[nx][ny]) {
visited[nx][ny] = true;
queue.offer(new int[]{nx, ny});
}
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
rows = sc.nextInt();
cols = sc.nextInt();
graph = new int[rows][cols];
visited = new boolean[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
graph[i][j] = sc.nextInt();
}
}
int groupCount = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (graph[i][j] == 1 && !visited[i][j]) {
bfs(i, j);
groupCount++;
}
}
}
System.out.println(groupCount);
sc.close();
}
}