Created
July 6, 2017 23:52
-
-
Save badbye/833cbb07cf49b2254824cc479b4f38ce to your computer and use it in GitHub Desktop.
Horse Run: The shortest path from one start point to the end point.
This file contains hidden or 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 edu.princeton.cs.algs4.MinPQ; | |
| import edu.princeton.cs.algs4.Stack; | |
| import edu.princeton.cs.algs4.StdIn; | |
| import edu.princeton.cs.algs4.StdOut; | |
| public class HorseRun{ | |
| private int n = 10; | |
| private int count; | |
| private Stack<Point> road; | |
| private boolean solverAble; | |
| public HorseRun(int x0, int y0, int x1, int y1){ | |
| if (x0 == x1 && y0 == y1) return; | |
| Point pStart = new Point(x0, y0); | |
| Point pEnd = new Point(x1, y1); | |
| MinPQ <State> minpq = new MinPQ<State>(); | |
| State initial = new State(pStart, 0, null); | |
| minpq.insert(initial); | |
| while (minpq.size() > 0 && !solverAble){ | |
| initial = minpq.delMin(); | |
| count = initial.steps + 1; | |
| for (Point p: initial.p.neibors()){ | |
| if (p.equals(pEnd)) { | |
| solverAble = true; | |
| processPath(new State(p, count, initial)); | |
| break; | |
| } | |
| if (initial.pre != null && p.equals(initial.pre.p)) continue; | |
| minpq.insert(new State(p, count, initial)); | |
| } | |
| } | |
| } | |
| private void processPath(State state){ | |
| road = new Stack<Point>(); | |
| while (state != null){ | |
| road.push(state.p); | |
| state = state.pre; | |
| } | |
| } | |
| public boolean isSolvable(){ | |
| return solverAble; | |
| } | |
| public int moves(){ | |
| return count; | |
| } | |
| public Iterable<Point> path(){ | |
| if (!solverAble) return null; | |
| return road; | |
| } | |
| private class State implements Comparable <State>{ | |
| public Point p; | |
| public int steps; | |
| public State pre; | |
| public State(Point point, int step, State previous){ | |
| p = point; | |
| steps = step; | |
| pre = previous; | |
| } | |
| public int compareTo(State that){ | |
| State y = (State) that; | |
| if (steps > y.steps) return +1; | |
| else if (steps < y.steps) return -1; | |
| return 0; | |
| } | |
| } | |
| private class Point{ | |
| public int x; | |
| public int y; | |
| public Point(int x0, int y0){ | |
| x = x0; | |
| y = y0; | |
| } | |
| public String toString() { | |
| StringBuilder s = new StringBuilder(); | |
| s.append("Point: <" + x + ", " + y + ">"); | |
| return s.toString(); | |
| } | |
| public boolean equals(Object that){ | |
| if (this == that) return true; | |
| if (that == null) return false; | |
| if (that.getClass() != this.getClass()) return false; | |
| Point that2 = (Point) that; | |
| if (x != that2.x) return false; | |
| if (y != that2.y) return false; | |
| return true; | |
| } | |
| public Iterable<Point> neibors(){ | |
| Stack<Point> s = new Stack<Point>(); | |
| int n = 10; | |
| if (x >= 1 && y >=2) s.push(new Point(x - 1, y - 2)); | |
| if (x >= 1 && y <= n-3) s.push(new Point(x - 1, y + 2)); | |
| if (x >= 2 && y >=1) s.push(new Point(x - 2, y - 1)); | |
| if (x >= 2 && y <= n-2) s.push(new Point(x - 2, y + 1)); | |
| if (x <= n-2 && y >=2) s.push(new Point(x + 1, y - 2)); | |
| if (x <= n-2 && y <=n-3) s.push(new Point(x + 1, y + 2)); | |
| if (x <= n-3 && y >= 1) s.push(new Point(x + 2, y - 1)); | |
| if (x <= n-3 && y <= n-2) s.push(new Point(x + 2, y + 1)); | |
| return s; | |
| } | |
| } | |
| public String toString(){ | |
| int[][] board = new int[n][n]; | |
| int st = 0; | |
| for (Point p: path()){ | |
| board[p.x][p.y] = st+1; | |
| st++; | |
| } | |
| StringBuilder s = new StringBuilder(); | |
| for (int i=0; i< n; i++){ | |
| for (int j=0; j<n; j++){ | |
| if (board[i][j] == 0) s.append("- "); | |
| else s.append(board[i][j]-1 + " "); | |
| } | |
| s.append("\n"); | |
| } | |
| return s.toString(); | |
| } | |
| private static void checkInput(int i){ | |
| if (i > 10 && i < 0) throw new java.lang.IllegalArgumentException("all the ints should be between 0 to 9"); | |
| } | |
| public static void main(String[] args){ | |
| StdOut.println("Please write down 4 numbers in [0, 9]. (x0, y0) -> (x1, y1):"); | |
| int x0 = StdIn.readInt(); | |
| checkInput(x0); | |
| int y0 = StdIn.readInt(); | |
| checkInput(y0); | |
| int x1 = StdIn.readInt(); | |
| checkInput(x1); | |
| int y1 = StdIn.readInt(); | |
| checkInput(y1); | |
| StdOut.println("Run from (" + x0 + ", " + y0 + ") to (" + x1 + ", " + y1 + "):"); | |
| HorseRun r = new HorseRun(x0, y0, x1, y1); | |
| if (r.isSolvable()) { | |
| StdOut.println("solve in " + r.moves() + " steps."); | |
| for (Point p: r.path()){ | |
| StdOut.println(p); | |
| } | |
| StdOut.println(r); | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The reference Java files are listed here: http://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/
Example: