CIJEVI - Cijevi

Tác giả: RR

Ngôn ngữ: Pascal

{$R+,Q+}
{$mode objFPC}
uses math;
const
  FINP='';
  FOUT='';
  MAXN=30;
var
  f1,f2:text;
  lu,lv,m,n,startu,startv,targetu,targetv:longint;
  lc:char;
  ok,ok2:boolean;
  a:array[1..MAXN,1..MAXN] of char;
  xet:array[-1..MAXN,-1..MAXN] of longint;
procedure openF;
begin
  assign(f1,FINP); reset(f1);
  assign(f2,FOUT); rewrite(f2);
end;
procedure closeF;
begin
  close(f1);
  close(f2);
end;
procedure inp;
var
  i,j:longint;
begin
  readln(f1,m,n);
  for i:=1 to m do
    begin
      for j:=1 to n do
        begin
          read(f1,a[i,j]);
          if a[i,j]='M' then
            begin startu:=i; startv:=j; end;
          if a[i,j]='Z' then
            begin targetu:=i; targetv:=j; end;
        end;
      readln(f1);
    end;
end;
procedure ans;
begin
  writeln(f2,lu,' ',lv,' ',lc);
end;
function check(u,v,h:longint):boolean; inline;
begin
  if (u<=0) or (v<=0) or (u>m) or (v>n) then exit(false);
  if a[u,v]='Z' then exit(true);
  if a[u,v]='M' then exit(true);
  case h of
    1: if a[u,v] in ['2','3','|','+'] then exit(true) else exit(false);
    2: if a[u,v] in ['1','2','-','+'] then exit(true) else exit(false);
    3: if a[u,v] in ['1','4','|','+'] then exit(true) else exit(false);
    4: if a[u,v] in ['3','4','-','+'] then exit(true) else exit(false);
  end;
end;
procedure visit(u,v:longint);
begin
  if (u<=0) or (v<=0) or (u>m) or (v>n) then exit;
  if xet[u,v]=1 then exit; xet[u,v]:=1;
  case a[u,v] of
    'M': begin
           if (u>1) and (a[u-1,v]<>'.') then visit(u-1,v);
           if (u<m) and (a[u+1,v]<>'.') then visit(u+1,v);
           if (v>1) and (a[u,v-1]<>'.') then visit(u,v-1);
           if (v<n) and (a[u,v+1]<>'.') then visit(u,v+1);
         end;
    '|': begin
           if ok2 then ok2:=check(u-1,v,3);
           if ok2 then ok2:=check(u+1,v,1);
           if (xet[u-1,v]=0) then visit(u-1,v);
           if (xet[u+1,v]=0) then visit(u+1,v);
         end;
    '-': begin
           if ok2 then ok2:=check(u,v-1,2);
           if ok2 then ok2:=check(u,v+1,4);
           if (xet[u,v-1]=0) then visit(u,v-1);
           if (xet[u,v+1]=0) then visit(u,v+1);
         end;
    '+': begin
           if ok2 then ok2:=check(u-1,v,3);
           if ok2 then ok2:=check(u+1,v,1);
           if ok2 then ok2:=check(u,v-1,2);
           if ok2 then ok2:=check(u,v+1,4);
           if (xet[u-1,v]=0) then visit(u-1,v);
           if (xet[u+1,v]=0) then visit(u+1,v);
           if (xet[u,v-1]=0) then visit(u,v-1);
           if (xet[u,v+1]=0) then visit(u,v+1);
         end;
    '1': begin
           if ok2 then ok2:=check(u,v+1,4);
           if ok2 then ok2:=check(u+1,v,1);
           if (xet[u,v+1]=0) then visit(u,v+1);
           if (xet[u+1,v]=0) then visit(u+1,v);
         end;
    '2': begin
           if ok2 then ok2:=check(u-1,v,3);
           if ok2 then ok2:=check(u,v+1,4);
           if (xet[u-1,v]=0) then visit(u-1,v);
           if (xet[u,v+1]=0) then visit(u,v+1);
         end;
    '3': begin
           if ok2 then ok2:=check(u-1,v,3);
           if ok2 then ok2:=check(u,v-1,2);
           if (xet[u-1,v]=0) then visit(u-1,v);
           if (xet[u,v-1]=0) then visit(u,v-1);
         end;
    '4': begin
           if ok2 then ok2:=check(u,v-1,2);
           if ok2 then ok2:=check(u+1,v,1);
           if (xet[u,v-1]=0) then visit(u,v-1);
           if (xet[u+1,v]=0) then visit(u+1,v);
         end;
    '.': begin
           lu:=u;
           lv:=v;
         end;
    'Z': ok:=true;
  end;
end;
procedure solve;
var
  tu,tv:longint;
begin
  visit(startu,startv);
  tu:=lu; tv:=lv;
  //Xet +
  ok:=false; ok2:=true; fillchar(xet,sizeof(xet),0);
  lu:=0; a[tu,tv]:='+';
  visit(startu,startv);
  if (lu=0) and ok and ok2 then
    begin lu:=tu; lv:=tv; lc:='+'; exit; end;
  //Xet |
  ok:=false; ok2:=true;
  fillchar(xet,sizeof(xet),0);
  lu:=0; a[tu,tv]:='|';
  visit(startu,startv);
  if (lu=0) and ok and ok2 then
    begin lu:=tu; lv:=tv; lc:='|'; exit; end;
  //Xet -
  ok:=false; ok2:=true;
  fillchar(xet,sizeof(xet),0);
  lu:=0; a[tu,tv]:='-';
  visit(startu,startv);
  if (lu=0) and ok and ok2 then
    begin lu:=tu; lv:=tv; lc:='-'; exit; end;
  //Xet 1
  ok:=false; ok2:=true;
  fillchar(xet,sizeof(xet),0);
  lu:=0; a[tu,tv]:='1';
  visit(startu,startv);
  if (lu=0) and ok and ok2 then
    begin lu:=tu; lv:=tv; lc:='1'; exit; end;
  //Xet 2
  ok:=false; ok2:=true;
  fillchar(xet,sizeof(xet),0);
  lu:=0; a[tu,tv]:='2';
  visit(startu,startv);
  if (lu=0) and ok and ok2 then
    begin lu:=tu; lv:=tv; lc:='2'; exit; end;
  //Xet 3
  ok:=false; ok2:=true;
  fillchar(xet,sizeof(xet),0);
  lu:=0; a[tu,tv]:='3';
  visit(startu,startv);
  if (lu=0) and ok and ok2 then
    begin lu:=tu; lv:=tv; lc:='3'; exit; end;
  //Xet 4
  ok:=false; ok2:=true;
  fillchar(xet,sizeof(xet),0);
  lu:=0; a[tu,tv]:='4';
  visit(startu,startv);
  if (lu=0) and ok and ok2 then
    begin lu:=tu; lv:=tv; lc:='4'; exit; end;
end;
begin
  openF;
  inp;
  solve;
  ans;
  closeF;
end.

Download