1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
#include <iostream>
#include <conio.h>
#include <iomanip>
#define FALSE 0
#define TRUE 1
#define NROWS 3
#define MCOLS 3
using namespace std;
static int steps;
// Symbols:
// '.' = open
// 'S' = start
// 'G' = goal
// '+' = path
char maze[NROWS][MCOLS] =
{
{'S','.','.'},
{'.','.','.'},
{'.','.','G'}
};
void display_maze(void);
int find_path(int x, int y);
int main()
{
display_maze();
if ( find_path(0, 0) == TRUE )
{
cout<<"Success!\n";
}
else
cout<<"Failed\n";
display_maze();
cout<<"Steps"<<steps;
getch();
return 0;
}
// main()
void
display_maze(void)
{
cout<<"MAZE:\n";
for(int a=0;a<NROWS;a++)
{
for(int b=0;b<MCOLS;b++)
{ cout<<setw(3);
cout<<maze[a][b];
}
cout<<endl;}
return;
}
int
find_path(int x, int y)
{
// If x,y is outside maze, return false.
if ( x < 0 || x > MCOLS - 1 || y < 0 || y > NROWS - 1 ) return FALSE;
// If x,y is the goal, return true.
if ( maze[y][x] == 'G')
{
return TRUE;}
// If x,y is not open, return false.
if ( maze[y][x] != '.' && maze[y][x] != 'S' ) return FALSE;
// Mark x,y part of solution path.
maze[y][x] = '+';
steps++;
label:
// If find_path North of x,y is true, return true.
if ( find_path(x, y - 1) == TRUE ) return TRUE;
// If find_path East of x,y is true, return true.
if ( find_path(x + 1, y) == TRUE ) return TRUE;
// If find_path South of x,y is true, return true.
if ( find_path(x, y + 1) == TRUE ) return TRUE;
// If find_path West of x,y is true, return true.
if ( find_path(x - 1, y) == TRUE ) return TRUE;
//mark the repeat steps
maze[y][x] = 'x';
return FALSE;
}#include <iostream>
#include <conio.h>
#include <iomanip>
#define FALSE 0
#define TRUE 1
#define NROWS 3
#define MCOLS 3
using namespace std;
static int steps;
// Symbols:
// '.' = open
// '#' = blocked
// 'S' = start
// 'G' = goal
// '+' = path
// 'x' = bad path
char maze[NROWS][MCOLS] =
{
{'S','.','.'},
{'.','.','.'},
{'.','.','G'}
};
void display_maze(void);
int find_path(int x, int y);
int main()
{
display_maze();
if ( find_path(0, 0) == TRUE )
{
cout<<"Success!\n";
}
else
cout<<"Failed\n";
display_maze();
cout<<"Steps"<<steps;
getch();
return 0;
}
// main()
void
display_maze(void)
{
cout<<"MAZE:\n";
for(int a=0;a<NROWS;a++)
{
for(int b=0;b<MCOLS;b++)
{ cout<<setw(3);
cout<<maze[a][b];
}
cout<<endl;}
return;
}
int
find_path(int x, int y)
{
// If x,y is outside maze, return false.
if ( x < 0 || x > MCOLS - 1 || y < 0 || y > NROWS - 1 ) return FALSE;
// If x,y is the goal, return true.
if ( maze[y][x] == 'G')
{
return TRUE;}
// If x,y is not open, return false.
if ( maze[y][x] != '.' && maze[y][x] != 'S' ) return FALSE;
// Mark x,y part of solution path.
maze[y][x] = '+';
steps++;
label:
// If find_path North of x,y is true, return true.
if ( find_path(x, y - 1) == TRUE ) return TRUE;
// If find_path East of x,y is true, return true.
if ( find_path(x + 1, y) == TRUE ) return TRUE;
// If find_path South of x,y is true, return true.
if ( find_path(x, y + 1) == TRUE ) return TRUE;
// If find_path West of x,y is true, return true.
if ( find_path(x - 1, y) == TRUE ) return TRUE;
//mark the repeat steps
maze[y][x] = 'x';
return FALSE;
}
|