天天看点

输油管道 (Standard IO)

题意/Description:

       请你帮忙设计一个从城市M到城市Z的输油管道,现在已经把整个区域划分为R行C列,每个单元格可能是空的也可能是以下7种基本管道之一:

输油管道 (Standard IO)

       油从城市M流向Z,‘+’型管道比较特殊,因为石油必须在两个方向(垂直和水平)上传输,如下图所示:

输油管道 (Standard IO)

       现在恐怖分子弄到了输油管道的设计图,并把其中一个单元格中的管道偷走了,请你帮忙找到偷走的管道的位置以及形状。

读入/Input:

       第一行包含两个整数R和C(1<=R,C<=25)。

  接下来R行每行C个字符描述被偷之后的形状,字符分为以下三种:

  (1)‘.’表示空;

  (2)字符‘|’(ASCII为124)、‘-’、‘+’、‘1’、‘2’、‘3’、‘4’描述管道的形状;

  (3)‘M’和‘Z’表示城市,两个都是只出现一次。

  输入保证石油的流向是唯一的,只有一个管道跟M和Z相连,除此此外,保证没有多余的管道,也就是说所有的管道在加进被偷的管道后一定都会被用上。

  输入保证有解而且是唯一的。

输出/Output:

       输出被偷走的管道的行号和列号以及管道的类型。

题解/solution:

       首先题目告诉我们恐怖分子只偷走了一个管道。我们从M点或Z点出发,顺这管道出发,找到那个漏油的地方(管道被偷之前肯定不会漏油),就是被偷走的管道的行号和列号。怎么判断呢?如果是‘-’,那必须和左右联通,不能和上下联通(很显然);如果是‘|’,那必须和上下联通,不能和左右联通(也很显然);‘+’,必须和上下左右联通(十分显然)。1、2、3、4也一样了,听完后,是不是水啊!

       点个赞啊,有不足告诉我。

代码/Code:

var
  n,m,x1,x2,y1,y2,o,p:longint;
  a:array [0..30,0..30] of char;
  f:array [0..30,0..30] of boolean;
  dx,dy:array [1..4] of integer;
procedure init;
var
  i,j:longint;
begin
  fillchar(f,sizeof(f),0);
  readln(n,m);
  for i:=1 to n do
    begin
      for j:=1 to m do
        begin
          read(a[i,j]);
          if a[i,j]='M' then
            begin
              x1:=i; y1:=j;
            end;
          if a[i,j]='Z' then
            begin
              x2:=i; y2:=j;
            end;
        end;
      readln;
    end;
end;

function fd(x,y:longint):boolean;
begin
  if (x<0) or (x>n+1) or (y<0) or (y>m+1) then exit(true);
  exit(false);
end;

procedure main(x,y:longint);
var
  i:longint;
begin
  if fd(x,y) or (f[x,y]) then exit;
  f[x,y]:=true;
  if a[x,y]='.' then
    begin
      o:=x; p:=y;
      exit;
    end;
  if a[x,y]='M' then
    begin
      for i:=1 to 4 do
        if a[x+dx[i],y+dy[i]]<>'.' then main(x+dx[i],y+dy[i]);
    end else
    case a[x,y] of
      '|':if f[x+dx[1],y+dy[1]] then main(x+dx[3],y+dy[3])
                                else main(x+dx[1],y+dy[1]);
      '-':if f[x+dx[2],y+dy[2]] then main(x+dx[4],y+dy[4])
                                else main(x+dx[2],y+dy[2]);
      '+':for i:=1 to 4 do main(x+dx[i],y+dy[i]);
      '1':if f[x+dx[3],y+dy[3]] then main(x+dx[4],y+dy[4])
                                else main(x+dx[3],y+dy[3]);
      '2':if f[x+dx[1],y+dy[1]] then main(x+dx[4],y+dy[4])
                                else main(x+dx[1],y+dy[1]);
      '3':if f[x+dx[1],y+dy[1]] then main(x+dx[2],y+dy[2])
                                else main(x+dx[1],y+dy[1]);
      '4':if f[x+dx[3],y+dy[3]] then main(x+dx[2],y+dy[2])
                                else main(x+dx[3],y+dy[3]);
    end;
end;

function t1(x,y:longint):boolean;
begin
  if a[x,y]='1' then exit(true);
  exit(false);
end;

function t2(x,y:longint):boolean;
begin
  if a[x,y]='2' then exit(true);
  exit(false);
end;

function t3(x,y:longint):boolean;
begin
  if a[x,y]='3' then exit(true);
  exit(false);
end;

function t4(x,y:longint):boolean;
begin
  if a[x,y]='4' then exit(true);
  exit(false);
end;

function t5(x,y:longint):boolean;
begin
  if a[x,y]='+' then exit(true);
  exit(false);
end;

function t6(x,y:longint):boolean;
begin
  if a[x,y]='-' then exit(true);
  exit(false);
end;

function t7(x,y:longint):boolean;
begin
  if a[x,y]='|' then exit(true);
  exit(false);
end;

procedure print;
var
  x,y:array [1..4] of longint;
  f1,f2,f3,f4:boolean;
  i:longint;
begin
  if (o=0) and (p=0) then
    begin
      if fd(x1-2,y1) and (a[x1-2,y1]<>'.') then begin o:=x1-2; p:=y1; end else
        if fd(x1+2,y1) and (a[x1+2,y1]<>'.') then begin o:=x1+2; p:=y1; end else
          if fd(x1,y1-2) and (a[x1,y1-2]<>'.') then begin o:=x1; p:=y1-2; end else
            if fd(x1,y1+2) then begin o:=x1; p:=y1+2; end;
    end;
  write(o,' ',p,' ');
  for i:=1 to 4 do
    begin
      x[i]:=o+dx[i]; y[i]:=p+dy[i];
    end;
  f1:=t2(x[2],y[2]) or t1(x[2],y[2]) or t5(x[2],y[2]) or t6(x[2],y[2]);
  f2:=t3(x[4],y[4]) or t4(x[4],y[4]) or t5(x[4],y[4]) or t6(x[4],y[4]);
  f3:=t4(x[1],y[1]) or t1(x[1],y[1]) or t5(x[1],y[1]) or t7(x[1],y[1]);
  f4:=t2(x[3],y[3]) or t3(x[3],y[3]) or t5(x[3],y[3]) or t7(x[3],y[3]);
  if (not f1) and (not f2) and (f3) and (f4) then write('|');
  if (f1) and (f2) and (not f3) and (not f4) then write('-');
  if (f1) and (f2) and (f3) and (f4) then write('+');
  if (not f1) and (f2) and (not f3) and (f4) then write('1');
  if (not f1) and (f2) and (f3) and (not f4) then write('2');
  if (f1) and (not f2) and (f3) and (not f4) then write('3');
  if (f1) and (not f2) and (not f3) and (f4) then write('4');
end;

begin
  init;
  dx[1]:=-1; dx[2]:=0; dx[3]:=1; dx[4]:=0;
  dy[1]:=0; dy[2]:=-1; dy[3]:=0; dy[4]:=1;
  main(x1,y1);
  print;
end.