0
0
DSA Cprogramming~30 mins

Rat in a Maze Problem in DSA C - Build from Scratch

Choose your learning style9 modes available
Rat in a Maze Problem
📖 Scenario: Imagine a small rat trying to find its way through a maze to reach the cheese. The maze is a grid where some cells are open paths and others are walls. The rat can only move right or down. We want to help the rat find a path from the top-left corner to the bottom-right corner.
🎯 Goal: Build a program that represents the maze as a 2D array, sets up a solution grid, uses a function to find a path for the rat, and finally prints the path taken by the rat through the maze.
📋 What You'll Learn
Create a 4x4 maze as a 2D array with specific open and blocked cells
Create a 4x4 solution grid initialized to zero
Write a function called solveMaze that finds a path from start to finish
Print the solution grid showing the path with 1s and other cells as 0
💡 Why This Matters
🌍 Real World
Maze solving algorithms are used in robotics for path planning and navigation in unknown environments.
💼 Career
Understanding recursive backtracking and grid traversal is important for software roles involving algorithms, game development, and AI.
Progress0 / 4 steps
1
Create the maze grid
Create a 4x4 integer array called maze with these exact values: {{1, 0, 0, 0}, {1, 1, 0, 1}, {0, 1, 0, 0}, {1, 1, 1, 1}}
DSA C
Hint

Use a 2D array with 4 rows and 4 columns. Each row is enclosed in curly braces and separated by commas.

2
Create the solution grid
Create a 4x4 integer array called solution and initialize all elements to 0
DSA C
Hint

Use int solution[4][4] = {0}; to initialize all elements to zero.

3
Write the solveMaze function
Write a function called solveMaze that takes maze, solution, x, and y as parameters and returns 1 if a path is found, 0 otherwise. The function should check if the current cell is safe, mark it in solution, and recursively try moving right or down.
DSA C
Hint

Use recursion to try moving right (x+1, y) and down (x, y+1). Mark the path in solution and backtrack if needed.

4
Print the solution path
Call solveMaze with maze, solution, 0, 0 and then print the solution grid row by row, with spaces between numbers.
DSA C
Hint

Call solveMaze(maze, solution, 0, 0). If it returns 1, print the solution array row by row with spaces between numbers.