-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw2105.java
More file actions
68 lines (56 loc) · 1.22 KB
/
sw2105.java
File metadata and controls
68 lines (56 loc) · 1.22 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
import java.util.Scanner;
public class sw2105 {
static int dx[] = {-1,1,1,-1};
static int dy[] = {1,1,-1,-1};
static int arr[][] = new int[20][20];
static int n;
static int start_x;
static int start_y;
static int answer = -1;
static boolean visit[]= new boolean[101];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int test =sc.nextInt();
for(int t=1; t<=test; t++) {
n= sc.nextInt();
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
arr[i][j]= sc.nextInt();
}
}
answer = -1;
for(int k=1; k<=100; k++) {
visit[k]=false;
}
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(i==2 && j==0) {
System.out.println();
}
start_x = i;
start_y = j;
cafe(i,j, 0, 0);
}
}
System.out.println("#" + t + " " + answer);
}
}
static void cafe(int x, int y, int d, int c) {
if(x<0 || y < 0 || x >= n || y >= n ) return;
if(visit[arr[x][y]]) {
if(start_x == x && start_y == y) {
if(answer < c)
answer = c;
}
return;
}
visit[arr[x][y]]=true;
int nx = x+dx[d];
int ny = y+dy[d];
if(d+1<=3) {
cafe(nx,ny,d+1,c+1);
}
cafe(nx,ny,d,c+1);
visit[arr[x][y]]=false;
}
}