-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcastle.cpp
167 lines (145 loc) · 4.33 KB
/
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
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
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <fstream>
#include <array>
#include <algorithm>
#include <vector>
using namespace std;
array<array<array<bool, 4>, 50>, 50> wallsorig;
array<array<array<bool, 4>, 50>, 50> walls; // West, North, East, South
array<array<bool, 50>, 50> visited;
int total = 0;
void decodewall(int x, int y, int &n, int c, int index){
if(n >= c){
walls[x][y][index] = true;
n -= c;
}
}
void cvt(int y, int x, int n){
visited[x][y] = false;
for(int i = 0; i < 4; i++){
walls[x][y][i] = false;
}
decodewall(x, y, n, 8, 3);
decodewall(x, y, n, 4, 2);
decodewall(x, y, n, 2, 1);
decodewall(x, y, n, 1, 0);
}
int recur(int x, int y, int capx, int capy){
if(visited[x][y] || x < 0 || y < 0 || x >= capx || y >= capy){
return 0;
}
visited[x][y] = true;
int num = 1;
for(int i = 0; i < 4; i++){
int dx = 0, dy = 0;
if(i == 0)
dx = -1;
if(i == 1)
dy = -1;
if(i == 2)
dx = 1;
if(i == 3)
dy = 1;
if(!walls[x][y][i])
num += recur(x + dx, y + dy, capx, capy);
}
return num;
}
void rstvisits(int M, int N){
for(int i = 0; i < M; i++)
for(int j = 0; j < N; j++)
visited[i][j] = false;
}
int main(){
ifstream fin ("castle.in");
ofstream fout ("castle.out");
int M, N;
fin >> M >> N;
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
int num;
fin >> num;
cvt(i, j, num);
}
}
wallsorig = walls;
int lroom = 0;
for(int i = 0; i < M; i++){
for(int j = 0; j < N; j++){
if(!visited[i][j]){
total++;
lroom = max(lroom, recur(i, j, M, N));
}
}
}
fout << total << endl;
fout << lroom << endl;
rstvisits(M, N);
int combinedroom = 0;
vector<int> optimalx;
vector<int> optimaly;
vector<bool> optimalN;
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
for(int k = 0; k < 4; k++){
if(walls[j][i][k]){
walls[j][i][k] = false;
int adjroom = recur(j, i, M, N);
if(adjroom >= combinedroom){
if(adjroom > combinedroom){
optimalx.clear();
optimaly.clear();
optimalN.clear();
}
combinedroom = adjroom;
if(k == 0){
optimaly.push_back(i);
optimalx.push_back(j + 1);
optimalN.push_back(false);
}
if(k == 1){
optimaly.push_back(i + 1);
optimalx.push_back(j + 1);
optimalN.push_back(true);
}
if(k == 2){
optimaly.push_back(i + 1);
optimalx.push_back(j + 1);
optimalN.push_back(false);
}
if(k == 3){
optimaly.push_back(i + 1);
optimalx.push_back(j + 2);
optimalN.push_back(true);
}
}
walls = wallsorig;
rstvisits(M, N);
}
}
}
}
int top = 51;
vector<int> topindex;
for(unsigned int i = 0; i < optimalx.size(); i++){
if(optimalx[i] <= top){
if(optimalx[i] < top)
topindex.clear();
top = optimalx[i];
topindex.push_back(i);
}
}
top = 0;
int topi = 0;
for(int i : topindex){
if(optimaly[i] > top){
top = optimaly[i];
topi = i;
}
}
char direction = 'N';
if(!optimalN[topi])
direction = 'E';
fout << combinedroom << endl << optimaly[topi] << " " << optimalx[topi] << " " << direction << endl;
return 0;
}