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 :



title-img




                        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

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 →

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →

Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →

Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →