Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program solution;
- {$APPTYPE CONSOLE}
- {MODE DELPHI}
- {$MAXSTACKSIZE 1000000000}
- {O-,R+,Q+}
- {$O+,R-,Q-}
- uses
- SysUtils,
- Math;
- const
- inf = round(1e9);
- var
- x, y, ans, res, k, i, j, z : integer;
- used : array[0..9, 0..9] of boolean;
- a : array[0..9, 0..9] of char;
- dx, dy : array[0..5] of integer;
- ax, ay : array[0..301] of integer;
- ach : array[0..301] of char;
- procedure swap(var a, b : char);
- var c : char;
- begin
- c := a;
- a := b;
- b := c;
- end;
- procedure getch(var ch : char);
- begin
- read(ch);
- while not (ch in ['a'..'z', 'A'..'Z']) do
- read(ch);
- end;
- function good(x, y : integer) : boolean;
- begin
- result := (x >= 1) and (x <= 8) and (y >= 1) and (y <= 8);
- end;
- function calc() : integer;
- var sz, x, y, i, j, z : integer;
- begin
- for i := 1 to 8 do
- for j := 1 to 8 do
- used[i, j] := false;
- for i := 1 to 8 do
- for j := 1 to 8 do
- for z := 1 to 4 do begin
- sz := 1;
- x := i;
- y := j;
- while (good(x + dx[z], y + dy[z])) and (a[x + dx[z], y + dy[z]] = a[i, j]) do begin
- inc(sz);
- x := x + dx[z];
- y := y + dy[z];
- end;
- if sz >= 3 then begin
- used[i, j] := true;
- x := i;
- y := j;
- while (good(x + dx[z], y + dy[z])) and (a[x + dx[z], y + dy[z]] = a[i, j]) do begin
- x := x + dx[z];
- y := y + dy[z];
- used[x, y] := true;
- end;
- end;
- end;
- result := 0;
- for i := 1 to 8 do
- for j := 1 to 8 do
- if used[i, j] then
- inc(result);
- end;
- function less(a, b : integer) : boolean;
- begin
- if ax[a] <> ax[b] then begin
- result := ax[a] < ax[b];
- exit;
- end;
- if ay[a] <> ay[b] then begin
- result := ay[a] < ay[b];
- exit;
- end;
- result := ach[a] < ach[b];
- end;
- begin
- //assign(input, 'input.txt'); reset(input);
- //assign(output, 'output.txt'); rewrite(output);
- dx[1] := -1;
- dy[2] := 1;
- dx[3] := 1;
- dy[4] := -1;
- for i := 1 to 8 do
- for j := 1 to 8 do
- getch(a[i, j]);
- ans := -inf;
- for i := 1 to 8 do
- for j := 1 to 8 do
- for z := 1 to 4 do begin
- x := i + dx[z];
- y := j + dy[z];
- if good(x, y) then begin
- swap(a[i, j], a[x, y]);
- res := calc();
- swap(a[i, j], a[x, y]);
- if res > ans then begin
- ans := res;
- k := 1;
- if i = x then begin
- ax[1] := i - 1;
- ay[1] := min(j, y) - 1;
- ach[1] := 'R';
- end
- else begin
- ax[1] := min(i, x) - 1;
- ay[1] := j - 1;
- ach[1] := 'D';
- end;
- end
- else
- if res = ans then begin
- inc(k);
- if i = x then begin
- ax[k] := i - 1;
- ay[k] := min(j, y) - 1;
- ach[k] := 'R';
- end
- else begin
- ax[k] := min(i, x) - 1;
- ay[k] := j - 1;
- ach[k] := 'D';
- end;
- end;
- end;
- end;
- for i := 1 to k - 1 do
- for j := k downto i + 1 do
- if less(j, i) then begin
- z := ax[i];
- ax[i] := ax[j];
- ax[j] := z;
- z := ay[i];
- ay[i] := ay[j];
- ay[j] := z;
- swap(ach[i], ach[j]);
- end;
- writeln(ans);
- writeln(ax[1], ' ', ay[1], ' ', ach[1]);
- for i := 2 to k do
- if less(i - 1, i) then
- writeln(ax[i], ' ', ay[i], ' ', ach[i]);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement