Advertisement
Guest User

It's broke

a guest
Nov 23rd, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1.     public SearchResult search(String initConfig, int modeFlag) {
  2.         int numPoppedStates = 0;
  3.         char[] possibleMoves = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
  4.  
  5.         // Step 1
  6.         PriorityQueue<State> open = new PriorityQueue<State>(11, State.comparator);
  7.         HashMap<String, Integer> realCosts = new HashMap<String, Integer>();
  8.         State initial = new State(initConfig, 0, getHeuristicCost(initConfig, modeFlag), "");
  9.         open.add(initial);
  10.  
  11.         // Step 2
  12.         while (!open.isEmpty()) {
  13.             // Step 3
  14.             State state = open.poll();
  15.             numPoppedStates ++;
  16.  
  17.             // Step 4, 5, 6
  18.             if (checkGoal(state.config)) {
  19.                 return new SearchResult(state.config, state.opSequence, numPoppedStates);
  20.             }
  21.  
  22.             // Step 7, 8
  23.             for (char moveChoice : possibleMoves) {
  24.                 String childConfig = move(state.config, moveChoice);
  25.                 State childState = new State(childConfig, state.realCost + 1,
  26.                         getHeuristicCost(childConfig, modeFlag), state.opSequence + moveChoice);
  27.  
  28.                 // Step 9, 10, 11
  29.                 if (!realCosts.containsKey(childState.config)) {
  30.                     realCosts.put(childState.config, childState.realCost);
  31.                     open.add(childState);
  32.                 } else {
  33.                     // Step 13
  34.                     if (childState.realCost < realCosts.get(childState.config)) {
  35.                         // Step 15, 16, 17, 18
  36.                         realCosts.put(childState.config, childState.realCost);
  37.                         open.remove(childState);
  38.                         open.add(childState);
  39.                     }
  40.                 }
  41.             }
  42.         }
  43.        
  44.         return null;
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement