https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5LtJYKDzsDFAXc
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.util.Scanner;
public class Solution {
private static int n;
private static int[][] room;
private static int[][] answer;
private static int[] di = {0, 1, 0, -1};
private static int[] dj = {1, 0, -1, 0};
private static int count;
public static int find (int i, int j) {
boolean finded = true;
for (int k = 0; k < 4; k++) {
if (i+di[k] >= 0 && i+di[k] < n && j+dj[k] >= 0 && j+dj[k] < n && room[i+di[k]][j+dj[k]] == room[i][j] + 1) {
count++;
i += di[k];
j += dj[k];
break;
}
else if (k == 3)
finded = false;
}
if (finded != false)
find(i, j);
return count;
}
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
for (int t = 1; t <= T; t++) {
n = scanner.nextInt();
room = new int[n][n];
answer = new int[n][n];
for (int i = 0; i < n; i++) { // 방 번호 입력
for (int j = 0; j < n; j++)
room[i][j] = scanner.nextInt();
}
for (int i = 0; i < n; i++) { // 방마다 최대 이동 개수 찾기
for (int j = 0; j < n; j++) {
count = 1;
answer[i][j] = find(i, j);
}
}
int max = 0;
int roomNum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (answer[i][j] >= max) {
if (answer[i][j] == max)
roomNum = Math.min(roomNum, room[i][j]);
else
roomNum = room[i][j];
max = answer[i][j];
}
}
}
System.out.println("#" + t + " " + roomNum + " " + max);
}
}
}
'Algorithm > 백준+프로그래머스+SWEA+정올+구름' 카테고리의 다른 글
[Algorithm] 백준 2751 수 정렬하기 2 (0) | 2021.08.08 |
---|---|
[Algorithm] SWEA 3499 퍼펙트 셔플 (0) | 2021.08.07 |
[Algorithm] SWEA 1940 가랏! RC카! (0) | 2021.08.05 |
[Algorithm] SWEA 1225 암호생성기 (0) | 2021.08.05 |
[Algorithm] SWEA 1218 괄호짝짓기 (0) | 2021.08.05 |