-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunhouse.cpp
264 lines (205 loc) · 6 KB
/
funhouse.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* @file funhouse.cpp
* @author William Weston
* @brief Fun House Problem From Kattis
* @version 0.1
* @date 2023-08-17
*
* @copyright Copyright (c) 2023
*
* Source: https://open.kattis.com/problems/funhouse
*/
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <iterator> // distance
#include <string>
#include <utility>
#include <vector>
// ------------------------------------------------------------------------------------------------
auto locate_exit( std::vector<std::string>& room ) -> void;
auto print_results( std::vector<std::string> const& room, int number ) -> void;
// --------------------------------------------- main ---------------------------------------------
auto main() -> int
{
auto house = 1;
while ( true )
{
int width, length;
std::cin >> width >> length;
if ( width == 0 && length == 0 )
break; // end of input
auto room_diagram = std::vector<std::string>();
room_diagram.reserve( length );
for ( auto line = std::string(); length--; )
{
std::getline( std::cin >> std::ws, line );
room_diagram.push_back( std::move( line ) );
}
locate_exit( room_diagram );
print_results( room_diagram, house );
++house; // next funhouse
}
return EXIT_SUCCESS;
}
// -------------------------------------------- Point ---------------------------------------------
struct Point final
{
int x;
int y;
};
auto operator<< ( std::ostream& out, Point point ) -> std::ostream&
{
return out << '(' << point.x << ", " << point.y << ')';
}
// ------------------------------------------ Exit_Finder ------------------------------------------
enum Direction { north, south, east, west };
auto operator<<( std::ostream& out, Direction direction ) -> std::ostream&
{
switch ( direction )
{
case north:
return out << "north";
case south:
return out << "south";
case east:
return out << "east";
case west:
return out << "west";
default:
assert ( true && "invalid Direction" );
return out;
}
}
class Exit_Finder final
{
public:
explicit Exit_Finder( std::vector<std::string>& room ) noexcept;
auto run() -> void;
private:
std::vector<std::string>& room_;
Point position_; // current position in room
Direction direction_; // 0 = east, 180 = west, 90 = north, 270 = south
};
auto locate_entrance( std::vector<std::string> const& room ) -> Point;
constexpr auto set_initial_direction( Point entrance, int w, int h ) -> Direction;
constexpr auto advance( Point position, Direction direction ) -> Point;
constexpr auto reflect( char mirror, Direction direction ) -> Direction;
constexpr auto is_mirror( char ch ) -> bool;
Exit_Finder::Exit_Finder( std::vector<std::string>& room ) noexcept
: room_( room )
, position_( locate_entrance( room ) )
, direction_( set_initial_direction( position_, room.front().size(), room.size() ) )
{}
auto
Exit_Finder::run() -> void
{
while ( room_[position_.y][position_.x] != 'x' )
{
position_ = advance( position_, direction_ );
auto const contents = room_[position_.y][position_.x];
if ( is_mirror( contents ) )
{
direction_ = reflect( contents, direction_ );
}
}
room_[position_.y][position_.x] = '&';
}
auto
locate_entrance( std::vector<std::string> const& room ) -> Point
{
auto const sz = room.size();
for ( std::vector<std::string>::size_type row = 0; row < sz; ++row )
{
auto const col = room[row].find( '*' );
if ( col != std::string::npos )
return Point{ static_cast<int>( col ), static_cast<int>( row ) };
}
assert( true && "Exit not found" );
return Point{ 0, 0 };
}
constexpr auto
set_initial_direction( Point const entrance, int const width, int const height ) -> Direction
{
if ( entrance.x == 0 ) return east;
if ( entrance.x == width - 1 ) return west;
if ( entrance.y == 0 ) return south;
return north;
}
constexpr auto
advance( Point const position, Direction const direction ) -> Point
{
switch ( direction )
{
case east:
return Point{ position.x + 1, position.y };
case west:
return Point{ position.x - 1, position.y };
case north:
return Point{ position.x, position.y - 1 };
case south:
return Point{ position.x, position.y + 1 };
default:
assert( true && "Unknown Direction" );
return {};
}
}
constexpr auto
reflect( char const mirror, Direction const direction ) -> Direction
{
if ( mirror == '/' )
{
switch ( direction)
{
case east:
return north;
case west:
return south;
case north:
return east;
case south:
return west;
default:
assert( true && "Unknown Direction" );
return {};
}
}
else if ( mirror == '\\' )
{
switch ( direction)
{
case east:
return south;
case west:
return north;
case north:
return west;
case south:
return east;
default:
assert( true && "Unknown Direction" );
return {};
}
}
assert( true && "unknown mirror" );
return east;
}
constexpr auto
is_mirror( char ch ) -> bool
{
return ch == '/' || ch == '\\';
}
// ------------------------------------------------------------------------------------------------
auto locate_exit( std::vector<std::string>& room ) -> void
{
auto finder = Exit_Finder( room );
finder.run();
}
auto print_results( std::vector<std::string> const& room, int number ) -> void
{
std::cout << "HOUSE " << number << '\n';
for ( auto const& line : room )
std::cout << line << '\n';
}
// ------------------------------------------------------------------------------------------------