# 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;
}

Set<String> visited = new HashSet<>();

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;
}

}

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))

return -1```
```

## Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

## Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

## Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

## Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and

## Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

## No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio