-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathW1_C7.cpp
More file actions
80 lines (67 loc) · 1.42 KB
/
W1_C7.cpp
File metadata and controls
80 lines (67 loc) · 1.42 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
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>
int main()
{
std::ifstream ipfl;
ipfl.open("input.txt");
std::map<char, std::pair<int, int>> keyMap;
std::pair<std::string, int> langMap[3];
int width, height;
ipfl >> width >> height;
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
char c;
ipfl >> c;
keyMap[c] = { w, h };
}
}
std::string str("%TEMPLATE-START%");
std::string end("%TEMPLATE-END%");
for (int f = 0; f < 3; f++)
{
ipfl >> langMap[f].first;
std::string langCode = "";
while (1)
{
std::string temp = "";
ipfl >> temp;
if (temp.find(str) != std::string::npos)
continue;
if (temp.find(end) != std::string::npos)
break;
langCode += temp;
}
int tsum = 0;
for (int i = 1; i < langCode.length(); i++)
{
int x1 = keyMap[langCode[i - 1]].first;
int y1 = keyMap[langCode[i - 1]].second;
int x2 = keyMap[langCode[i]].first;
int y2 = keyMap[langCode[i]].second;
int diff = std::max(abs(x1 - x2), abs(y1 - y2));
tsum += diff;
}
langMap[f].second = tsum;
}
std::string mlan = "";
double gmin = std::numeric_limits<int>::max();
for (int b = 0; b < 3; b++)
{
if (langMap[b].second < gmin)
{
gmin = langMap[b].second;
mlan = langMap[b].first;
}
}
ipfl.close();
std::ofstream opfl;
opfl.open("output.txt");
opfl << mlan << "\n" << gmin;
opfl.close();
return 0;
}