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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
| Type _TaskList = (_update, _remove);
type _strategy = (_search, _lookingforCenter, _lookingforBounds, _notDefined,
_lookingforCool);
const maxcol=45;
maxrow=41;
bound_value=400;
diag = 2300;
const maxlev : longint = 3;
Type Table = array [0..maxrow+1, 0..maxcol+1] of integer;
var c : integer;
t : table;
qre : integer;
strategy : _strategy;
zone : table;
gluk, was : table;
current, move, curmax, maxmove : longint;
flower : integer;
maxx,curr : array [0..20] of longint;
GameState : Longint;
procedure clear(y, x, flower, setcol : byte);
begin
if (t[y][x]=0) then exit;
if was[y][x]=1 then exit;
was[y][x] := 1;
if (t[y][x]=flower) then begin
t[y][x] := setcol;
clear(y, x+1,flower, setcol);
clear(y, x-1,flower, setcol);
clear(y-1, x,flower, setcol);
clear(y+1, x,flower, setcol);
exit;
end;
end;
procedure Fill(y, x, col : byte);
begin
if (t[y][x]=0) then exit;
if t[y][x]=col then exit;
if was[y][x]=1 then exit;
was[y][x] := 1;
fill(y, x+1, col );
fill(y, x-1, col);
fill(y-1, x, col);
fill(y+1, x, col);
exit;
end;
procedure search(lev : integer);
var
color : integer;
qqr, i : integer;
savc, curc : integer;
_task : _TaskList;
frc, toc : integer;
ccr : longint;
function get(x, y, lev : longint) : longint;
begin
get := x+y;
{ if (x=maxCOl) then get := 10000*y*(maxlev-lev);
if (y=maxRow) then Get := 10000*x*(maxlev-lev);}
end;
procedure qsearch(y, x : integer);
begin
if was[y, x] = 1 then exit;
if (t[y][x]=toc) and (_task=_update) then begin
was[y][x] := 1;
inc(current, get(x, y,lev)); zone[y][x] := lev; inc(qre);
qsearch(y, x+1);
qsearch(y, x-1);
qsearch(y-1, x);
qsearch(y+1, x);
end;
end;
procedure update(y, x : byte);
begin
if (t[y][x]=0) then exit;
if was[y][x]=1 then exit;
was[y][x] := 1;
if (t[y][x]=frc) then begin
if (_task = _update) or (zone[y][x]<>lev) then begin
t[y][x] := toc;
if zone[y][x] = -1 then zone[y][x] :=-2;
end else begin
dec(current, get(x, y,lev)); zone[y][x] := -1; dec(qre);
end;
update(y, x+1);
update(y, x-1);
update(y-1, x);
update(y+1, x);
exit;
end;
if _task = _update then
if t[y][x]=toc then
begin
inc(current, get(x, y,lev)); zone[y][x] := lev; inc(qre);
qsearch(y, x+1);
qsearch(y, x-1);
qsearch(y-1, x);
qsearch(y+1, x);
end;
end;
procedure makeit(t : _TaskList; frmcol, tocol : integer);
begin
_task := t;
frc := frmcol;
toc := tocol;
fillchar(was, sizeof(was), 0);
update(1, 1);
end;
begin
if lev = maxlev then begin
for i := maxlev-1 downto 0 do begin
if maxx[i] > curr[i] then break;
if maxx[i] < curr[i] then break;
end;
if Curr[i] > Maxx[i] then
begin Maxx := Curr; MaxMove := Move; end;
exit;
end;
savc := t[1][1];
for i := 1 to 6 do
begin
if (i<>savc) and ((i<>flower) or (lev<>0)) then begin
if lev = 0 then
move := i;
ccr := current;
qqr := qre;
makeit(_update, savc, i);
curr[lev] := current;
search(lev+1);
makeit(_remove, i, savc);
end;
end;
end;
{--------------------------------------------------------------------}
procedure mark(var a : table; _type : integer; var b : table; col : integer);
var i, j : integer;
begin
for i := 1 to MaxRow do
for j := 1 to MaxCol do begin
if a[i][j] = _type then b[i][j] := col;
end;
end;
{--------------------------------------------------------------------}
procedure AnalyzeIntPoints;
var tt : Table;
c : integer;
i, j : integer;
begin
fillchar(was, sizeof(was), 0);
fill(1, 1, 7);
mark(was, 0, t, 7);
tt := t;
fillchar(Was, sizeof(Was), 0);
clear(1, 1, t[1,1], 9);
fillchar(Was, sizeof(Was), 0);
fill (MaxRow, MaxCol, 9);
mark(t, 9, was, 1);
mark(was, 0, t, 9);
c := 0;
for i := 1 to MaxRow do
for j := 1 to MaxCol do begin
if not (t[i][j] in [7, 9]) then
c := 1;
if t[i][j] = 9 then inc(GameState);
if (t[i][j] = 9) and (i*MaxCol+j*MaxRow>diag) then Strategy:=_Lookingforbounds;
end;
if (t[Maxrow][1] in [7, 9]) and (t[1][maxCol] in [7, 9]) then
strategy := _search;
if (gamestate>900) {or ({(gamestate>300) and (gamestate<500))} then Strategy := _Search;
gluk := t;
t := tt;
if c<>0 then mark(was, 0, t, 9);
fillchar(Was, sizeof(Was), 0);
end;
var i, j : integer;
ch : char;
procedure SearchWays(var when, color : table; x, y : integer; flower : integer; badColor : integer);
var
find : boolean;
flag : boolean;
min : integer;
CHK : INTEGER;
procedure update(a, b, c, d : integer);
begin
if find then exit;
if t[a][b]=9 then exit;
if gluk[a][b]=badColor then exit; { Testing t[a][b] is neutral }
if when[c][d] = - 1 then exit;
if (when[c][d]=0) and (t[a][b]=flower) then exit;
if (when [a][b] = -1) or (when[c][d] < when [a][b]) then
begin
CHK := WHEN[A][B];
when[a][b] := when[c][d];
if t[a][b]<>t[c][d] then inc(when[a][b]);
if when[c][d]=0 then color[a][b] := t[a][b] else
color[a][b] := color[c][d];
IF CHK<>WHEN[A][B] THEN flag :=true;
end;
end;
begin
find := false;
fillchar(when, sizeof(when), 255);
when[y][x] := 0;
repeat
flag := false;
for i := 1 to maxRow do
for j := 1 to maxCol do begin
update(i,j, i-1, j);
update(i,j, i+1, j);
update(i,j, i, j-1);
update(i,j, i, j+1);
end;
until not flag;
end;
procedure FindCenter;
var when, color : table;
i, j : integer;
min : integer;
begin
searchways(when, color, 1, 1, flower, t[maxrow, maxcol]);
min := maxint;
maxmove := 0;
for i := 1 to maxRow do
for j := 1 to maxCol do begin
if (i*MaxCol+j*MaxRow > Diag) and (when[i][j] <min) and (when[i][j]<>-1) then
begin min := when[i][j]; maxmove := color[i][j] end;
end;
if maxmove = 0 then strategy := _lookingforBounds;
end;
procedure FindBounds;
var
whW, whH, colW, ColH : table;
i, j : integer;
ctr : integer;
min, max : integer;
procedure FindCool;
var i, j, k : integer;
col : integer;
begin
max := 0;
for k := 1 to 6 do begin
col := 0;
for i :=1 to MaxRow do
for j := 1 to MaxCol do begin
if (whh[i][j] - whw[i][j] > 0) and (whh[i][j] < 30000) and (colw[i][j]=k) then begin
inc(col);
end;
end;
if col > max then begin MaxMove := k; max := col ; end;
end;
end;
begin
searchways(whW, ColW, 1, 1, flower, t[maxrow, maxcol]);
fillchar(was, sizeof(was), 0);
searchways(whh, ColH, MaxCol, MaxRow, t[1, 1], 9);
mark(whH, -1, whh, MaxInt);
mark(whW, -1, whW, MaxInt);
max := 0;
maxMove := 0;
min := maxint;
ctr := 5;
for i := MaxRow downto 1 do begin
for j := 1 to i do
if whw[i][MaxCol]=0 then ctr := i-j;
if (WhH[i][MaxCol] - WhW [i][maxcol]>=1) and (whW[i][maxcol] < min) and (ctr>=10) then begin
min := whW[i][MaxCol]; maxMove := colW[i][MaxCol]; break;
end;
end;
ctr := 5;
for i := MaxCol downto 1 do begin
for j := 1 to i do
if whw[maxRow][i]=0 then ctr := i-j;
if (whH[MaxRow][i]-WhW[MaxRow][i]>=1) and (whW[MaxRow][i] <min) and (ctr >=10) then
begin min := WhW[MaxRow][i]; maxMove := colW[MaxRow][i]; break; end;
end;
if maxMove = 0 then
FindCool;
if MaxMove = 0 then Strategy := _Search;
end;
begin
assign(input, 'filya.in'); reset(input);
for i := 1 to maxrow do begin
for j := 1 to maxCol do begin
read(ch); t[i][j] := ord(ch)-48;
end;
readln;
end;
fillchar(zone, sizeof(zone), 255);
flower := t[maxRow, MaxCol];
fillchar(Was, sizeof(Was), 0);
clear(maxRow, MaxCol, t[maxRow,Maxcol], 7);
strategy := _lookingforcenter;
AnalyzeIntPoints;
fillchar(Was, sizeof(Was), 0);
if strategy=_lookingForCenter then FindCenter;
if strategy= _lookingforBounds then findbounds;
if maxmove = 0 then
for i := 1 to 6 do begin
if (i<>flower) and (i<>t[1][1]) then begin MaxMove :=i; break; end;
end;
if strategy = _Search then
begin
if gameState > 800 then MaxLev := 1;
fillchar(zone, sizeof(zone), 255);
search(0);
end;
assign(output, 'filya.out'); Rewrite(output);
writeln(maxMove);
end. |