-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy path2.1.1 - The Castle.cpp
123 lines (116 loc) · 3.66 KB
/
2.1.1 - The Castle.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
/*
ID: nathan.18
TASK: castle
LANG: C++
*/
#include <iostream>
#include <string>
#include <utility>
#include <sstream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <fstream>
using namespace std;
#define INF 1000000000
#define LL_INF 4500000000000000000
#define LSOne(S) (S & (-S))
#define EPS 1e-9
#define A first
#define B second
#define mp make_pair
#define PI acos(-1.0)
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int m, n;
bool passable[50][50][4]; // north, east, south, west
int room[50][50];
int roomSize[2501];
int biggestOriginalRoom = 0;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
void floodfill(int i, int j, int color) {
if (i < 0 || i >= m || j < 0 || j >= n || room[i][j] != -1) return;
room[i][j] = color;
roomSize[color]++;
biggestOriginalRoom = max(biggestOriginalRoom, roomSize[color]);
for (int x = 0; x < 4; x++) {
if (passable[i][j][x]) {
floodfill(i + dy[x], j + dx[x], color);
}
}
}
int main() {
freopen("castle.in", "r", stdin);
freopen("castle.out", "w", stdout);
cin >> n >> m;
memset(passable, true, sizeof passable);
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) {
int x; cin >> x;
if (x >= 8) {
x -= 8;
passable[i][j][2] = false;
}
if (x >= 4) {
x -= 4;
passable[i][j][1] = false;
}
if (x >= 2) {
x -= 2;
passable[i][j][0] = false;
}
if (x >= 1) {
x -= 1;
passable[i][j][3] = false;
}
}
memset(room, -1, sizeof room);
memset(roomSize, 0, sizeof roomSize);
int roomCounter = 0;
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) {
if (room[i][j] == -1) {
floodfill(i, j, ++roomCounter);
}
}
int largestRoom = 0;
pair<int, ii> wallToRemove = mp(INF, mp(-1, -1)); // direction (0 = north, 1 = east...), x, y
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) {
for (int x = 0; x < 2; x++) {
int i1 = i+dy[x], j1 = j+dx[x];
if (i1 < 0 || i1 >= m || j1 < 0 || j1 >= n) continue;
int a = room[i][j], b = room[i1][j1];
if (a != b) {
int combined = roomSize[a] + roomSize[b];
if (combined > largestRoom) {
largestRoom = combined;
wallToRemove = mp(x, mp(i, j));
} else if (combined == largestRoom) {
if (wallToRemove.B.B > j) { // farthest to west
wallToRemove = mp(x, mp(i, j));
} else if (wallToRemove.B.B == j) {
if (wallToRemove.B.A < i) { // farthest to south
wallToRemove = mp(x, mp(i, j));
} else if (wallToRemove.B.A == i) {
if (wallToRemove.A == 1) { // choose north before east
wallToRemove = mp(x, mp(i, j));
}
}
}
}
}
}
}
cout << roomCounter << endl;
cout << biggestOriginalRoom << endl;
cout << largestRoom << endl;
cout << wallToRemove.B.A+1 << " " << wallToRemove.B.B+1 << " " << (wallToRemove.A == 0 ? 'N' : 'E') << endl;
return 0;
}