import java.io.*;
import java.util.*;
public class portal {
static int INF = (int) 1e9;
static int[] di = { 1, -1, 0, 0 };
static int[] dj = { 0, 0, 1, -1 };
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner();
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt(), m = sc.nextInt();
int[][] up = new int[n][m], down = new int[n][m], left = new int[n][m], right = new int[n][m];
int[][] dist = new int[n][m];
for (int[] x : dist)
Arrays.fill(x, INF);
char[][] grid = new char[n][m];
PriorityQueue<edge> pq = new PriorityQueue<>((x, y) -> x.c - y.c);
for (int i = 0; i < n; i++) {
grid[i] = sc.next().toCharArray();
for (int j = 0; j < m; j++)
if (grid[i][j] == 'C')
pq.add(new edge(i, j, dist[i][j] = 0));
}
for (int j = 0; j < m; j++) {
up[0][j] = 0;
down[n - 1][j] = n - 1;
}
for (int i = 0; i < n; i++) {
left[i][0] = 0;
right[i][m - 1] = m - 1;
}
for (int i = 1; i < n - 1; i++)
for (int j = 1; j < m - 1; j++) {
up[i][j] = grid[i][j] == '#' ? i : up[i - 1][j];
left[i][j] = grid[i][j] == '#' ? j : left[i][j - 1];
}
for (int i = n - 2; i > 0; i--)
for (int j = m - 2; j > 0; j--) {
down[i][j] = grid[i][j] == '#' ? i : down[i + 1][j];
right[i][j] = grid[i][j] == '#' ? j : right[i][j + 1];
}
int ans = -1;
while (!pq.isEmpty()) {
edge curr = pq.poll();
int i = curr.i, j = curr.j, c = curr.c;
if (grid[i][j] == 'F') {
ans = dist[i][j];
break;
}
if (c > dist[i][j])
continue;
for (int k = 0; k < 4; k++) {
int i2 = i + di[k], j2 = j + dj[k];
if (grid[i2][j2] != '#' && c + 1 < dist[i2][j2])
pq.add(new edge(i2, j2, dist[i2][j2] = c + 1));
}
int min = INF;
// up
int i2 = up[i][j] + 1, j2 = j;
min = Math.min(min, Math.abs(i - i2));
// down
i2 = down[i][j] - 1;
min = Math.min(min, Math.abs(i - i2));
// left
i2 = i;
j2 = left[i][j] + 1;
min = Math.min(min, Math.abs(j - j2));
// right
j2 = right[i][j] - 1;
min = Math.min(min, Math.abs(j - j2));
min++;
// up
i2 = up[i][j] + 1;
j2 = j;
if (c + min < dist[i2][j2])
pq.add(new edge(i2, j2, dist[i2][j2] = c + min));
// down
i2 = down[i][j] - 1;
if (c + min < dist[i2][j2])
pq.add(new edge(i2, j2, dist[i2][j2] = c + min));
// left
i2 = i;
j2 = left[i][j] + 1;
if (c + min < dist[i2][j2])
pq.add(new edge(i2, j2, dist[i2][j2] = c + min));
// right
j2 = right[i][j] - 1;
if (c + min < dist[i2][j2])
pq.add(new edge(i2, j2, dist[i2][j2] = c + min));
}
out.println(ans == -1 ? "nemoguce" : ans);
out.close();
}
static class edge {
int i, j, c;
edge(int a, int b, int C) {
i = a;
j = b;
c = C;
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
Scanner(String fileName) throws FileNotFoundException {
br = new BufferedReader(new FileReader(fileName));
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
String nextLine() throws IOException {
return br.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(next());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(next());
}
boolean ready() throws IOException {
return br.ready();
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
219 ms |
15736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
210 ms |
15660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
208 ms |
15600 KB |
Output is correct |
2 |
Correct |
213 ms |
15120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
216 ms |
15288 KB |
Output is correct |
2 |
Correct |
217 ms |
15444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
209 ms |
15376 KB |
Output is correct |
2 |
Correct |
218 ms |
15340 KB |
Output is correct |
3 |
Correct |
224 ms |
15664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
232 ms |
16280 KB |
Output is correct |
2 |
Correct |
221 ms |
16012 KB |
Output is correct |
3 |
Correct |
275 ms |
17052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
309 ms |
17636 KB |
Output is correct |
2 |
Correct |
243 ms |
16668 KB |
Output is correct |
3 |
Correct |
389 ms |
16824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
459 ms |
24368 KB |
Output is correct |
2 |
Correct |
366 ms |
21000 KB |
Output is correct |
3 |
Correct |
378 ms |
21896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
493 ms |
28144 KB |
Output is correct |
2 |
Correct |
338 ms |
22500 KB |
Output is correct |
3 |
Correct |
431 ms |
24692 KB |
Output is correct |
4 |
Correct |
411 ms |
21964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
520 ms |
30508 KB |
Output is correct |
2 |
Correct |
311 ms |
23500 KB |
Output is correct |
3 |
Correct |
492 ms |
29032 KB |
Output is correct |
4 |
Correct |
447 ms |
28976 KB |
Output is correct |