-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
129 lines (107 loc) Β· 3.71 KB
/
Main.java
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package Simulation.P2931;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int R, C;
static char[][] map;
static Position M, Z;
static boolean[][] visited;
static int visited_cnt;
static int[] di = {-1, 0, 1, 0};
static int[] dj = {0, 1, 0, -1};
static int ans_i, ans_j;
static int[] ans_d = new int[4];
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Simulation/P2931/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
R = Integer.parseInt(st.nextToken());
C = Integer.parseInt(st.nextToken());
map = new char[R][C];
visited = new boolean[R][C];
for (int i = 0; i < R; i++) {
String line = br.readLine();
for (int j = 0; j < C; j++) {
map[i][j] = line.charAt(j);
if (map[i][j] == 'M') M = new Position(i, j, -1);
else if (map[i][j] == 'Z') Z = new Position(i, j, -1);
if (map[i][j] != '.') visited_cnt++;
}
}
search(M);
search(Z);
System.out.println(++ans_i + " " + ++ans_j + " " + getBlock());
}
static char getBlock() {
if (visited_cnt > 0) return '+';
if (ans_d[1] == 1 && ans_d[2] == 1) return '1';
else if (ans_d[0] == 1 && ans_d[1] == 1) return '2';
else if (ans_d[0] == 1 && ans_d[3] == 1) return '3';
else if (ans_d[2] == 1 && ans_d[3] == 1) return '4';
else if (ans_d[0] == 1 || ans_d[2] == 1) return '|';
else return '-';
}
static void search(Position p) {
if (!visited[p.i][p.j]) {
visited[p.i][p.j] = true;
visited_cnt --;
}
if (p.d == -1) {
for (int d = 0; d < 4; d++) {
Position to = move(p, d);
if (to != null) search(to);
}
}
int to_d = getMoveDirection(p, map[p.i][p.j]);
Position to = move(p, to_d);
if (to != null) search(to);
}
static Position move(Position p, int to_d) {
if (to_d == -1) return null;
int to_i = p.i + di[to_d];
int to_j = p.j + dj[to_d];
if (!isValidPath(to_i, to_j)) return null;
if (map[to_i][to_j] == '.' && map[p.i][p.j] != 'M' && map[p.i][p.j] != 'Z') {
if (p.d != -1) {
ans_i = to_i;
ans_j = to_j;
ans_d[(to_d+2)%4] ++;
} else if (to_i == ans_i && to_j == ans_j) {
ans_d[(to_d+2)%4] ++;
}
return null;
}
return new Position(to_i, to_j, to_d);
}
static int getMoveDirection(Position p, char block) {
if (block == '|' || block == '-' || block == '+') {
return p.d;
} else if (block == '1') {
if (p.d == 3) return 2;
else if (p.d == 0) return 1;
} else if (block == '2') {
if (p.d == 2) return 1;
else if (p.d == 3) return 0;
} else if (block == '3') {
if (p.d == 1) return 0;
else if (p.d == 2) return 3;
} else if (block == '4') {
if (p.d == 1) return 2;
else if (p.d == 0) return 3;
}
return -1;
}
static boolean isValidPath(int i, int j) {
return 0 <= i && i < R && 0 <= j && j < C;
}
static class Position {
int i, j, d;
public Position(int i, int j, int d) {
this.i = i;
this.j = j;
this.d = d;
}
}
}