-
Notifications
You must be signed in to change notification settings - Fork 0
/
cslikar.cpp
172 lines (142 loc) · 2.96 KB
/
cslikar.cpp
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
#include <iostream>
#include <queue>
#include <climits>
using namespace std;
struct cell {
char ch;
int flood_time;
bool visited;
int dist;
cell() {
flood_time = INT_MAX;
visited = false;
dist = INT_MAX;
}
};
struct posn {
int r;
int c;
int flood_time;
int dist;
};
class kaktus_grid {
public :
int r;
int c;
cell grid[50][50];
queue<posn> q;
posn start;
posn end;
void update_flood_times() {
while ( !q.empty() ) {
posn top = q.front();
q.pop();
push_neighbours(top);
}
}
void push_neighbours( posn top ) {
check_adj( top, top.r + 1, top.c );
check_adj( top, top.r - 1, top.c );
check_adj( top, top.r, top.c + 1 );
check_adj( top, top.r, top.c - 1 );
}
void check_adj( posn top, int r, int c ) {
if ( r >= 0 && r < this->r && c >= 0 && c < this->c ) {
if ( grid[r][c].ch != 'X' && grid[r][c].ch != 'D' ) {
if ( grid[r][c].flood_time > top.flood_time + 1 ) {
grid[r][c].flood_time = top.flood_time + 1;
posn p;
p.r = r;
p.c = c;
p.flood_time = top.flood_time + 1;
q.push(p);
}
}
}
}
void min_path() {
update_flood_times();
q.push(start);
while ( !q.empty() ) {
posn top = q.front();
q.pop();
check_neighbours(top);
}
if ( grid[end.r][end.c].dist != INT_MAX ) {
cout << grid[end.r][end.c].dist << endl;
} else {
cout << "KAKTUS" << endl;
}
}
void check_neighbours( posn top ) {
check_adjacent(top, top.r + 1, top.c );
check_adjacent(top, top.r - 1, top.c );
check_adjacent(top, top.r, top.c + 1 );
check_adjacent(top, top.r, top.c - 1 );
}
void check_adjacent( posn top, int r, int c ) {
if ( r >= 0 && r < this->r && c >= 0 && c < this->c ) {
if ( grid[r][c].ch != 'X' ) {
if ( grid[r][c].flood_time > top.dist + 1 && grid[r][c].dist > top.dist + 1) {
grid[r][c].dist = top.dist + 1;
posn p;
p.r = r;
p.c = c;
p.dist = top.dist + 1;
q.push(p);
}
}
}
}
void print_flood_times() {
for ( int i = 0; i < r; i++ ) {
for ( int j = 0; j < c; j++ ) {
if ( grid[i][j].dist != INT_MAX ) {
cout << grid[i][j].dist << " ";
} else {
cout << "#" << " ";
}
}
cout << endl;
}
cout << endl << endl;
}
kaktus_grid( int r, int c ) {
this->r = r;
this->c = c;
for ( int i = 0; i < r; i++ ) {
for ( int j = 0; j < c; j++ ) {
cell tmp;
cin >> tmp.ch;
if ( tmp.ch == '*' ) {
tmp.flood_time = 0;
tmp.visited = true;
posn p;
p.r = i;
p.c = j;
p.flood_time = 0;
q.push(p);
} else if ( tmp.ch == 'S' ) {
start.r = i;
start.c = j;
start.dist = 0;
tmp.dist = 0;
} else if ( tmp.ch == 'D' ) {
end.r = i;
end.c = j;
}
grid[i][j] = tmp;
}
}
}
};
int main()
{
int r;
int c;
cin >> r;
cin >> c;
kaktus_grid k(r, c);
k.min_path();
return 0;
}