Created
January 8, 2017 12:51
-
-
Save Wonicon/6b79625341a3315ab87c43da3f456219 to your computer and use it in GitHub Desktop.
Optimized accept-reject sequence to approach target frame. (Pokemon Sun / Moon)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Scanner; | |
/** | |
* This code generate optimized accept-reject sequence to approach target frame. | |
* Background: Pokemon Sun / Moon Random Number | |
*/ | |
public class JumpFrame { | |
enum Op {Accept, Reject}; | |
static class Frame { | |
int prev; | |
Op op; | |
int distance; | |
Frame(int prev, Op op, int distance) { | |
this.prev = prev; | |
this.op = op; // Operation on `prev' frame. | |
this.distance = distance; | |
} | |
} | |
Frame[] frames; | |
private void updateNextFrame(int next, int curr, Op op) { | |
if (next < frames.length && frames[curr].distance + 1 < frames[next].distance) { | |
frames[next].distance = frames[curr].distance + 1; | |
frames[next].prev = curr; | |
frames[next].op = op; | |
} | |
} | |
public void dp(int[] next) { | |
frames = new Frame[next.length]; | |
for (int i = 0; i < next.length; i++) { | |
frames[i] = new Frame(i - 1, Op.Reject, i); | |
} | |
for (int i = 0; i < next.length; i++) { | |
updateNextFrame(i + next[i], i, Op.Accept); | |
updateNextFrame(i + 1, i, Op.Reject); | |
} | |
} | |
public void route(int target) { | |
if (target > 0) { | |
route(frames[target].prev); | |
System.out.println("frame " + frames[target].prev + ": " + frames[target].op); | |
} | |
} | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
System.out.print("Input target frame: "); | |
int target = input.nextInt(); | |
System.out.println("Input frame sequence: <frame.no> <consume>, end with EOF."); | |
ArrayList<Integer> array = new ArrayList<>(); | |
while (input.hasNext()) { | |
input.nextInt(); | |
array.add(input.nextInt()); | |
} | |
JumpFrame jf = new JumpFrame(); | |
jf.dp(array.stream().mapToInt(e -> e).toArray()); | |
jf.route(target); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment