-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1210.java
More file actions
74 lines (61 loc) Β· 1.73 KB
/
Copy path1210.java
File metadata and controls
74 lines (61 loc) Β· 1.73 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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
private static int[] dy = {0,0,-1};
private static int[] dx = {-1,1,0};
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
//νμ 10κ°μ ν
μ€νΈμΌμ΄μ€
for(int t=0; t<10; t++) {
int T = Integer.parseInt(bf.readLine().trim());
int[][] arr = new int[100][100];
int[][] visited = new int[100][100];
// λ°μμ λΆν° κ±°μ¬λ¬ μ¬λΌκ°μ!
int start_y=0, start_x=0;
for(int i=0; i<100; i++) {
String[] str = bf.readLine().split(" ");
for(int j=0; j<100; j++) {
arr[i][j] = Integer.parseInt(str[j]);
// κ±°μ¬λ¬ μ¬λΌκ°λ μ§μ μ μ₯
if(arr[i][j] == 2) {
start_y = i;
start_x = j;
}
}
}
// go!
int ans = go(arr, visited, start_y, start_x);
System.out.println("#" + T + " " + ans);
}
}
private static int go(int[][] arr, int[][] visited, int y, int x) {
// check-in
visited[y][x] = 1;
while(true) {
// μΌμͺ½ -> μ€λ₯Έμͺ½ -> μ
for(int dir=0; dir<3; dir++) {
int ny = y + dy[dir];
int nx = x + dx[dir];
// λμ°©
if(ny == 0)
return nx;
// λ²μ λ΄ & κΈΈμ΄ μμ & λ°©λ¬Ένμ μμ
if(check(ny, nx) && arr[ny][nx]!=0 && visited[ny][nx]==0) {
y = ny;
x = nx;
visited[ny][nx] = 1;
break; // μ€μ!
}
}
}
}
// λ²μ 체ν¬
private static boolean check(int y, int x) {
return y>=0 && y<100 && x>=0 && x<100;
}
}