The following is a two-dimensional character array of size 12X12 which represents a maze. The symbol ‘#’ represent a wall of the maze and the symbol ‘.’ represents a possible path. There is an entry point and exit point of the maze, which are denoted by a ‘.’ Write a C program which takes text file which contains the maze as command line argument and prints the minimum number of moves required to go from entry to exit. If there is no path from entry to exit display “-1”. You need to formulate the maze as a graph and use Breadth First Search to find shortest path from entry to exit. You can treat each cell as a vertex and if we can go from one cell to other cell with one move add edge between the corresponding vertices. For example, for the below maze:
############
. . . ##. ##. . .#
##. . . . . . . .##
#. .######. ##
####. . . . . . .#
# . . . .#. ###.#
###. . . . . . . .#
#. . .######. #
#####. . .#. .#
#. . .#. #. #. ##
#.#. . .#. . . . .
############
Min moves: 22
#include<conio.h>
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <cmath>
#include<dos.h>
#include <bits/stdc++.h>
#include<vector>
using namespace std;
#define ROW 5
#define COL 5
int main()
{
char maze[ROW][COL] ={
{'#','-','#','#'},
{'-','#','-','#'},
{'#','-','-','#'},
{'-','#','-','#'},
{'#','-','-','#'},
};
int r,c;
cout<<"\n\tMaze\n";
for(r=0;r<ROW;r++)
{
for(c=0;c<COL;c++)
{
cout<<"\t"<<maze[r][c];
}
cout<<endl;
}
return(0);
}
Comments
Leave a comment