输油管道 纪中 1432 优美的暴力

来源:互联网 发布:sim800 网络连接失败 编辑:程序博客网 时间:2024/05/18 17:01

Description

  请你帮忙设计一个从城市M到城市Z的输油管道,现在已经把整个区域划分为R行C列,每个单元格可能是空的也可能是以下7种基本管道之一:
  这里写图片描述
  油从城市M流向Z,‘+’型管道比较特殊,因为石油必须在两个方向(垂直和水平)上传输,如下图所示:
这里写图片描述
       
  现在恐怖分子弄到了输油管道的设计图,并把其中一个单元格中的管道偷走了,请你帮忙找到偷走的管道的位置以及形状。

Input

  第一行包含两个整数R和C(1<=R,C<=25)。
  接下来R行每行C个字符描述被偷之后的形状,字符分为以下三种:
  (1)‘.’表示空;
  (2)字符‘|’(ASCII为124)、‘-’、‘+’、‘1’、‘2’、‘3’、‘4’描述管道的形状;
  (3)‘M’和‘Z’表示城市,两个都是只出现一次。
  输入保证石油的流向是唯一的,只有一个管道跟M和Z相连,除此此外,保证没有多余的管道,也就是说所有的管道在加进被偷的管道后一定都会被用上。
  输入保证有解而且是唯一的。

分析

一堆case和if的水题。

代码

const  dx:array[1..4] of longint=(-1,0,1,0);  dy:array[1..4] of longint=(0,1,0,-1);var  a:array[0..100,0..100] of longint;  i,j,k:longint;  n,m:longint;  c:char;  ans:longint;procedure bfs(x,y,xy,s:longint);var  i,j,k:longint;  x1,y1,x2,y2,s1:longint;begin  j:=0;  ans:=1;  for i:=1 to 4 do    begin      if i=xy then continue;      case i of        1:if (a[x+dx[i],y+dy[i]]=2)or(a[x+dx[i],y+dy[i]]=3)or(a[x+dx[i],y+dy[i]]=6)            then continue;        2:if (a[x+dx[i],y+dy[i]]=1)or(a[x+dx[i],y+dy[i]]=2)or(a[x+dx[i],y+dy[i]]=5)            then continue;        3:if (a[x+dx[i],y+dy[i]]=1)or(a[x+dx[i],y+dy[i]]=4)or(a[x+dx[i],y+dy[i]]=6)            then continue;        4:if (a[x+dx[i],y+dy[i]]=4)or(a[x+dx[i],y+dy[i]]=3)or(a[x+dx[i],y+dy[i]]=5)            then continue;      end;      if a[x+dx[i],y+dy[i]]<>0        then          begin            if j=1 then              begin                writeln(x,' ',y,' ','+');                exit;              end;            x2:=x+dx[i]; y2:=y+dy[i];            s1:=i;            j:=1;          end;    end;  if (xy=1) or (xy=3) then begin  x1:=x+dx[xy];  y2:=y;  end;  if (xy=2) or (xy=4) then begin    x1:=x;    y1:=y1+dy[xy];  end;  case xy of    1:begin        if s1=3 then writeln(x,' ',y,' |');        if s1=2 then writeln(x,' ',y,' 2');        if s1=4 then writeln(x,' ',y,' 3');      end;    2:begin        if s1=3 then writeln(x,' ',y,' 1');        if s1=1 then writeln(x,' ',y,' 2');        if s1=4 then writeln(x,' ',y,' -');      end;    3:begin        if s1=1 then writeln(x,' ',y,' |');        if s1=2 then writeln(x,' ',y,' 1');        if s1=4 then writeln(x,' ',y,' 4');      end;    4:begin        if s1=3 then writeln(x,' ',y,' 4');        if s1=2 then writeln(x,' ',y,' -');        if s1=1 then writeln(x,' ',y,' 3');      end;  end;end;begin  readln(n,m);  for i:=1 to n do    begin      for j:=1 to m do        begin          read(c);          case c of            '|':a[i,j]:=5;            '-':a[i,j]:=6;            '+':a[i,j]:=7;            '1':a[i,j]:=1;            '2':a[i,j]:=2;            '3':a[i,j]:=3;            '4':a[i,j]:=4;            '.':a[i,j]:=0;            'M':a[i,j]:=10;            'Z':a[i,j]:=10;          end;        end;      readln;    end;  for i:=1 to n do    for j:=1 to n do      begin        if a[i,j]=0 then continue;        ans:=0;        case a[i,j] of          1:begin              if a[i,j+1]=0                then bfs(i,j+1,4,1);              if a[i+1,j]=0                then bfs(i+1,j,1,1);            end;          2:begin              if a[i,j+1]=0                then bfs(i,j+1,4,2);              if a[i-1,j]=0                then bfs(i-1,j,3,2);            end;          3:begin              if a[i,j-1]=0                then bfs(i,j-1,2,3);              if a[i-1,j]=0                then bfs(i-1,j,3,3);            end;          4:begin              if a[i,j-1]=0                then bfs(i,j-1,2,4);              if a[i+1,j]=0                then bfs(i+1,j,1,4);            end;          5:begin              if a[i-1,j]=0                then bfs(i-1,j,3,5);              if a[i+1,j]=0                then bfs(i+1,j,1,5);            end;          6:begin              if a[i,j+1]=0                then bfs(i,j+1,4,6);              if a[i,j-1]=0                then bfs(i,j-1,2,6);            end;          7:begin              if a[i,j+1]=0                then bfs(i,j+1,4,7);              if a[i+1,j]=0                then bfs(i+1,j,1,7);              if a[i,j-1]=0                then bfs(i,j-1,2,7);              if a[i-1,j]=0                then bfs(i-1,j,3,7);            end;        end;        if ans=1 then halt;      end;end.
1 0
原创粉丝点击