슈도코드
n,m 입력받기
count =0
Array[][] arr = new Array[n][m]
Array[][] visited = new Array[n][m]
for(i : n만큼 반복) {
for(j : m만큼 반복) {
arr[i][j] = 입력받는 수 대입
}
}
for(i : n만큼 반복) {
for(j : m만큼 반복) {
if(!visited[n][m] && arr[n][m] == 1) {
count++
int max = DFS(n,m)
}
}
count(그림개수) 출력
max(가장 큰 그림 넓이) 출력
}
DFS(n,m){
int max = 1
if(visited[n][m]) return
visited[n][m] = true
for(i : 4만큼 반복) {
int dy = {0,1,0,-1}
int dx = {1,0,-1,0}
int nextX = x + dx[i]
int nextY = y + dy[i]
if( 0<= nextX <= m && 0<=nextY<=m && arr[nextX][nextY] == 1 && visited[nextX][nextY] ==false) {
DFS(nextX, nextY)
max++
}
}
return max
}
Troubleshooting 1
package DFS;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.IOException;
public class paint1926 {
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());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int maxArea =0;
int paintCount =0;
arr = new int[n][m];
visited = new boolean[n][m];
for(int i =0; i<n+1; i++) {
st = new StringTokenizer(br.readLine());
for(int j =0; j<m+1; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
for(int i =0; i<n+1; i++) {
for(int j =0; j<m+1; j++) {
if(!visited[n][m] && arr[n][m] == 1) {
paintCount++;
maxArea = DFS(n,m);
}
}
System.out.println(paintCount);
System.out.println(maxArea);
}
}
public static int DFS(int n,int m) {
int maxArea = 1;
if(visited[n][m]) return 0;
visited[n][m] = true;
for(int i=0; i<4; i++) {
int[] dn = {0,1,0,-1};
int[] dm = {1,0,-1,0};
int nextM = n + dm[i];
int nextN = m + dn[i];
if( 0<= nextM && nextM <= m && 0<= nextN && nextN <=m && arr[nextM][nextN] == 1 && visited[nextM][nextN] ==false) {
DFS(nextM, nextN);
maxArea++;
}
}
return maxArea;
}
}
Troubleshooting 2
package DFS;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.IOException;
public class paint1926 {
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());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int maxArea =0;
int paintCount =0;
arr = new int[n][m];
visited = new boolean[n][m];
for(int i =0; i<n; i++) {
st = new StringTokenizer(br.readLine());
for(int j =0; j<m; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
for(int i =0; i<n; i++) {
for(int j =0; j<m; j++) {
if(!visited[i][j] && arr[i][j] == 1) {
paintCount++;
maxArea = DFS(i,j);
}
}
}
System.out.println(paintCount);
System.out.println(maxArea);
}
public static int DFS(int n,int m) {
int maxArea = 1;
if(visited[n][m]) return 0;
visited[n][m] = true;
for(int i=0; i<4; i++) {
int[] dn = {0,1,0,-1};
int[] dm = {1,0,-1,0};
int nextM = n + dm[i];
int nextN = m + dn[i];
if( 0<= nextM && nextM <= m && 0<= nextN && nextN <=m && arr[nextM][nextN] == 1 && visited[nextM][nextN] ==false) {
DFS(nextM, nextN);
maxArea++;
}
}
return maxArea;
}
}