반응형
문제
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
아이디어
1. (0,0) -> (0,1) -> ... -> (N,N)순으로 탐방한다.
2. int[][] Room
3. LinkedList<int[]> Legngth -> 길이 내림차순(길이가 같으면 방 오름차순 = 가장 작은수가 적힌 방부터)
4. 탐색이 모두 끝나면 Length 리스트의 맨 앞을 출력.
코드
import java.io.*;
import java.util.*;
public class Solution {
// 방을 만든다.
static int[][] Room;
static int[] di = { -1, 1, 0, 0 };
static int[] dj = { 0, 0, -1, 1 };
static int N;
static LinkedList<int[]> Length = new LinkedList<int[]>(); // 방 숫자[0], 길이[1]
static int cnt = 1;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
// 테스트 케이스 수를 입력받는다.
int T = Int(br.readLine());
// 테스트 케이스 수 만큼 입력받는다.
for (int tc = 1; tc <= T; ++tc) {
// 방의 사이즈를 입력받는다.
N = Int(br.readLine());
Room = new int[N][N];
// 각 방의 숫자를 입력받는다.
for (int i = 0; i < N; ++i) {
st = new StringTokenizer(br.readLine(), " ");
Room[i] = new int[N];
for (int j = 0; j < N; ++j) {
Room[i][j] = Int(st.nextToken());
}
}
// 탐방한다.
for(int i=0; i<N; ++i) {
for(int j=0; j<N; ++j) {
cnt = 1;
Search(i,j);
Length.offer(new int[] {Room[i][j],cnt});
}
}
// 정렬한다.
Collections.sort(Length, (o1,o2)-> {
if(o2[1]==o1[1])
return Integer.compare(o1[0], o2[0]);
return Integer.compare(o2[1], o1[1]);
});
// 출력한다.
System.out.println("#"+tc+" "+Length.peek()[0]+" "+Length.peek()[1]);
Length.clear();
}
}
static void Search(int x, int y) {
// 4방 탐색을 한다.
for (int d = 0; d < 4; ++d) {
int ni = x + di[d];
int nj = y + dj[d];
if(isBoundary(ni,nj) && Room[ni][nj]-Room[x][y]==1) {
Search(ni,nj);
cnt++;
}
}
}
static boolean isBoundary(int x, int y) {
if (x >= 0 && x < N && y >= 0 && y < N)
return true;
return false;
}
static int Int(String s) {
return Integer.parseInt(s);
}
}
cpdm
choppadontbiteme.tistory.com
반응형
'Algorithm > SWEA' 카테고리의 다른 글
[ SWEA ] SWEA 1210 Ladder 1 D4 JAVA (0) | 2022.09.29 |
---|---|
[ SWEA ] 1218 괄호짝짓기 D4 JAVA (0) | 2022.09.29 |
[ SWEA ] 1223. 계산기 2 D4 JAVA (0) | 2022.09.29 |
[ SWEA ] 10726. 이진수 표현 D3 C++ (0) | 2022.09.06 |
[ SWEA ] 1221. GNS D3 C++ (0) | 2022.08.30 |