-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnlineJudge.java
More file actions
81 lines (68 loc) · 2.43 KB
/
OnlineJudge.java
File metadata and controls
81 lines (68 loc) · 2.43 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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
class Pair {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
public class OnlineJudge {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String args[]) throws IOException {
run();
bw.flush();
br.close();
bw.close();
}
static int[] moveX = new int[] { 1, -1, 0, 0 };
static int[] moveY = new int[] { 0, 0, 1, -1 };
public static void run() throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[][] arr = new int[m + 1][n + 1];
for (int i = 0; i < m; i++) {
String input = br.readLine();
for (int j = 0; j < n; j++)
arr[i + 1][j + 1] = input.charAt(j) - '0';
}
Queue zeroQ = new LinkedList();
Queue oneQ = new LinkedList();
zeroQ.offer(new Pair(1, 1));
int[][] dist = new int[m + 1][n + 1];
boolean[][] c = new boolean[m + 1][n + 1];
while (!zeroQ.isEmpty() || !oneQ.isEmpty()) {
Pair now = zeroQ.isEmpty() ? (Pair) oneQ.poll() : (Pair) zeroQ.poll();
if (c[m][n]) {
break;
}
for (int i = 0; i < 4; i++) {
int nextX = now.x + moveX[i];
int nextY = now.y + moveY[i];
if (nextX > 0 && nextY > 0 && nextX <= m && nextY <= n) {
Pair p = new Pair(nextX, nextY);
if (!c[nextX][nextY]) {
c[nextX][nextY] = true;
if (arr[nextX][nextY] == 1) {
dist[nextX][nextY] = dist[now.x][now.y] + 1;
oneQ.offer(p);
} else {
dist[nextX][nextY] = dist[now.x][now.y];
zeroQ.offer(p);
}
}
}
}
}
bw.write(dist[m][n] + "\n");
}
}