Sie sind auf Seite 1von 4
Path Finder ============================================ #include <conio.h> #include <string.h>
Path Finder
============================================
#include <conio.h>
#include <string.h>
#include <iostream.h>
char GetChoice();
void GetDimensions(int& r, int& c);
void InputMaze(int r, int c, Maze& l);
void PathFinder(int r, int c, Maze& l, char mark, Boolean& found);
int main()
{
char choice = 'Y';
int rows;
int cols;
char flag = 'a';
while (choice == 'Y' || choice == 'y')
{
Boolean pathFound = FALSE;
GetDimensions(rows, cols);
Maze lab(rows, cols);
InputMaze(rows, cols, lab);
int startRow = 1;
int startCol = 1;
PathFinder(startRow, startCol, lab, flag, pathFound);
if (pathFound)
{
cout << lab
<< endl;
}
else
{
cout << "NO PATH EXISTS!"<< endl;
}
choice = GetChoice();
}
return (1); } char GetChoice() { char ch; cout << "Continue? (Y or N): ";
return (1);
}
char GetChoice()
{
char ch;
cout << "Continue? (Y or N): ";
cin >> ch;
cout << endl;
cin.ignore(50, '\n');
return (ch);
}
void GetDimensions(int& r, int& c)
{
cout << "Enter number of rows desired (MAX = 25): ";
cin >> r;
cout << "Enter number of columns desired (MAX = 25): ";
cin >> c;
cout << endl;
cin.ignore(50, '\n');
return;
}
void InputMaze(int r, int c, Maze& l)
{
cout << "Specify maze as "
<< r
<< " lines of "
<< c
<< " chars ('.' or '*'): "
<< endl;
cin >> l;
cout << endl; return; } void PathFinder(int r, int c, Maze& l, char mark, Boolean&
cout << endl;
return;
}
void PathFinder(int r, int c, Maze& l, char mark, Boolean& found)
{
if (l.is_open(r, c))
{
l.mark(r, c, mark);
if (r == l.max_row() && c == l.max_col())
{
found = TRUE;
return;
}
mark++;
if (mark > 'z')
mark = 'a';
if (c < l.max_col())
{
PathFinder(r, c+1, l, mark, found);
if (found)
return;
}
if (r < l.max_row())
{
PathFinder(r+1, c, l, mark, found);
if (found)
return;
}
if (c > 1)
{
PathFinder(r, c-1, l, mark, found);
if (found)
return;
} if (r > 1) { PathFinder(r-1, c, l, mark, found); if (found) return; }
}
if (r > 1)
{
PathFinder(r-1, c, l, mark, found);
if (found)
return;
}
l.unmark(r, c);
}
else
return;
}
http://www.ravianeducation.blogspot.com
FARHAN: 03008855006