import java.io.*;
import java.util.*;
public class Main {
static int N;
static int[][] box;
static boolean[][] visited;
static int maxBombCnt = 1;
static int bombCnt;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
box = new int[N][N];
StringTokenizer st;
for(int i = 0; i < N ; i++){
st = new StringTokenizer(br.readLine());
for(int j = 0; j < N ; j++){
box[i][j] = Integer.parseInt(st.nextToken());
}
}
int allBombGroupCnt = 0;
visited = new boolean[N][N];
for(int i = 0; i < N; i++){
for(int j = 0 ; j < N ; j++){
bombCnt = 0;
if(visited[i][j]){continue;}
dfs(i,j,box[i][j]);
if(bombCnt>=4){
maxBombCnt = Math.max(maxBombCnt, bombCnt);
allBombGroupCnt+=1;
}
}
}
System.out.println(allBombGroupCnt+" "+maxBombCnt);
}
static int[][] way = {{-1,0},{0,1},{1,0},{0,-1}};
private static void dfs(int x, int y, int startValue){
visited[x][y] = true;
bombCnt += 1;
for(int w = 0; w < 4; w++){
int nx = x + way[w][0];
int ny = y + way[w][1];
if(nx<0 || nx>=N || ny<0 || ny>=N || visited[nx][ny])continue;
if(box[nx][ny]== startValue){
dfs(nx,ny,startValue);
}
}
}
}
자주 놓치는 부분이
(1) dfs 처음 진입할 때,
visited check를 하지않아서 bombCnt 수를 잘못 체크하는 경우
(2) dfs 내에서 다음 위치를 선택할 때,
- 현재 값이랑 같은 값인지 확인하는 로직을 안 넣는 경우
- 방문여부를 체크하지 않는 경우
https://www.codetree.ai/missions/2/problems/puyo-puyo?&utm_source=clipboard&utm_medium=text
[BJ] 1965: 상자넣기 (0) | 2023.12.12 |
---|---|
[BJ] 16987 : 계란으로 계란치기 (0) | 2023.12.12 |
[코드트리 챌린지] 5회차(10월 4일 ~ 10월 9일) : 사각형 채우기 3 (1) | 2023.10.09 |
[코드트리 챌린지] 문자열 - 문자열 선언하고 사용하기 (0) | 2023.09.18 |
[코드트리 챌린지] 실력체크 + 적절한 문제집 추천 (0) | 2023.09.01 |
댓글 영역