8-Puzzle - Amazon Top Interview Questions
Problem Statement :
You are given a 3x3 board of unique integers from 0 to 8, representing the state of a puzzle. In this puzzle, you can swap the 0 with one of its 4 neighbours (if it exists), and you are trying to solve it by obtaining the following configuration: [[0, 1, 2], [3, 4, 5], [6, 7, 8]] Return the minimum number of swaps required to solve the puzzle, or return -1 if it's not possible. Constraints n = 3 where n is the number of rows and columns in board 0 ≤ board[r][c] ≤ 8 Example 1 Input board = [ [1, 0, 2], [3, 4, 5], [6, 7, 8] ] Output 1 Explanation We can swap the 0 and the 1 to solve the puzzle.
Solution :
Solution in C++ :
static unordered_map<string, int64_t> cost;
/* Precompute Utility */
inline void precompute() {
string initial_node = "012345678";
queue<string> q;
q.push(initial_node);
cost[initial_node] = 0;
while (!q.empty()) {
auto current_node = q.front();
q.pop();
int z = find(current_node.begin(), current_node.end(), '0') - current_node.begin();
assert(z >= 0 && z < 9);
int curr = cost[current_node];
int lo = z / 3 * 3;
int hi = (z / 3 + 1) * 3;
for (auto i : {-3, 3, -1, 1}) {
if (abs(i) == 3 && z + i < (int)current_node.size() && z + i >= 0 ||
abs(i) == 1 && z + i >= lo && z + i < hi) {
swap(current_node[z + i], current_node[z]);
if (!cost.count(current_node)) {
cost[current_node] = curr + 1;
q.push(current_node);
} else if (cost[current_node] > curr + 1) {
cost[current_node] = curr + 1;
q.push(current_node);
}
swap(current_node[z + i], current_node[z]);
}
}
}
}
/* Calling Precompute */
struct S {
S() {
precompute();
}
} _;
int solve(vector<vector<int>>& board) {
string target;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) target += (board[i][j] + '0');
return cost.count(target) ? cost[target] : -1;
}
Solution in Java :
import java.util.*;
class Solution {
public int solve(int[][] board) {
StringBuilder sb = new StringBuilder();
int rows = board.length, cols = board[0].length;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sb.append(String.valueOf(board[i][j]));
}
}
Map<Integer, List<Integer>> map = new HashMap<>();
map.put(0, Arrays.asList(1, 3));
map.put(1, Arrays.asList(0, 2, 4));
map.put(2, Arrays.asList(1, 5));
map.put(3, Arrays.asList(0, 4, 6));
map.put(4, Arrays.asList(1, 3, 5, 7));
map.put(5, Arrays.asList(2, 4, 8));
map.put(6, Arrays.asList(3, 7));
map.put(7, Arrays.asList(4, 6, 8));
map.put(8, Arrays.asList(5, 7));
String source = sb.toString(), target = "012345678";
if (source.equals(target)) {
return 0;
}
Queue<String> q = new LinkedList<>();
Set<String> visited = new HashSet<>();
q.add(source);
visited.add(source);
int count = 0;
while (!q.isEmpty()) {
for (int b = q.size(); b > 0; b--) {
String cur = q.poll();
int zeroIdx = cur.indexOf("0");
char[] arr = cur.toCharArray();
for (int nextIdx : map.get(zeroIdx)) {
swap(arr, zeroIdx, nextIdx);
String nextStr = new String(arr);
if (!visited.contains(nextStr)) {
if (nextStr.equals(target)) {
return count + 1;
}
visited.add(nextStr);
q.add(nextStr);
}
swap(arr, zeroIdx, nextIdx);
}
}
++count;
}
return -1;
}
private void swap(char[] arr, int i, int j) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
Solution in Python :
class Solution:
def solve(self, board):
def get_neighbours(state):
state = list(state) # make mutable
idx = state.index(0) # find index
neighbors = []
for move in [-3, 3]: # vertical switch
switch = idx + move
if 0 <= switch < 9: # check if out of bounds
state[idx], state[switch] = state[switch], state[idx]
neighbors.append(tuple(state))
state[idx], state[switch] = state[switch], state[idx]
for move in [-1, 1]: # horizontal switch
switch = idx + move
if idx // 3 == switch // 3: # check if row is not changed
state[idx], state[switch] = state[switch], state[idx]
neighbors.append(tuple(state))
state[idx], state[switch] = state[switch], state[idx]
return neighbors
board = sum(board, []) # flatten the board
board = tuple(board) # make tuple
target = tuple(range(9))
visited = set(tuple(board))
queue = deque([(board, 0)])
while queue:
cur, cnt = queue.popleft()
if cur == target:
return cnt
for nex in get_neighbours(cur):
if nex in visited:
continue
queue.append((nex, cnt + 1))
visited.add(nex)
return -1
View More Similar Problems
Insert a node at a specific position in a linked list
Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e
View Solution →Delete a Node
Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo
View Solution →Print in Reverse
Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing
View Solution →Reverse a linked list
Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio
View Solution →Compare two linked lists
You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis
View Solution →Merge two sorted linked lists
This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C
View Solution →