On a 3x3 board, there are 8 tiles represented by the integers 1 through 8, and an empty square represented by 0.
A move consists of choosing 0 and a 4-directionally adjacent number and swapping it.
Given an initial state of the puzzle board and final state, return the least number of moves required so that the initial state to final state.
If it is impossible to move from initial state to final state, return -1.
Example
Example 1:
Input:
[
[2,8,3],
[1,0,4],
[7,6,5]
]
[
[1,2,3],
[8,0,4],
[7,6,5]
]
Output:
4
Explanation:
[ [
[2,8,3], [2,0,3],
[1,0,4], --> [1,8,4],
[7,6,5] [7,6,5]
] ]
[ [
[2,0,3], [0,2,3],
[1,8,4], --> [1,8,4],
[7,6,5] [7,6,5]
] ]
[ [
[0,2,3], [1,2,3],
[1,8,4], --> [0,8,4],
[7,6,5] [7,6,5]
] ]
[ [
[1,2,3], [1,2,3],
[0,8,4], --> [8,0,4],
[7,6,5] [7,6,5]
] ]
Example 2:
Input:
[[2,3,8],[7,0,5],[1,6,4]]
[[1,2,3],[8,0,4],[7,6,5]]
Output:
-1
Challenge
- How to optimize the memory?
- Can you solve it with A* algorithm?
public class Solution { /** * @param init_state: the initial state of chessboard * @param final_state: the final state of chessboard * @return: return an integer, denote the number of minimum moving */ public int minMoveStep(int[][] init_state, int[][] final_state) { String start = matrixToString(init_state); String end = matrixToString(final_state); Queue<String> queue = new LinkedList<>(); Set<String> visited = new HashSet<>(); queue.offer(start); visited.add(start); int numSteps = 0; while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { String currState = queue.poll(); if (currState.equals(end)) { return numSteps; } for (String nextState : getNextStates(currState)) { if (!visited.contains(nextState)) { queue.offer(nextState); visited.add(nextState); } } } numSteps++; } return -1; } private String matrixToString(int[][] matrix) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { sb.append(matrix[i][j]); } } return sb.toString(); } private List<String> getNextStates(String state) { int index = state.indexOf('0'); int x = index / 3; int y = index % 3; List<String> ans = new ArrayList<>(); int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; for (int i = 0; i < 4; i++) { int nx = x + dirs[i][0]; int ny = y + dirs[i][1]; if (nx >= 0 && nx < 3 && ny >= 0 && ny < 3) { char[] stateChar = state.toCharArray(); stateChar[x * 3 + y] = stateChar[nx * 3 + ny]; stateChar[nx * 3 + ny] = '0'; ans.add(new String(stateChar)); } } return ans; } }
No comments:
Post a Comment