|
| 1 | +// Cliff Climbing |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class Main { |
| 5 | + public static void main(String[] args) { |
| 6 | + Scanner sc = new Scanner(System.in); |
| 7 | + while (true) { |
| 8 | + int w = sc.nextInt(), h = sc.nextInt(); |
| 9 | + if ((w | h) == 0) |
| 10 | + break; |
| 11 | + int[][] cliff = new int[h][w]; |
| 12 | + int[][][] costs = new int[2][h][w]; |
| 13 | + for (int i = 0; i < h; i++) { |
| 14 | + Arrays.fill(cliff[i], -1); |
| 15 | + Arrays.fill(costs[0][i], -1); |
| 16 | + Arrays.fill(costs[1][i], -1); |
| 17 | + } |
| 18 | + List<Integer> gx = new ArrayList<Integer>(); |
| 19 | + List<Integer> gy = new ArrayList<Integer>(); |
| 20 | + PriorityQueue<Node> queue = new PriorityQueue<Node>(); |
| 21 | + for (int i = 0; i < h; i++) |
| 22 | + for (int j = 0; j < w; j++) { |
| 23 | + char c = sc.next().charAt(0); |
| 24 | + if (Character.isDigit(c)) |
| 25 | + cliff[i][j] = c - '0'; |
| 26 | + else if (c == 'S') { |
| 27 | + cliff[i][j] = 0; |
| 28 | + queue.add(new Node(j, i, 0, 'R')); |
| 29 | + queue.add(new Node(j, i, 0, 'L')); |
| 30 | + } else if (c == 'T') { |
| 31 | + cliff[i][j] = 0; |
| 32 | + gx.add(j); |
| 33 | + gy.add(i); |
| 34 | + } else if (c == 'X') |
| 35 | + cliff[i][j] = -1; |
| 36 | + } |
| 37 | + while (!queue.isEmpty()) { |
| 38 | + Node node = queue.poll(); |
| 39 | + int tx = node.x; |
| 40 | + int ty = node.y; |
| 41 | + int tc = node.cost; |
| 42 | + char tf = node.foot; |
| 43 | + if (tf == 'R') { |
| 44 | + if (costs[0][ty][tx] >= 0 && costs[0][ty][tx] <= tc) |
| 45 | + continue; |
| 46 | + costs[0][ty][tx] = tc; |
| 47 | + for (int lx = -3; lx <= -1; lx++) |
| 48 | + for (int ly = -3 - lx; ly <= 3 + lx; ly++) { |
| 49 | + int x = tx + lx; |
| 50 | + int y = ty + ly; |
| 51 | + if (x >= 0 && x < w && y >= 0 && y < h |
| 52 | + && cliff[y][x] >= 0) |
| 53 | + queue.add(new Node(x, y, tc + cliff[y][x], 'L')); |
| 54 | + } |
| 55 | + } else { |
| 56 | + if (costs[1][ty][tx] >= 0 && costs[1][ty][tx] <= tc) |
| 57 | + continue; |
| 58 | + costs[1][ty][tx] = tc; |
| 59 | + for (int rx = 1; rx <= 3; rx++) |
| 60 | + for (int ry = -3 + rx; ry <= 3 - rx; ry++) { |
| 61 | + int x = tx + rx; |
| 62 | + int y = ty + ry; |
| 63 | + if (x >= 0 && x < w && y >= 0 && y < h |
| 64 | + && cliff[y][x] >= 0) |
| 65 | + queue.add(new Node(x, y, tc + cliff[y][x], 'R')); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + int result = -1; |
| 70 | + for (int i : gy) |
| 71 | + for (int j : gx) |
| 72 | + for (int k = 0; k < 2; k++) { |
| 73 | + if (result == -1) |
| 74 | + result = costs[k][i][j]; |
| 75 | + else if (costs[k][i][j] >= 0) |
| 76 | + result = Math.min(result, costs[k][i][j]); |
| 77 | + } |
| 78 | + System.out.println(result); |
| 79 | + } |
| 80 | + sc.close(); |
| 81 | + } |
| 82 | + |
| 83 | + static class Node implements Comparable<Node> { |
| 84 | + int x, y, cost; |
| 85 | + char foot; |
| 86 | + |
| 87 | + Node(int i, int j, int c, char f) { |
| 88 | + x = i; |
| 89 | + y = j; |
| 90 | + cost = c; |
| 91 | + foot = f; |
| 92 | + } |
| 93 | + |
| 94 | + public int compareTo(Node anotherNode) { |
| 95 | + if (this.foot == anotherNode.foot) |
| 96 | + return this.cost - anotherNode.cost; |
| 97 | + return this.foot - anotherNode.foot; |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | +} |
0 commit comments