Skip to content

Commit e08d48a

Browse files
committed
Add some programs
1 parent 6413b4a commit e08d48a

File tree

6 files changed

+293
-0
lines changed

6 files changed

+293
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// Twirling Robot
2+
import java.util.*;
3+
4+
public class Main {
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
int[] dx = { 0, 1, 0, -1 };
8+
int[] dy = { -1, 0, 1, 0 };
9+
while (true) {
10+
int w = sc.nextInt(), h = sc.nextInt();
11+
if ((w | h) == 0)
12+
break;
13+
int[][] field = new int[h][w];
14+
for (int i = 0; i < h; i++)
15+
for (int j = 0; j < w; j++)
16+
field[i][j] = sc.nextInt();
17+
int[] command = new int[4];
18+
for (int i = 0; i < 4; i++)
19+
command[i] = sc.nextInt();
20+
int[][][] costs = new int[4][h][w];
21+
for (int k = 0; k < 4; k++)
22+
for (int i = 0; i < h; i++)
23+
Arrays.fill(costs[k][i], -1);
24+
PriorityQueue<Node> queue = new PriorityQueue<Node>();
25+
queue.add(new Node(0, 0, 0, 'E'));
26+
while (!queue.isEmpty()) {
27+
Node node = queue.poll();
28+
int x = node.x;
29+
int y = node.y;
30+
int cost = node.cost;
31+
if (node.dir == 'N') {
32+
if (costs[0][y][x] >= 0 && costs[0][y][x] <= cost)
33+
continue;
34+
costs[0][y][x] = cost;
35+
for (int i = 0; i < 4; i++) {
36+
int tcost = cost;
37+
if (field[y][x] != i)
38+
tcost += command[i];
39+
int tx = x + dx[i];
40+
int ty = y + dy[i];
41+
if (tx >= 0 && tx < w && ty >= 0 && ty < h)
42+
switch (i) {
43+
case 0:
44+
queue.add(new Node(tx, ty, tcost, 'N'));
45+
break;
46+
case 1:
47+
queue.add(new Node(tx, ty, tcost, 'E'));
48+
break;
49+
case 2:
50+
queue.add(new Node(tx, ty, tcost, 'S'));
51+
break;
52+
default:
53+
queue.add(new Node(tx, ty, tcost, 'W'));
54+
}
55+
}
56+
}
57+
if (node.dir == 'E') {
58+
if (costs[1][y][x] >= 0 && costs[1][y][x] <= cost)
59+
continue;
60+
costs[1][y][x] = cost;
61+
for (int i = 0; i < 4; i++) {
62+
int tcost = cost;
63+
if (field[y][x] != i)
64+
tcost += command[i];
65+
int tx = x + dx[(1 + i) % 4];
66+
int ty = y + dy[(1 + i) % 4];
67+
if (tx >= 0 && tx < w && ty >= 0 && ty < h)
68+
switch (i) {
69+
case 0:
70+
queue.add(new Node(tx, ty, tcost, 'E'));
71+
break;
72+
case 1:
73+
queue.add(new Node(tx, ty, tcost, 'S'));
74+
break;
75+
case 2:
76+
queue.add(new Node(tx, ty, tcost, 'W'));
77+
break;
78+
default:
79+
queue.add(new Node(tx, ty, tcost, 'N'));
80+
}
81+
}
82+
}
83+
if (node.dir == 'S') {
84+
if (costs[2][y][x] >= 0 && costs[2][y][x] <= cost)
85+
continue;
86+
costs[2][y][x] = cost;
87+
for (int i = 0; i < 4; i++) {
88+
int tcost = cost;
89+
if (field[y][x] != i)
90+
tcost += command[i];
91+
int tx = x + dx[(2 + i) % 4];
92+
int ty = y + dy[(2 + i) % 4];
93+
if (tx >= 0 && tx < w && ty >= 0 && ty < h)
94+
switch (i) {
95+
case 0:
96+
queue.add(new Node(tx, ty, tcost, 'S'));
97+
break;
98+
case 1:
99+
queue.add(new Node(tx, ty, tcost, 'W'));
100+
break;
101+
case 2:
102+
queue.add(new Node(tx, ty, tcost, 'N'));
103+
break;
104+
default:
105+
queue.add(new Node(tx, ty, tcost, 'E'));
106+
}
107+
}
108+
}
109+
if (node.dir == 'W') {
110+
if (costs[3][y][x] >= 0 && costs[3][y][x] <= cost)
111+
continue;
112+
costs[3][y][x] = cost;
113+
for (int i = 0; i < 4; i++) {
114+
int tcost = cost;
115+
if (field[y][x] != i)
116+
tcost += command[i];
117+
int tx = x + dx[(3 + i) % 4];
118+
int ty = y + dy[(3 + i) % 4];
119+
if (tx >= 0 && tx < w && ty >= 0 && ty < h)
120+
switch (i) {
121+
case 0:
122+
queue.add(new Node(tx, ty, tcost, 'W'));
123+
break;
124+
case 1:
125+
queue.add(new Node(tx, ty, tcost, 'N'));
126+
break;
127+
case 2:
128+
queue.add(new Node(tx, ty, tcost, 'E'));
129+
break;
130+
default:
131+
queue.add(new Node(tx, ty, tcost, 'S'));
132+
}
133+
}
134+
}
135+
}
136+
int result = Integer.MAX_VALUE;
137+
for (int i = 0; i < 4; i++)
138+
if (costs[i][h - 1][w - 1] >= 0)
139+
result = Math.min(result, costs[i][h - 1][w - 1]);
140+
System.out.println(result);
141+
}
142+
sc.close();
143+
}
144+
145+
static class Node implements Comparable<Node> {
146+
int x, y, cost;
147+
char dir;
148+
149+
Node(int i, int j, int c, char d) {
150+
x = i;
151+
y = j;
152+
cost = c;
153+
dir = d;
154+
}
155+
156+
public int compareTo(Node anotherNode) {
157+
if (this.dir == anotherNode.dir)
158+
return this.cost - anotherNode.cost;
159+
return this.dir - anotherNode.dir;
160+
}
161+
162+
}
163+
}

AizuOnlineJudge/ProblemSets11/1156_Twirling_Robot/README.md

Whitespace-only changes.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Molecular Formula
2+
import java.util.*;
3+
4+
public class Main {
5+
static String formula;
6+
static int pos;
7+
static Map<String, Integer> table;
8+
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
table = new HashMap<String, Integer>();
12+
while (true) {
13+
String atom = sc.next();
14+
if (atom.equals("END_OF_FIRST_PART"))
15+
break;
16+
int weight = sc.nextInt();
17+
table.put(atom, weight);
18+
}
19+
while (true) {
20+
formula = sc.next();
21+
pos = 0;
22+
if (formula.equals("0"))
23+
break;
24+
int result = formula();
25+
if (result == -1)
26+
System.out.println("UNKNOWN");
27+
else
28+
System.out.println(result);
29+
}
30+
sc.close();
31+
}
32+
33+
static int formula() {
34+
int result = 0;
35+
while (pos < formula.length()) {
36+
int t = molecule();
37+
if (t == -1)
38+
return -1;
39+
result += t;
40+
}
41+
return result;
42+
}
43+
44+
static int molecule() {
45+
int result = 0;
46+
char c = formula.charAt(pos);
47+
if (c == '(') {
48+
pos++;
49+
while (formula.charAt(pos) != ')')
50+
result += molecule();
51+
pos++;
52+
result *= number();
53+
} else {
54+
result = atom();
55+
if (result == -1)
56+
return -1;
57+
if (pos < formula.length()
58+
&& Character.isDigit(formula.charAt(pos)))
59+
result *= number();
60+
}
61+
return result;
62+
}
63+
64+
static int atom() {
65+
String atom = formula.substring(pos, ++pos);
66+
if (pos < formula.length()
67+
&& Character.isLowerCase(formula.charAt(pos)))
68+
atom += formula.charAt(pos++);
69+
if (table.containsKey(atom))
70+
return table.get(atom);
71+
return -1;
72+
}
73+
74+
static int number() {
75+
int result = 0;
76+
while (pos < formula.length() && Character.isDigit(formula.charAt(pos))) {
77+
result *= 10;
78+
result += formula.charAt(pos++) - '0';
79+
}
80+
return result;
81+
}
82+
}

AizuOnlineJudge/ProblemSets12/1244_Molecular_Formula/README.md

Whitespace-only changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.*;
2+
3+
public class CatchTheBeatEasy {
4+
5+
public String ableToCatchAll(int[] x, int[] y) {
6+
PriorityQueue<Node> queue = new PriorityQueue<Node>();
7+
queue.add(new Node(0, 0));
8+
for (int i = 0; i < x.length; i++)
9+
queue.add(new Node(x[i], y[i]));
10+
boolean b = true;
11+
Node node = queue.poll();
12+
while (!queue.isEmpty() && b) {
13+
Node newnode = queue.poll();
14+
b = b && (Math.abs(newnode.x - node.x) <= newnode.y - node.y);
15+
node = newnode;
16+
}
17+
if (b)
18+
return "Able to catch";
19+
return "Not able to catch";
20+
}
21+
22+
class Node implements Comparable<Node> {
23+
int x, y;
24+
25+
Node(int x, int y) {
26+
this.x = x;
27+
this.y = y;
28+
}
29+
30+
@Override
31+
public int compareTo(Node o) {
32+
return this.y - o.y;
33+
}
34+
}
35+
36+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class CatAndRat {
2+
3+
final double PI = 3.14159265358979;
4+
5+
public double getTime(int R, int T, int Vrat, int Vcat) {
6+
if (Vrat >= Vcat)
7+
return -1.0;
8+
double len = Math.min(Vrat * T, R * PI);
9+
return len / (Vcat - Vrat);
10+
}
11+
12+
}

0 commit comments

Comments
 (0)