10.7 蚂蚁 2392

题目

在二维平面坐标轴里,有N只蚂蚁,第i只蚂蚁所在的点坐标是(xi,yi),坐标都是整数。所有蚂蚁的移动速度都相等,都是每秒一个单位。每只蚂蚁都有一个固定的移动方向,是如下4中方向之一,都平行于坐标轴:
N:向北,朝上,y坐标正方向
E: 向东,朝右,x坐标正方向
S: 向南,朝下,y坐标负方向
W:向西,朝左,x坐标负方向
当2只或多只蚂蚁在某个时刻(不一定是整数时刻)撞到一起,那么这些蚂蚁都会立即消失。 例如蚂蚁A的初始位置是(0, 0)且方向是向东,蚂蚁B的初始位置是(1, 0)且方向是向西,那么0.5秒后,两只蚂蚁会在点(0.5, 0)处碰撞,两只蚂蚁瞬间都消失。当所有的碰撞结束后,还有多少只蚂蚁存在?不管蚂蚁最终移动到哪里,只要没有消失,都算是存在。
-1000 ≤ x,y ≤ 1000。输入数据保证,一开始没有两只蚂蚁具有相同的位置。
N(1 ≤ N ≤ 50)
-1000 ≤ x,y ≤ 1000

题解

模拟吧,注意蚂蚁碰撞的先后顺序,还有多只蚂蚁碰到一起的情况。一步一步模拟,要有条理,不然容易连错哪里都不知道

代码

var
  n,i,j,k,ans:longint;
  s:string;
  x,y,b:array[1..50]of longint;
  a:array[-2020..2020,-2020..2020]of longint;

procedure rose(i:longint);
begin
  if s[i]='N' then y[i]:=y[i]+1 else
  if s[i]='S' then y[i]:=y[i]-1 else
  if s[i]='E' then x[i]:=x[i]+1 else x[i]:=x[i]-1;
end;

procedure bfs;
var
  i,j,k,t,c:longint;
begin
  while true do
    begin
      for i:=1 to n do
        if b[i]=0 then
          begin
            t:=0;
            a[x[i],y[i]]:=0;
            rose(i);
            if (a[x[i],y[i]]<>0)and(x[i]>=-2000)
               and(x[i]<=2000)and(y[i]>=-2000)and(y[i]<=2000) then
              begin
                b[a[x[i],y[i]]]:=1;
                b[i]:=1;
              end
            else
              a[x[i],y[i]]:=i;
              if (x[i]<-2000)or(x[i]>2000)or(y[i]<-2000)or(y[i]>2000) then
                b[i]:=2;
          end;
      c:=0;
      for i:=1 to n do
        if b[i]=1 then begin a[x[i],y[i]]:=0;b[i]:=-1;end;
      for i:=1 to n do
        if b[i]<>0 then inc(c);
      if c=n then exit;
    end;
end;

begin
  readln(n);
  readln(s);
  for i:=1 to n do
    begin
      readln(x[i],y[i]);
      x[i]:=x[i]*2;y[i]:=y[i]*2;
      a[x[i],y[i]]:=i;
    end;
  bfs;
  for i:=1 to n do
    if b[i]=2 then inc(ans);
  writeln(ans);
end.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值