Dsa
Backtracking
Medium
Coloring the Grid
You are given a grid of size N x N, initially empty (represented by 0). Your task is to fill this grid with colors represented by integers from 1 to K. You can only color a cell if it is adjacent (horizontally or vertically) to the same color. Your goal is to count the number of valid ways to completely fill the grid. The input format is as follows:
Input:
N K
Where N (1 <= N <= 10) is the size of the grid and K (1 <= K <= 5) is the number of colors.
Output:
An integer representing the number of valid ways to fill the grid.
Example:
Input:
2 2
Output:
8
Key concepts
backtrackinggridcoloring
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now