Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public SearchResult search(String initConfig, int modeFlag) {
- int numPoppedStates = 0;
- char[] possibleMoves = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
- // Step 1
- PriorityQueue<State> open = new PriorityQueue<State>(11, State.comparator);
- HashMap<String, Integer> realCosts = new HashMap<String, Integer>();
- State initial = new State(initConfig, 0, getHeuristicCost(initConfig, modeFlag), "");
- open.add(initial);
- // Step 2
- while (!open.isEmpty()) {
- // Step 3
- State state = open.poll();
- numPoppedStates ++;
- // Step 4, 5, 6
- if (checkGoal(state.config)) {
- return new SearchResult(state.config, state.opSequence, numPoppedStates);
- }
- // Step 7, 8
- for (char moveChoice : possibleMoves) {
- String childConfig = move(state.config, moveChoice);
- State childState = new State(childConfig, state.realCost + 1,
- getHeuristicCost(childConfig, modeFlag), state.opSequence + moveChoice);
- // Step 9, 10, 11
- if (!realCosts.containsKey(childState.config)) {
- realCosts.put(childState.config, childState.realCost);
- open.add(childState);
- } else {
- // Step 13
- if (childState.realCost < realCosts.get(childState.config)) {
- // Step 15, 16, 17, 18
- realCosts.put(childState.config, childState.realCost);
- open.remove(childState);
- open.add(childState);
- }
- }
- }
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement