π¬ λ‘μ§μ λ¨Όμ μκ°νκ³ μμμ λ§κ² μ½λλ₯Ό μ§°λλ μ½κ² νλ Έμ΅λλ€.
maxCnt / cnt
, max / tmp
λΉκ΅import java.util.Scanner;
public class SWEA_1861_μ μ¬κ°νλ°© {
static int dx[] = {0, 0, 1, -1};
static int dy[] = {-1, 1, 0, 0};
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int t = 1; t <= T; t++) {
int n = sc.nextInt();
int room[][] = new int[n][n];
int tmp = 0; // νμ¬ μμΉμ κ°
int cnt = 0; // μ΄λν νμ
int maxCnt = 0; // μ΅λ μ΄λ νμ
int max = 0; // μ΅λλ‘ μ΄λν λ°©μ κ°
// λ°©μ κ°μ μ μ₯
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
room[i][j] = sc.nextInt();
}
}
// λͺ¨λ λ°©μ λ€λ¦°λ€.
for(int i = 0; i < n ; i++) {
for(int j = 0; j < n; j++) {
cnt = 0;
boolean flag = true;
// ν΄λΉ κ°μ tmpμ λ£λλ€.
tmp = room[i][j];
int si = i;
int sj = j;
d: while(flag) {
// 4λ°©νμν΄μ κ° κ³³ μλ 보기
c: for(int k = 0; k < 4; k++) {
int di = si + dx[k];
int dj = sj + dy[k];
if(dj < 0 || di < 0 || dj >= n || di >= n) {
if(k == 3) {
flag = false;
break d;
}
continue c;
}
// νμ¬ μμΉλ³΄λ€ +1 ν° κ° μλμ§ νμΈ
if(room[di][dj] == room[si][sj] + 1) {
si = di;
sj = dj;
cnt++;
break c;
}
if(k == 3) {
flag = false;
break d;
}
}
}
// λμ΄μ λμκ° μ μμ λ, maxCntμ Cntλ₯Ό λΉκ΅νλ€.
if(maxCnt > cnt) {
continue;
}
else if (maxCnt < cnt) {
maxCnt = cnt;
max = tmp;
}
else {
if(max > tmp) max = tmp;
}
}
}
// λ΅ μΆλ ₯
System.out.print("#" + t + " " + max + " ");
System.out.println(maxCnt + 1);
}
}
}