-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCCC'08S3.java
More file actions
79 lines (73 loc) · 2.5 KB
/
Copy pathCCC'08S3.java
File metadata and controls
79 lines (73 loc) · 2.5 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
import java.util.*;
import java.io.*;
public class homework {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
public static void main(String[] args) throws IOException {
int T = readInt();
int[][] mh = {{0, 1}, {0, -1}};
int[][] mv = {{1, 0}, {-1, 0}};
for (int t = 0; t < T; t++) {
int N = readInt(), M = readInt();
int[][] dist = new int[N+2][M+2];
char[][] grid = new char[N+2][M+2];
for (int i = 1; i <= N; i++) {
String s = next();
for (int j = 1; j <= M; j++) {
grid[i][j] = s.charAt(j-1);
dist[i][j] = -1;
}
}
Queue<Pair> q = new LinkedList<>();
q.add(new Pair(1, 1));
dist[1][1] = 1;
while (!q.isEmpty()) {
Pair cur = q.poll();
if (grid[cur.x][cur.y] == '+' || grid[cur.x][cur.y] == '-') {
for (int[] m: mh) {
if (dist[cur.x + m[0]][cur.y + m[1]] == -1 && grid[cur.x + m[0]][cur.y + m[1]] != '*') {
q.add(new Pair(cur.x + m[0], cur.y + m[1]));
dist[cur.x + m[0]][cur.y + m[1]] = dist[cur.x][cur.y] + 1;
}
}
}
if (grid[cur.x][cur.y] == '+' || grid[cur.x][cur.y] == '|') {
for (int[] m: mv) {
if (dist[cur.x + m[0]][cur.y + m[1]] == -1 && grid[cur.x + m[0]][cur.y + m[1]] != '*') {
q.add(new Pair(cur.x + m[0], cur.y + m[1]));
dist[cur.x + m[0]][cur.y + m[1]] = dist[cur.x][cur.y] + 1;
}
}
}
}
System.out.println(dist[N][M]);
}
}
static class Pair {
int x, y;
Pair (int x, int y) {
this.x = x;
this.y = y;
}
}
static String next () throws IOException {
while (st == null || ! st.hasMoreTokens())
st = new StringTokenizer(br.readLine().trim());
return st.nextToken();
}
static long readLong () throws IOException {
return Long.parseLong(next());
}
static int readInt () throws IOException {
return Integer.parseInt(next());
}
static double readDouble () throws IOException {
return Double.parseDouble(next());
}
static char readCharacter () throws IOException {
return next().charAt(0);
}
static String readLine () throws IOException {
return br.readLine().trim();
}
}