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
|
// Programmieren 1, Praktikum 5, Aufgabe 3
// Sven Eisenhauer
// 10.01.2005
//
// file: main.cpp
//
// purpose: Find all exists of a given maze
//
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
// contains function prototypes for functions srand and rand
#include <cstdlib>
// constant definitions
const int maxLines=14;
const int maxRows=14;
const int startRow=1;
const int startLine=3;
//function prototypes
//void mazePrint(const char[][maxRows], const int);
void mazePrint(const char[][maxRows]);
void mazeTraverse(char [][maxRows],int,int,int&);
int main ()
{
int exitCount=0;
char maze[maxLines][maxRows] =
{
{'.','.','.','.','.','.','.','.','.','.','.','.','.','.'},
{'.','#','#','#','#','#','#','#','#','#','#','#','#','.'},
{'.','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ','#','.'},
{'.',' ',' ','#',' ','#',' ','#','#','#','#',' ','#','.'},
{'.','#','#','#',' ','#',' ',' ',' ',' ','#',' ','#','.'},
{'.',' ',' ',' ',' ',' ','#','#','#',' ','#',' ',' ','.'},
{'.','#','#','#','#',' ','#',' ','#',' ','#',' ','#','.'},
{'.','#',' ',' ','#',' ','#',' ','#',' ','#',' ','#','.'},
{'.','#','#',' ','#',' ','#',' ','#',' ','#',' ','#','.'},
{'.','#',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#','.'},
{'.','#','#','#','#','#','#',' ','#','#','#',' ','#','.'},
{'.','#',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#','.'},
{'.','#','#','#','#','#','#','#','#',' ','#','#','#','.'},
{'.','.','.','.','.','.','.','.','.','.','.','.','.','.'}
};
mazeTraverse(maze,startRow,startLine,exitCount);
cout << "\nNumber of Exits: " << exitCount << endl;
return 0;
}
void mazeTraverse(char laby[][maxRows],int x,int y,int &count)
{
char temp=' ';
if (x < maxLines && y < maxRows && x > 0 && y > 0)
{
if (laby[y][x] == ' ')
laby[y][x] = 'x';
if (laby[y+1][x] == '.')
{
count++;
itoa(count,&temp,10);
laby[y+1][x]=temp;
}
if (laby[y][x+1] == '.')
{
count++;
itoa(count,&temp,10);
laby[y][x+1]=temp;
}
if (laby[y-1][x] == '.')
{
count++;
itoa(count,&temp,10);
laby[y-1][x]=temp;
}
if (laby[y][x-1] == '.')
{
count++;
itoa(count,&temp,10);
laby[y][x-1]=temp;
}
//mazePrint(laby,count);
mazePrint(laby);
if (laby[y+1][x] == ' ')
mazeTraverse(laby,x,y+1,count);
if (laby[y][x+1] == ' ')
mazeTraverse(laby,x+1,y,count);
if (laby[y-1][x] == ' ')
mazeTraverse(laby,x,y-1,count);
if (laby[y][x-1] == ' ')
mazeTraverse(laby,x-1,y,count);
}
}
void mazePrint(const char array[][maxRows])
//void mazePrint(const char array[][maxRows],const int count)
{
system ("cls");
for (int i=0;i<maxLines;i++)
{
for (int j=0;j<maxRows;j++)
cout << array[i][j];
cout << "\n";
}
//cout << "\n" << count;
//system ("pause");
}
|