Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.PriorityQueue;
- import java.util.Scanner;
- /**
- * Generic class to represent a regular pair (C++ style).
- *
- * Use it for storing the indexes of a grid cell like this <row, col>.
- */
- class Pair<T, U> {
- public T first;
- public U second;
- public Pair(T first, U second) {
- this.first = first;
- this.second = second;
- }
- @Override
- public boolean equals(Object other) {
- Pair<T, U> p = (Pair<T, U>) other;
- return this.first.equals(p.first) && this.second.equals(p.second);
- }
- @Override
- public int hashCode() {
- int result = this.first == null ? 0 : this.first.hashCode();
- result = result * 31 + (this.second == null ? 0 : this.second.hashCode());
- return result;
- }
- @Override
- public String toString() {
- return "<" + this.first + ", " + this.second + ">";
- }
- }
- /**
- * Generic class to represent a tuple with three members.
- *
- * Use it to store cell to expand (whose row might be triTuple.second and col triTuple.third).
- * triTuple.first (g + h) stores the cost associated with the cell defined by the last two members.
- */
- class TriTuple<T, U, V> implements Comparable<TriTuple<T, U, V>> {
- public T first;
- public U second;
- public V third;
- public TriTuple(T first, U second, V third) {
- this.first = first;
- this.second = second;
- this.third = third;
- }
- @Override
- public boolean equals(Object other) {
- TriTuple<T, U, V> t = (TriTuple<T, U, V>) other;
- return this.first.equals(t.first) && this.second.equals(t.second) && this.third.equals(t.third);
- }
- @Override
- public int hashCode() {
- int result = this.first == null ? 0 : this.first.hashCode();
- result = result * 31 + (this.second == null ? 0 : this.second.hashCode());
- result = result * 31 + (this.third == null ? 0 : this.third.hashCode());
- return result;
- }
- @Override
- public int compareTo(TriTuple<T, U, V> other) {
- return (Integer) this.first - (Integer) other.first;
- }
- @Override
- public String toString() {
- return "<" + this.first + ", " + this.second + ", " + this.third + ">";
- }
- }
- public class Solution {
- /**
- * Generic method to print the grid.
- *
- * @param m The matrix to display.
- */
- public static <T> void printMatrix(List<List<T>> m) {
- System.out.println("Matrix contents:");
- for (List<T> v : m) {
- for (T x : v) {
- System.out.print(x);
- }
- System.out.println();
- }
- System.out.println();
- }
- /**
- * Generic method to print the path to a goal state.
- *
- * Each element in the path is a pair containing a matrix cell's row and col.
- *
- * @param path The path to a goal state.
- */
- public static <T, U> void printPath(List<Pair<T, U>> path) {
- System.out.println(path.size() - 1);
- for (int i = path.size() - 1; i >= 0; i--) {
- System.out.println(path.get(i).first + " " + path.get(i).second);
- }
- }
- /**
- * Computes and returns the absolute value of an integer.
- *
- * @param a Integer value to compute the absolute value for.
- *
- * @return The absolute value of the integer supplied as input.
- */
- public static Integer abs(Integer a) {
- return a < 0 ? -a : a;
- }
- /**
- * Manhattan distance heuristic used by A*.
- *
- * @param source A cell in the matrix encoded as <row, col>.
- * @param dest A cell in the matrix encoded as <row, col>.
- *
- * @return Manhattan distance between the two cells.
- */
- public static int h(Pair<Integer, Integer> source, Pair<Integer, Integer> dest) {
- return abs(source.first - dest.first) + abs(source.second - dest.second);
- }
- /**
- * Method to retrieve a list of cells accesible from a given cell.
- *
- * @param M Grid, encoded as a list of Characters.
- * @param row Row of the source cell.
- * @param col Column of the source cell.
- *
- * @return A list containing at most four neighboring cells.
- */
- public static <T extends Character> List<Pair<Integer, Integer>> getNeighbors(List<List<T>> M, int row, int col) {
- List<Pair<Integer, Integer>> ret = new ArrayList<>();
- if (row - 1 >= 0 && !M.get(row - 1).get(col).equals('%')) {
- ret.add(new Pair<>(row - 1, col));
- }
- if (row + 1 < M.size() && !M.get(row + 1).get(col).equals('%')) {
- ret.add(new Pair<>(row + 1, col));
- }
- if (col - 1 >= 0 && !M.get(row).get(col - 1).equals('%')) {
- ret.add(new Pair<>(row, col - 1));
- }
- if (col + 1 < M.get(row).size() && !M.get(row).get(col + 1).equals('%')) {
- ret.add(new Pair<>(row, col + 1));
- }
- return ret;
- }
- /**
- * Reconstructs the path from source to destination, based on the parents.
- *
- * @param n Encoding of the destination cell.
- * @param p A mapping between each cell and its parent cell.
- */
- public static List<Pair<Integer, Integer>> buildPath(Pair<Integer, Integer> n, Map<Pair<Integer, Integer>, Pair<Integer, Integer>> p) {
- List<Pair<Integer, Integer>> ret = new ArrayList<>();
- while (n.first != -1 && n.second != -1) {
- ret.add(n);
- n = p.get(n);
- }
- return ret;
- }
- /**
- * Method to implement the A* algorithm.
- *
- * @param M Encoding of the grid.
- * @param rp Pacman's starting row.
- * @param cp Pacman's starting column.
- * @param rf Food row.
- * @param cf Fool column.
- *
- * @return The shortest path between Pacman and food, determined with A*.
- * If no such path exists, returns an empty path.
- */
- public static <T extends Character> List<Pair<Integer, Integer>> astar(List<List<T>> M, int rp, int cp, int rf, int cf) {
- PriorityQueue<TriTuple<Integer, Integer, Integer>> open = new PriorityQueue<>();
- /*
- * TODO: implement A*.
- * Use `open` for storing the next cell to expand, along with its total score (f = g + h).
- * Use `getNeighbors()` to retrieve the cells accessible from a given cell.
- * Use `buildPath()` to return the path from Pacman to food (once you found one path).
- * Use `h()` to estimate the cost from the current cell to the destination cell (you can try other heuristics too).
- * The `closed` set may be implemented as a hashMap, mapping each cell to its visited status (true or false).
- * The parent of each cell may be stored in a hashMap.
- */
- return new ArrayList<Pair<Integer, Integer>>();
- }
- public static void main(String[] args) {
- int rp, cp;
- int rf, cf;
- int nr, nc;
- char c;
- Scanner s = new Scanner(System.in);
- rp = s.nextInt();
- cp = s.nextInt();
- rf = s.nextInt();
- cf = s.nextInt();
- nr = s.nextInt();
- nc = s.nextInt();
- List<List<Character>> M = new ArrayList<>();
- for (int i = 0; i < nr; i++) {
- List<Character> currentRow = new ArrayList<>();
- String nextRow = s.next();
- for (int j = 0; j < nc; j++) {
- currentRow.add(nextRow.charAt(j));
- }
- M.add(currentRow);
- }
- s.close();
- //printMatrix(M);
- List<Pair<Integer, Integer>> path = astar(M, rp, cp, rf, cf);
- printPath(path);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement