P1331 海战

先来讲一下怎么判断是否撞了…

.....
.###.
.###.
.....
这样可以理解为一条船
一旦装船了
.....
.A#..
.##..
.....
则必定会形成一个非矩形的形状,即找如A处的转弯处

再套上一个裸的bfs就行了
奉上丑陋的代码QAQ

const z:array[1..4,1..2]of longint=((1,0),(-1,0),(0,1),(0,-1));//方向数组
var i,j,k:longint;
    m,n:longint;
    t,h:longint;
    max:int64;
    x,y,s:array[0..100010]of longint;
    b:array[-10..1010,-10..1010]of boolean;
    a:array[-10..1010,-10..1010]of char;
begin
  read(m,n);
  readln;
  for i:=1 to m do//读入QAQ
  begin
    for j:=1 to n do
    begin
      read(a[i,j]);
      if a[i,j]='#' then b[i,j]:=true;
    end;
    readln;
  end;
  for i:=1 to m do
  for j:=1 to n do
  begin
    if (a[i,j]='.') then//判断是否撞船了呀QAQ
    if (a[i-1,j]='#') and (a[i,j-1]='#')
    or (a[i-1,j]='#') and (a[i,j+1]='#')
    or (a[i+1,j]='#') and (a[i,j-1]='#')
    or (a[i+1,j]='#') and (a[i,j+1]='#') then
    begin
      write('Bad placement.');//无解输出
    exit;
    end;
  end;
  max:=0;
  for i:=1 to m do
  for j:=1 to n do
  begin
    if b[i,j] then//裸的bfs
    begin
      t:=1;
      h:=1;
      x[1]:=i;
      y[1]:=j;
      inc(max);
      repeat
        for k:=1 to 4 do
        if b[x[t]+z[k,1],y[t]+z[k,2]] then
        begin
          inc(h);
          x[h]:=x[t]+z[k,1];
          y[h]:=y[t]+z[k,2];
          b[x[t]+z[k,1],y[t]+z[k,2]]:=false;
        end;
        inc(t);
      until t>h;
    end;
  end;
  write('There are ',max,' ships.');//玄学的输出QwQ
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值