📖 Scenario: You are helping a chess player find all the ways to place 4 queens on a 4x4 chessboard so that no two queens attack each other. Queens can attack horizontally, vertically, and diagonally. Your task is to write a program that finds all safe arrangements of queens on the board.
🎯 Goal: Build a program that uses backtracking to place 4 queens on a 4x4 board safely and prints all valid board arrangements.
📋 What You'll Learn
Create a 4x4 board represented as a 2D array of integers initialized to 0
Create a function isSafe that checks if placing a queen at a given row and column is safe
Create a recursive function solveNQueens that tries to place queens column by column
Print all valid board arrangements with queens represented by 1 and empty spaces by 0
💡 Why This Matters
🌍 Real World
The N Queens problem is a classic example of constraint satisfaction problems used in artificial intelligence and optimization tasks.
💼 Career
Understanding backtracking and recursion helps in solving complex algorithmic problems in software development and technical interviews.
Progress0 / 4 steps
1
Create the 4x4 chessboard
Create a 4x4 integer array called board and initialize all elements to 0.
DSA C
Hint
Use int board[4][4] = {0}; to create and initialize the board.
2
Write the isSafe function
Write a function int isSafe(int board[4][4], int row, int col) that returns 1 if placing a queen at board[row][col] is safe, otherwise returns 0. Check the row on left side, upper diagonal on left side, and lower diagonal on left side.
DSA C
Hint
Check left side row, upper diagonal, and lower diagonal for any queen (value 1).
3
Write the solveNQueens recursive function
Write a recursive function int solveNQueens(int board[4][4], int col) that tries to place queens column by column. If all queens are placed, return 1. For each row in the current column, check if it is safe using isSafe. If safe, place the queen (set to 1), recursively call solveNQueens for next column, and if it returns 1, return 1. Otherwise, backtrack by removing the queen (set to 0). If no row works, return 0.
DSA C
Hint
Try placing queens column by column and backtrack if needed.
4
Print the board and run the solver
Write a function void printBoard(int board[4][4]) that prints the board with 1s and 0s. Then in main, call solveNQueens(board, 0). If it returns 1, call printBoard(board) to display the solution.
DSA C
Hint
Print each row with spaces between numbers and a newline after each row. Call solveNQueens(board, 0) in main and print the board if solution found.
Practice
(1/5)
1. What is the main goal of the N Queens Problem?
easy
A. Place N queens on an NxN chessboard so that all queens attack each other
B. Find the shortest path for a queen to move across the board
C. Count the total number of queens on the board
D. Place N queens on an NxN chessboard so that no two queens attack each other
Solution
Step 1: Understand the problem statement
The N Queens Problem requires placing N queens on an NxN chessboard so that no two queens threaten each other.
Step 2: Identify the correct goal
This means no two queens share the same row, column, or diagonal.
Final Answer:
Place N queens on an NxN chessboard so that no two queens attack each other -> Option D
Quick Check:
Goal = Place queens safely [OK]
Hint: Remember: Queens must not attack each other [OK]
Common Mistakes:
Thinking queens can attack each other
Confusing the problem with counting queens
Ignoring diagonal attacks
2. Which of the following is the correct way to declare a 2D array for the chessboard in C for the N Queens Problem with size N?
easy
A. int board[];
B. int board[N][N];
C. int board[N];
D. int board(N,N);
Solution
Step 1: Recall C syntax for 2D arrays
In C, a 2D array is declared as type name[size1][size2]; where both sizes are specified.
Step 2: Match the correct declaration
int board[N][N]; uses int board[N][N]; which correctly declares a 2D array for the chessboard.
Final Answer:
int board[N][N]; -> Option B
Quick Check:
2D array syntax = int board[N][N]; [OK]
Hint: Use int board[N][N] for 2D arrays in C [OK]
Common Mistakes:
Using 1D array for 2D board
Incorrect syntax like int board(N,N);
Not specifying both dimensions
3. What will be the output of the following code snippet for N=4 when printing the board after placing queens at positions (0,1), (1,3), (2,0), (3,2)?