/* * Translation of bowen-dp-pokemon.cpp into java Dynamic programming with bitmask keeping track of - The current location. - Which pokemons have been caught. This has running time of O(L^2 * 2^M), where L is the number of locations, and M is the number of distinct pokemons. */ import java.util.*; public class BowenDpPokemon { static class Pair implements Comparable { public Pair() { first = second = 0; } public Pair(int a, int b) { first = a; second = b; } public int first, second; public boolean equals(Object other) { Pair p = (Pair)other; return first == p.first && second == p.second; } public int compareTo(Pair other) { if (first != other.first) return first - other.first; return second - other.second; } public int hashCode() { return (first << 16) | second; } } public static final int INF = (int)1E9; public static HashMap pid = new HashMap(); public static HashMap lid = new HashMap(); public static HashMap pokemon = new HashMap(); public static HashMap pokemask = new HashMap(); public static Pair loc[] = new Pair[21]; public static int N, M, L, S, dp[][] = new int[21][1<<15]; public static int dist(int i, int j) { return Math.abs(loc[i].first - loc[j].first) + Math.abs(loc[i].second - loc[j].second); } public static int solve(int x, int m) { if (m == (1< it : pokemon.entrySet()) { lid.put(it.getKey(), L); loc[L] = it.getKey(); pokemask.put(L, it.getValue()); L++; } for (int arr[] : dp) { Arrays.fill(arr, -1); } S = lid.get(new Pair(0, 0)); System.out.println(solve(S, pokemask.get(S))); } }