-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
155 lines (124 loc) · 4.84 KB
/
main.cpp
File metadata and controls
155 lines (124 loc) · 4.84 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
#include "visualizer.h"
#include <iostream>
#include <fstream>
#include <assert.h>
#include <algorithm>
#include <stdio.h>
#include <utility>
#include <vector>
#include "utility.hpp"
std::ifstream::pos_type fileSize(const char* filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
if (!in.good())
{
throw std::exception();
}
return in.tellg();
}
std::vector<uint8_t> loadFile(const char* fileName, size_t expectedFileSize)
{
size_t fsize = fileSize(fileName);
if (fsize != expectedFileSize)
{
throw std::exception();
}
std::vector<uint8_t> data(fsize);
std::ifstream ifile(fileName, std::ifstream::binary);
if (!ifile.good())
{
throw std::exception();
}
ifile.read((char*)&data[0], fsize);
return data;
}
bool donut(int x, int y, int x1, int y1)
{
int dx = x - x1;
int dy = y - y1;
int r2 = dx * dx + dy * dy;
return r2 >= 150 && r2 <= 400;
}
int main(int argc, char** argv)
{
printf("%s\n", argv[0]);
const size_t expectedFileSize = IMAGE_DIM * IMAGE_DIM;
auto elevation = loadFile("assets/elevation.data", expectedFileSize);
auto overrides = loadFile("assets/overrides.data", expectedFileSize);
std::ofstream of("pic.bmp");
maze m = make_maze(IMAGE_DIM, IMAGE_DIM, overrides, elevation);
vertex_descriptor roverPos = vertex((ROVER_X+ROVER_Y*IMAGE_DIM), m.m_grid);
vertex_descriptor humanPos = vertex((BACHELOR_X+BACHELOR_Y*IMAGE_DIM), m.m_grid);
vertex_descriptor weddingPos = vertex((WEDDING_X+WEDDING_Y*IMAGE_DIM), m.m_grid);
if (m.solve(roverPos, humanPos))
std::cout << "Rover has reached the bachelor!" << std::endl;
else
std::cout << "Rover cacn't reach the bachelor." << std::endl;
std::set<std::pair<size_t, size_t>> path1;
double pathTime1 = 0;
for(auto elem = m.m_solution.begin(); elem != m.m_solution.end(); ++elem)
{
if(std::next(elem,1) != m.m_solution.end())
pathTime1 += m.timeWeight(*elem, *std::next(elem,1), elevation);
path1.insert(std::make_pair(elem->at(0), elem->at(1)));
}
std::cout << "Time taken by rover to reach the bachelor is " << pathTime1 << " island seconds."<<std::endl;
std::cout<<std::endl;
m.m_solution.clear();
//Wedding party -> Bachelor to Wedding
if (m.solve(humanPos, weddingPos))
std::cout << "The bachelor has reached his wedding!" << std::endl;
else
std::cout << "Looks like the bachelor stays a bachelor for a while longer." << std::endl;
std::set<std::pair<size_t, size_t>> path2;
double pathTime2 = 0;
for(auto elem = m.m_solution.begin(); elem != m.m_solution.end(); ++elem)
{
if(std::next(elem,1) != m.m_solution.end())
pathTime2 += m.timeWeight(*elem, *std::next(elem,1), elevation);
path2.insert(std::make_pair(elem->at(0), elem->at(1)));
}
std::cout << "Time taken by the rover to reach the wedding from the bachelor's position is " << pathTime2 << " island seconds."<<std::endl;
std::cout<<std::endl;
double totalTime = pathTime1+pathTime2;
std::cout << "Total time taken = " << totalTime << " island seconds. "<<std::endl;
int minutes = totalTime/60;
int hours = minutes/60;
std::cout << "Or in island time, the entire trip took " << int(hours%60) << " hours " << int(minutes%60)
<< " minutes " << int(totalTime)%60 << " seconds."<<std::endl;
std::cout<<std::endl;
std::cout << "You can open the map with $feh pic.bmp or $edisplay pic.bmp on Linux systems." <<std::endl;
visualizer::writeBMP(
of,
&elevation[0],
IMAGE_DIM,
IMAGE_DIM,
[&] (size_t x, size_t y, uint8_t elevation) {
//Path
//if(std::find(path1.begin(), path1.end(), std::make_pair(x,y)) != path1.end())
//{
// return uint8_t(visualizer::IPV_PATH);
//}
// Marks interesting positions on the map
if (donut(x, y, ROVER_X, ROVER_Y) ||
donut(x, y, BACHELOR_X, BACHELOR_Y) ||
donut(x, y, WEDDING_X, WEDDING_Y) || path1.find(std::make_pair(x,y)) != path1.end()|| path2.find(std::make_pair(x,y)) != path2.end())
{
return uint8_t(visualizer::IPV_PATH);
}
// Signifies water
if ((overrides[y * IMAGE_DIM + x] & (OF_WATER_BASIN | OF_RIVER_MARSH)) ||
elevation == 0)
{
return uint8_t(visualizer::IPV_WATER);
}
// Signifies normal ground color
if (elevation < visualizer::IPV_ELEVATION_BEGIN)
{
elevation = visualizer::IPV_ELEVATION_BEGIN;
}
return elevation;
});
//system("edisplay pic.bmp");
return 0;
}