-
Notifications
You must be signed in to change notification settings - Fork 0
/
MazeCompressor.cpp
58 lines (56 loc) · 1.57 KB
/
MazeCompressor.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
#include "MazeCompressor.h"
std::string MazeCompressor::Compress(const Maze2D& maze)
{
std::string str;
std::string extended_str;
char** temp_maze = maze.getMaze();
for (int i = 0; i < maze.getHeight(); i++)
for (int j = 0; j < maze.getWidth(); j++)
extended_str += temp_maze[i][j];
char temp_char = '\0';
int counter = 0;
str += std::to_string(maze.getHeight()) + " " + std::to_string(maze.getWidth());
for (char c : extended_str)
{
if (temp_char != c)
{
if (temp_char != '\0')
{
str += " ";
str += temp_char;
str += std::to_string(counter);
}
counter = 1;
temp_char = c;
}
else
counter++;
}
str += " ";
str += temp_char;
str += std::to_string(counter);
return str;
}
Maze2D MazeCompressor::Depress(std::string str)
{
int height = (int)std::atoi(str.substr(0 , str.find(" ")).c_str());
str = str.substr(str.find(" ") + 1, str.size() - str.find(" "));
int width = std::atoi(str.substr(0, str.find(" ")).c_str());
str = str.substr(str.find(" ") + 1, str.size() - str.find(" "));
Maze2D* maze = new Maze2D(height , width);
std::string extended_str;
while (1)
{
std::string temp_str = str.substr(1, str.find(" "));
for (int i = 0; i < std::atoi(temp_str.c_str()); i++)
extended_str += str[0];
if (str.find(" ") == str.npos)
break;
str = str.substr(str.find(" ") + 1, str.size() - str.find(" "));
}
char** temp_maze = maze->getMaze();
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
temp_maze[i][j] = extended_str[i * height + j];
return *maze;
}