Problem G Gentlebots 随机模拟

Rainforest Inc. is opening a large new automated warehouse in the far Northern reaches of theUK—some place they call “Walthamstow”.The robotic worker drones inside will operate in not just one or two, but three dimensions, andcan move in any one of the 6 cardinal dimensions in steps of 1 metre at a time. For example,a robot looking to move from position (X1, Y1, Z1) to position (X2, Y2, Z2) will, assuming noobstacles, need to take (|X2 − X1| + |Y2 − Y1| + |Z2 − Z1|) steps in total.Since this warehouse is in Britain, and because every stereotype is true, the robotic denizens areall impeccably polite. When two robots travelling in opposite directions meet, they wordlesslynegotiate for one of the robots to step aside somehow so the other can pass.Multiple robots cannot occupy the same integer co-ordinates, and no two robots can swappositions in one move. All moves are instantaneous.We have prepared a test run of the warehouse with just two machines installed. Write a programto pass this test.InputTwo lines, one for each robot, each containing six space-separated integers (X0Y0Z0) and(X∞Y∞Z∞), the intended start and end locations of a robot respectively (−1000 ≤ X, Y, Z ≤1000).The robots will start in different places from each other, and will also end in different placesfrom each other.OutputOutput up to 7000 lines, giving one possible list of locations of the robots over time. The positionof both robots at time T must be given as bracketed space-separated (X, Y, Z) co-ordinate tupleson line T.Co-ordinates must not exceed an absolute value of 106.



同时dfs,两人同时进行模拟,6个方向,两人各选择一个,走即可。如果能找到比现在还有更优(更近)的解,那就朝那个方向移动。至此这就是最普通的题 。但是题目说如果找不到最优解,那么随便走。所以我们要模拟“随便走”,如果没有最优解,那么rank随机数出两个方向走即可。

#include <iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,z;
}A,B,C,D;
int tx[7]={1,-1,0,0,0,0,0};
int ty[7]={0,0,-1,1,0,0,0};
int tz[7]={0,0,0,0,1,-1,0};
int dis(node t1,node t2)
{
    return abs(t1.x-t2.x)+abs(t1.y-t2.y)+abs(t1.z-t2.z);
}
int main()
{
    cin>>A.x>>A.y>>A.z;
    cin>>B.x>>B.y>>B.z;
    cin>>C.x>>C.y>>C.z;
    cin>>D.x>>D.y>>D.z;
    while(1)
    {
        cout<<"("<<A.x<<" "<<A.y<<" "<<A.z<<")";
        cout<<" ";
        cout<<"("<<C.x<<" "<<C.y<<" "<<C.z<<")";
        cout<<endl;
        int pre=dis(A,B)+dis(C,D);
        if(pre==0)
        {
            return 0;
        }
        int flag=1e9+8;
        int diyige=0;
        int dierge=0;
        for(int i=0;i<7;i++)
        {
            for(int j=0;j<7;j++)
            {
                node E;
                E.x=A.x+tx[i];
                E.y=A.y+ty[i];
                E.z=A.z+tz[i];
                node F;
                F.x=C.x+tx[j];
                F.y=C.y+ty[j];
                F.z=C.z+tz[j];
                if(dis(E,C)==0)//不能前脚走后脚到
                {
                    continue;
                }
                if(dis(E,F)==0)//不能同时到一个地方
                {
                    continue;
                }
                if(dis(F,A)==0)//不能前脚走后脚到
                {
                    continue;
                }
                if(dis(F,E)==0)//不能同时到一个地方
                {
                    continue;
                }
                int now=dis(E,B)+dis(F,D);
                if(now<flag)
                {
                    flag=now;
                    diyige=i;
                    dierge=j;
                }


            }
        }
        if(flag>=pre)//如果移动后最小也要比原来远,那么即便动一个。(!!!)也就是题目中额自由移动
        {
             while(1)
            {
                int i=rand()%7,j=rand()%7;
                node E;
                E.x=A.x+tx[i];
                E.y=A.y+ty[i];
                E.z=A.z+tz[i];
                node F;
                F.x=C.x+tx[j];
                F.y=C.y+ty[j];
                F.z=C.z+tz[j];
                if(dis(E,C)==0)//不能前脚走后脚到
                {
                    continue;
                }
                if(dis(E,F)==0)//不能同时到一个地方
                {
                    continue;
                }
                if(dis(F,A)==0)//不能前脚走后脚到
                {
                    continue;
                }
                if(dis(F,E)==0)//不能同时到一个地方
                {
                    continue;
                }
                diyige=i,dierge=j;
                break;
            }
        }
        A.x+=tx[diyige];
         A.y+=ty[diyige];
          A.z+=tz[diyige];
          C.x+=tx[dierge];
         C.y+=ty[dierge];
          C.z+=tz[dierge];




    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值