-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttwo.java
147 lines (134 loc) · 4.16 KB
/
ttwo.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
/*
ID: Xu Yan
LANG: JAVA
TASK: ttwo
*/
/**
* Thoughts: I approach the problem by simulation. I think the tricky point is how to save state efficiently so that we can know if they'll never meet quickly
* Pitfalls:
*
* Take-away tips:
*
* @author Xu Yan
* @date May 25th, 2016
*/
public class ttwo {
private enum Direction {
North(1, new int[]{-1,0}), East(2, new int[]{0,1}), South(3, new int[]{1,0}), West(4, new int[]{0,-1});
private final int value;
private final int[] offset;
Direction(int value, int[] offset) {
this.value = value;
this.offset = offset;
}
}
private static class MovingObject {
private char[][] graph;
int[] coordinate;
Direction direction;
public MovingObject(char[][] graph, int[] coordinate, Direction direction) {
this.graph = graph;
this.coordinate = coordinate;
this.direction = direction;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof MovingObject)) {
return false;
}
MovingObject that = (MovingObject) obj;
return this.coordinate[0] == that.coordinate[0] && this.coordinate[1] == that.coordinate[1];
}
@Override
public int hashCode() {
int result = 17;
result = result * 31 + this.coordinate[0];
result = result * 31 + this.coordinate[1];
result = result * 31 + this.direction.value;
return result;
}
public void move() {
int new_row = this.coordinate[0] + this.direction.offset[0];
int new_col = this.coordinate[1] + this.direction.offset[1];
if (new_row >= 0 && new_row < graph.length && new_col >= 0 && new_col < graph[0].length && this.graph[new_row][new_col] != '*') {
this.coordinate[0] = new_row;
this.coordinate[1] = new_col;
} else {
changeDirection();
}
}
private void changeDirection() {
switch (this.direction) {
case North:
this.direction = Direction.East;
break;
case East:
this.direction = Direction.South;
break;
case South:
this.direction = Direction.West;
break;
case West:
this.direction = Direction.North;
break;
}
}
}
private static int getState(MovingObject[] objects) {
Arrays.sort(objects, new Comparator<MovingObject>() {
@Override
public int compare(MovingObject o1, MovingObject o2) {
// Priority: more South -> more West
return (o1.coordinate[0] != o2.coordinate[0]) ? (o1.coordinate[0] - o2.coordinate[0]) : (o1.coordinate[1] - o2.coordinate[1]);
}
});
String state = "" + objects[0].coordinate[0] + objects[0].coordinate[1] + objects[0].direction.value + objects[1].coordinate[0] + objects[1].coordinate[1] + objects[1].direction.value;
return Integer.parseInt(state);
}
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("ttwo.in"));
MovingObject farmer = null;
MovingObject cows = null;
Set<Integer> states = new HashSet<Integer>(); // The states we've already been to. If we're at a state we've been to before, farmer and cows will never meet
char[][] graph = new char[10][10];
for (int row = 0; row < 10; row++) {
String row_data = f.readLine();
for (int col = 0; col < 10; col++) {
graph[row][col] = row_data.charAt(col);
if (graph[row][col] == 'F') {
farmer = new MovingObject(graph, new int[]{row, col}, Direction.North);
} else if (graph[row][col] == 'C') {
cows = new MovingObject(graph, new int[]{row, col}, Direction.North);
}
}
}
f.close();
// Save current state
states.add(getState(new MovingObject[] {farmer, cows}));
int steps = 0;
while (!farmer.equals(cows)) {
farmer.move();
cows.move();
int new_state = getState(new MovingObject[] {farmer, cows});
if (states.contains(new_state)) {
steps = 0;
break;
}
states.add(new_state);
steps++;
}
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("ttwo.out")));
out.println(steps);
out.close();
}
}