-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1906A.cpp
More file actions
77 lines (63 loc) · 2.03 KB
/
1906A.cpp
File metadata and controls
77 lines (63 loc) · 2.03 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
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> directions = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
vector<pair<char, pair<int, int>>> getNeighbors(
char grid[3][3], int row, int col,
pair<int, int> exclude1 = {-1, -1},
pair<int, int> exclude2 = {-1, -1}
) {
vector<pair<char, pair<int, int>>> result;
for (auto [dx, dy] : directions) {
int newRow = row + dx;
int newCol = col + dy;
pair<int, int> current = {newRow, newCol};
if (newRow >= 0 && newRow < 3 && newCol >= 0 && newCol < 3) {
if (current != exclude1 && current != exclude2) {
result.push_back({grid[newRow][newCol], current});
}
}
}
sort(result.begin(), result.end());
return result;
}
int main() {
char grid[3][3];
vector<pair<int, int>> allPositions;
char smallest = 'Z';
for (int i = 0; i < 3; i++) {
string temp;
cin >> temp;
for (int j = 0; j < 3; j++) {
grid[i][j] = temp[j];
if (grid[i][j] < smallest) {
smallest = grid[i][j];
allPositions.clear();
allPositions.push_back({i, j});
} else if (grid[i][j] == smallest) {
allPositions.push_back({i, j});
}
}
}
string result = "ZZZ";
for (auto smallestPos : allPositions) {
auto level1 = getNeighbors(grid, smallestPos.first, smallestPos.second, smallestPos);
for (auto &[char2, pos2] : level1) {
auto level2 = getNeighbors(grid, pos2.first, pos2.second, smallestPos, pos2);
for (auto &[char3, pos3] : level2) {
string temp = "";
temp += grid[smallestPos.first][smallestPos.second];
temp += char2;
temp += char3;
if (temp < result) {
result = temp;
}
}
}
}
cout << result << endl;
return 0;
}