-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pollack.java
98 lines (90 loc) · 4.1 KB
/
Pollack.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
package submit;
import java.util.HashSet;
import java.util.List;
import game.FindState;
import game.Finder;
import game.Node;
import game.NodeStatus;
import game.ScramState;
/** Student solution for two methods. */
public class Pollack extends Finder {
/** Get to the orb in as few steps as possible. <br>
* Once you get there, you must return from the function in order to pick it up. <br>
* If you continue to move after finding the orb rather than returning, it will not count.<br>
* If you return from this function while not standing on top of the orb, it will count as <br>
* a failure.
*
* There is no limit to how many steps you can take, but you will receive<br>
* a score bonus multiplier for finding the orb in fewer steps.
*
* At every step, you know only your current tile's ID and the ID of all<br>
* open neighbor tiles, as well as the distance to the orb at each of <br>
* these tiles (ignoring walls and obstacles).
*
* In order to get information about the current state, use functions<br>
* state.currentLoc(), state.neighbors(), and state.distanceToOrb() in FindState.<br>
* You know you are standing on the orb when distanceToOrb() is 0.
*
* Use function state.moveTo(long id) in FindState to move to a neighboring<br>
* tile by its ID. Doing this will change state to reflect your new position.
*
* A suggested first implementation that will always find the orb, but <br>
* likely won't receive a large bonus multiplier, is a depth-first walk. <br>
* Some modification is necessary to make the search better, in general. */
@Override
public void findOrb(FindState state) {
// TODO 1: Get the orb
HashSet<Long> h= new HashSet<>();
dfs(state, h);
}
public void dfs(FindState state, HashSet<Long> h) {
Long n= state.currentLoc();
h.add(n);
if (state.distanceToOrb() == 0) { return; }
for (NodeStatus w : state.neighbors()) {
if (!h.contains(w.getId())) {
state.moveTo(w.getId());
dfs(state, h);
if (state.distanceToOrb() == 0) { return; }
state.moveTo(n);
}
}
}
/** Pres Pollack is standing at a node given by parameter state.<br>
*
* Get out of the cavern before the ceiling collapses, trying to collect as <br>
* much gold as possible along the way. Your solution must ALWAYS get out <br>
* before time runs out, and this should be prioritized above collecting gold.
*
* You now have access to the entire underlying graph, which can be accessed <br>
* through parameter state. <br>
* state.currentNode() and state.getExit() will return Node objects of interest, and <br>
* state.allNodes() will return a collection of all nodes on the graph.
*
* The cavern will collapse in the number of steps given by <br>
* state.stepsLeft(), and for each step this number is decremented by the <br>
* weight of the edge taken. <br>
* Use state.stepsLeft() to get the time still remaining, <br>
* Use state.moveTo() to move to a destination node adjacent to your current node.<br>
* Do not call state.grabGold(). Gold on a node is automatically picked up <br>
* when the node is reached.<br>
*
* The method must return from this function while standing at the exit. <br>
* Failing to do so before time runs out or returning from the wrong <br>
* location will be considered a failed run.
*
* You will always have enough time to scram using the shortest path from the <br>
* starting position to the exit, although this will not collect much gold. <br>
* For this reason, using the shortest path method to calculate the shortest <br>
* path to the exit is a good starting solution */
@Override
public void scram(ScramState state) {
// TODO 2: scram
List<Node> lists= Path.shortest(state.currentNode(), state.getExit());
lists.remove(0);
for (Node w : lists) {
state.moveTo(w);
}
return;
}
}