-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1861.java
More file actions
107 lines (88 loc) Β· 1.98 KB
/
Copy path1861.java
File metadata and controls
107 lines (88 loc) Β· 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
// C++ pairλ₯Ό ꡬνν¨.
class Pair{
Integer y;
Integer x;
public Pair(Integer y, Integer x) {
this.y = y;
this.x = x;
}
public Integer first() {
return y;
}
public Integer second() {
return x;
}
}
public class Main {
private static int N;
private static int[][] arr;
private static int idx;
private static int MAX;
private static int[] dy = {-1,1,0,0};
private static int[] dx = {0,0,-1,1};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int t=1; t<=T; t++) {
// init
N = sc.nextInt();
arr = new int[N][N];
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
arr[i][j] = sc.nextInt();
idx = Integer.MAX_VALUE;
MAX = Integer.MIN_VALUE;
// solve!
sol();
// print answer
System.out.println("#" + t + " " + idx + " " + MAX);
}
}
// λ¬Έμ νμ΄
private static void sol() {
for(int y=0; y<N; y++) {
for(int x=0; x<N; x++) {
// bfs call
int sum = bfs(y, x);
// compare
if(sum > MAX) {
MAX = sum;
idx = arr[y][x];
}
if(MAX==sum && idx > arr[y][x])
idx = arr[y][x];
}
}
}
// νμ
private static int bfs(int y, int x) {
Queue<Pair> q = new LinkedList<>();
boolean[][] visited = new boolean[N][N];
visited[y][x] = true;
q.add(new Pair(y, x));
int sum = 1;
while(!q.isEmpty()) {
y = q.peek().first();
x = q.peek().second();
q.poll();
for(int i=0; i<4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
// λ§΅ μμ΄κ³ , κ°λ³Έμ μκ³ , μ ννκ² νμ¬λ³΄λ€ 1 λ§νΌ λ ν΄λλ§ μ΄λ.
if(check(ny, nx) && !visited[ny][nx] && (arr[y][x]+1==arr[ny][nx])) {
sum++;
visited[ny][nx] = true;
q.add(new Pair(ny, nx));
}
}
}
return sum;
}
// μΈλ±μ€ λ²μ 체ν¬
private static boolean check(int y, int x) {
return y>=0 && y<N && x>=0 && x<N;
}
}