遭遇战(特长生准备)

这篇博客探讨了一种矩阵游戏,玩家需要沿着特定方向移动并转弯,目标是在矩阵中与敌人相遇。由于玩家和敌人的速度相同,可以通过每次移动两步的方式来检查是否相遇。由于DFS方法受限于栈的大小,博主建议使用while循环进行模拟。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:

给你一个n*n的矩阵,左上角是(0,0),右下角是(n-1,n-1),现在我呢要一直的向一个方向走,并且走到尽头后向右转弯,然后我的敌人也是向一个方向一直走,走到尽头向左转弯,是否有一个时刻他们可以在一个点相遇。开始方向用d表示,0=东,1=南,2=西,3=北;n<1000;
同时还有十组数据。

思路:

因为他们速度是一样的,就可以一次移动两个,各走一步,如果走到相同地方,或者是都不可以走了,那么就可以退出了,但是又不可以用dfs写,因为pascal只有15000的栈,c++更小。。用while模拟一下就好了!!
转弯是不用时间的。

程序:

const
        maxn=1001;
        dx:array [1..4] of longint=(0,1,0,-1);
        dy:array [1..4] of longint=(1,0,-1,0);
var
        a,b:array [-1..maxn,-1..maxn] of boolean;
        i,j,n,t,x1,y1,d,s,dep,x2,y2,d1,d2,k:longint;
        f1,f2:boolean;
procedure dfs;
var
        tx,ty,i:longint;
begin
  a[x1,y1]:=false; b[x2,y2]:=false;
  while (x1<>x2) or (y1<>y2) do
    begin
      if f1
        then begin
               tx:=x1+dx[d1]; ty:=y1+dy[d1];
               if a[tx,ty]=false
                then begin
                       d1:=((d1) mod 4+1);
                       tx:=x1+dx[d1]; ty:=y1+dy[d1];
                       if a[tx,ty]=false then f1:=false;
                     end;
               if f1
                then begin
                       x1:=tx; y1:=ty; a[x1,y1]:=false;
                     end;
             end;
      if f2
        then begin
               tx:=x2+dx[d2]; ty:=y2+dy[d2];
               if b[tx,ty]=false
                then begin
                       d2:=((d2+2) mod 4+1);
                       tx:=x2+dx[d2]; ty:=y2+dy[d2];
                       if b[tx,ty]=false then f2:=false;
                     end;
               if f2
                then begin
                       x2:=tx; y2:=ty; b[x2,y2]:=false;
                     end;
             end;
      if (f1=false) and (f2=false) then break;
    end;
  if (x1=x2) and (y1=y2)
    then writeln(x1-1,' ',y1-1)
    else writeln(-1);
end;

begin
        assign(input,'fight.in'); reset(input);
        assign(output,'fight.out'); rewrite(output);
        readln(t);
        for i:=1 to t do
        begin
                readln(n);
                fillchar(a,sizeof(a),false);
                fillchar(b,sizeof(b),false);
                for j:=1 to n do
                 for k:=1 to n do
                 begin
                        a[j,k]:=true;
                        b[j,k]:=true;
                 end;

                f1:=true; f2:=true;
                readln(x1,y1,d1);
                readln(x2,y2,d2);
                inc(x1); inc(y1);  inc(d1);
                inc(x2); inc(y2);  inc(d2);
                dfs;
        end;
        close(input); close(output);
end.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值