Skip to content

Commit 6413b4a

Browse files
committed
Add some Java files
1 parent ac850f0 commit 6413b4a

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// A New Plan of Aizu Ski Resort
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[][] field = new int[h][w];
12+
int[][] dp = new int[h][w];
13+
for (int i = 0; i < h; i++)
14+
for (int j = 0; j < w; j++)
15+
field[i][j] = sc.nextInt();
16+
for (int i = 0; i < w; i++)
17+
if (field[0][i] == 0)
18+
dp[0][i] = 1;
19+
for (int i = 0; i < h - 1; i++) {
20+
for (int j = 0; j < w; j++) {
21+
switch (field[i][j]) {
22+
case 0:
23+
if (field[i + 1][j] != 1)
24+
dp[i + 1][j] += dp[i][j];
25+
if (j >= 1 && field[i + 1][j - 1] == 0)
26+
dp[i + 1][j - 1] += dp[i][j];
27+
if (j < w - 1 && field[i + 1][j + 1] == 0)
28+
dp[i + 1][j + 1] += dp[i][j];
29+
break;
30+
case 2:
31+
if (i < h - 2 && field[i + 2][j] != 1)
32+
dp[i + 2][j] += dp[i][j];
33+
break;
34+
}
35+
}
36+
}
37+
int result = 0;
38+
for (int i = 0; i < w; i++) {
39+
result += dp[h - 1][i];
40+
if (h >= 2 && field[h - 2][i] == 2)
41+
result += dp[h - 2][i];
42+
}
43+
System.out.println(result);
44+
}
45+
sc.close();
46+
}
47+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

AizuOnlineJudge/ProblemSets11/1150_Cliff_Climbing/README.md

Whitespace-only changes.

0 commit comments

Comments
 (0)