CIJEVI - Cijevi
Tác giả: ll931110
Ngôn ngữ: Pascal
Program CIJEVI;
Const
input = '';
output = '';
maxn = 25;
d: array[1..6,1..2,1..2] of integer = (((1,2),(4,3)),((3,2),(4,1)),((2,1),(3,4)),((2,3),(1,4)),((2,2),(4,4)),((1,1),(3,3)));
h: array[1..4,1..3] of integer = ((1,4,6),(3,4,5),(2,3,6),(1,2,5));
dx: array[1..4] of integer = (-1,0,1,0);
dy: array[1..4] of integer = (0,1,0,-1);
Var
a: array[0..maxn + 1,0..maxn + 1] of char;
t: array[1..7] of char;
r,c,cx,cy: integer;
sx,sy,fx,fy: integer;
brnum,count: integer;
check: boolean;
Procedure init;
Var
f: text;
i,j: integer;
Begin
Assign(f, input);
Reset(f);
Readln(f, r, c);
For i:= 0 to r + 1 do
For j:= 0 to c + 1 do a[i,j]:= '.';
For i:= 1 to r do
Begin
For j:= 1 to c do
Begin
read(f, a[i,j]);
If a[i,j] <> '.' then inc(brnum);
End;
Readln(f);
End;
t[1]:= '1';
t[2]:= '2';
t[3]:= '3';
t[4]:= '4';
t[5]:= '-';
t[6]:= chr(124);
t[7]:= '+';
End;
Procedure brway(var k: integer);
Var
i,u: integer;
Begin
u:= 0;
For i:= 1 to 3 do
if a[cx,cy] = t[h[k,i]] then
Begin
u:= h[k,i];
break;
End;
If u = 0 then
If a[cx,cy] <> '+' then
Begin
check:= false;
exit;
End
else
Begin
cx:= cx + dx[k];
cy:= cy + dy[k];
exit;
End;
For i:= 1 to 2 do
if k = d[u,i,1] then
Begin
k:= d[u,i,2];
cx:= cx + dx[k];
cy:= cy + dy[k];
break;
End;
End;
Function tk: boolean;
Begin
tk:= ((cx = fx) and (cy = fy)) or not check;
End;
Function getbr(k: integer): integer;
Var
i,u: integer;
tx,ty,tmp,tmpcount: integer;
Begin
tx:= cx;
ty:= cy;
For i:= 1 to 3 do
Begin
cx:= tx;
cy:= ty;
check:= true;
u:= h[k,i];
a[cx,cy]:= t[u];
tmp:= k;
tmpcount:= count;
Repeat
brway(tmp);
If not tk then dec(tmpcount);
Until ((cx = fx) and (cy = fy)) or not check;
If (cx = fx) and (cy = fy) and (tmpcount < 2) then exit(u) else a[tx,ty]:= '.';
End;
getbr:= 7;
End;
Procedure solve;
Var
f: text;
k,i,j: integer;
Begin
For i:= 1 to r do
For j:= 1 to c do
If a[i,j] = 'M' then
Begin
sx:= i;
sy:= j;
End
else if a[i,j] = 'Z' then
Begin
fx:= i;
fy:= j;
End;
For i:= 1 to 4 do
Begin
cx:= sx + dx[i];
cy:= sy + dy[i];
If (a[cx,cy] <> '.') and (a[cx,cy] <> 'Z') then
Begin
k:= i;
break;
End;
End;
count:= brnum - 2;
Repeat
brway(k);
If a[cx,cy] <> '.' then dec(count);
Until a[cx,cy] = '.';
Assign(f, output);
Rewrite(f);
Write(f, cx, ' ', cy, ' ', t[getbr(k)]);
Close(f);
End;
Begin
init;
solve;
End.