-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabirynt.cpp
More file actions
178 lines (140 loc) · 3.02 KB
/
Labirynt.cpp
File metadata and controls
178 lines (140 loc) · 3.02 KB
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
#include<iostream>
#include<fstream>
#include<iomanip>
#include"Stos.h"
using namespace std;
#define DEBUG
#ifdef DEBUG
ostream& debug = cout;
#else
ostream debug = ostream(nullptr);
#endif
struct Punkt {
int value = 0;
bool odwiedzone = false;
int x = 0;
int y = 0;
};
Punkt** szukaj_wyjscia(Punkt** maze, int width, int height,int startX,int startY,int koniecX, int koniecY)
{
Node<Punkt>* stos = nullptr;
stos->add(stos,maze[1][0]);
int row = startY;
int col = startX;
while (!stos->czy_pusty(stos))
{
bool czyMaPrzejscie = false;
col = stos->object.x;
row = stos->object.y;
maze[row][col].value = 100;
maze[row][col].odwiedzone = true;
if (row == koniecY && col == koniecX)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
debug << setw(4) << maze[i][j].value << " ";
}
debug << '\n';
}
return maze;
}
if (row + 1 < height)
{
if (maze[row + 1][col].value == 255 && !maze[row + 1][col].odwiedzone)
{
stos->add(stos, maze[row + 1][col]);
czyMaPrzejscie = true;
}
}
if (row - 1 >= 0)
{
if (maze[row - 1][col].value == 255 && !maze[row - 1][col].odwiedzone)
{
stos->add(stos, maze[row - 1][col]);
czyMaPrzejscie = true;
}
}
if (col + 1 < width)
{
if (maze[row][col+1].value == 255 && !maze[row][col + 1].odwiedzone)
{
stos->add(stos, maze[row][col+1]);
czyMaPrzejscie = true;
}
}
if (col - 1 >= 0)
{
if (maze[row][col-1].value == 255 && !maze[row][col-1].odwiedzone)
{
stos->add(stos, maze[row][col - 1]);
czyMaPrzejscie = true;
}
}
if (!czyMaPrzejscie)
{
maze[row][col].value = 50;
stos->remove(stos);
}
}
}
void zapisz_obrazek(Punkt**maze, const char* plik_wyjsciowy, int width, int height)
{
fstream file;
file.open(plik_wyjsciowy, ios::out | ios::binary);
if (!file.good()) return;
file << "P5" << '\n' << width << " " << height << '\n' << "255\n";
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
file.put(maze[i][j].value);
}
}
file.close();
}
int main(int argc,char*argv[])
{
fstream file;
file.open(argv[1], fstream::in);
if (!file.good()) return 0;
string format = "";
int width = 0;
int height = 0;
int maxValue = 0;
file >> format;
file >> width >> height;
file >> maxValue;
Punkt** maze = new Punkt* [height];
file.ignore();
for (int i = 0; i < height; i++)
{
*(maze + i) = new Punkt[width];
for (int j = 0; j < width; j++)
{
unsigned char pixel;
file.read(reinterpret_cast<char*>(&pixel), sizeof(pixel));
int value = static_cast<int>(pixel);
*(*(maze + i) + j) = Punkt{ value,false,j,i };
}
}
file.close();
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
debug << setw(4) << maze[i][j].value << " ";
}
debug << '\n';
}
debug << '\n';
maze = szukaj_wyjscia(maze, width, height, 0, 1, width - 1, height - 2);
zapisz_obrazek(maze, argv[2], width, height);
for (int i = 0; i < height; i++)
{
delete[] maze[i];
}
delete[] maze;
return 0;
}