Rainforest Inc. is opening a large new automated warehouse in the far Northern reaches of the
UK—some place they call “Walthamstow”.
The robotic worker drones inside will operate in not just one or two, but three dimensions, and
can 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 no
obstacles, need to take (jX2 ? X1j + jY2 ? Y1j + jZ2 ? Z1j) steps in total.
Since this warehouse is in Britain, and because every stereotype is true, the robotic denizens are
all impeccably polite. When two robots travelling in opposite directions meet, they wordlessly
negotiate 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 swap
positions in one move. All moves are instantaneous.
We have prepared a test run of the warehouse with just two machines installed. Write a program
to pass this test.
Input
Two lines, one for each robot, each containing six space-separated integers (X0Y0Z0) and
(X1Y1Z1), 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 places
from each other.
Output
Output up to 7000 lines, giving one possible list of locations of the robots over time. The position
of both robots at time T must be given as bracketed space-separated (X; Y;Z) co-ordinate tuples
on line T.
Co-ordinates must not exceed an absolute value of 106.
题意:
两个机器人从各自的起点出发,到各自的终点,没有障碍物,但是两个机器人不能同时出现在同一个格子里面。
输出他们的路径。
思路:
第一时间想到了这个
https://www.bilibili.com/video/av11031178?from=search&seid=45073191624370909
但是这题的效率要求没有这么高,而且只需要处理两个机器人即可。
先处理出来其中一个机器人的路径,然后把第二个机器人挪到第一人的路经之外,然后等第一个机器人走完之后,第二个机器人再出发。
代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream> 2 #include<algorithm> 3 #include<vector> 4 #include<stack> 5 #include<queue> 6 #include<map> 7 #include<set> 8 #include<cstdio> 9 #include<cstring> 10 #include<cmath> 11 #include<ctime> 12 #define fuck(x) cout<<#x<<" = "<<x<<endl; 13 #define ls (t<<1) 14 #define rs ((t<<1)+1) 15 using namespace std; 16 typedef long long ll; 17 typedef unsigned long long ull; 18 const int maxn = 100086; 19 const int inf = 2.1e9; 20 const ll Inf = 999999999999999999; 21 const int mod = 1000000007; 22 const double eps = 1e-6; 23 const double pi = acos(-1); 24 struct node{ 25 int x,y,z; 26 bool operator<(const node &p)const{ 27 if(p.x==x&&y==p.y){return z<p.z;} 28 if(p.x==x){return y<p.y;} 29 return x<p.x; 30 } 31 }; 32 queue<node>ans; 33 int to1[3][3],to2[3][3]; 34 map<node,bool>vis; 35 int to[6][3]={1,0,0,-1,0,0, 0,1,0,0,-1,0, 0,0,1,0,0,-1}; 36 bool dfs(node a,node ea){ 37 node tmp; 38 vis[a]=true; 39 ans.push(a); 40 if(a.x==ea.x&&a.y==ea.y&&a.z==ea.z){return true;} 41 for(int i=0;i<3;i++){ 42 if(i==0&&ea.x==a.x){continue;} 43 if(i==1&&ea.y==a.y){continue;} 44 if(i==2&&ea.z==a.z){continue;} 45 tmp.x=a.x+to1[i][0]; 46 tmp.y=a.y+to1[i][1]; 47 tmp.z=a.z+to1[i][2]; 48 if(dfs(tmp,ea)){return true;} 49 } 50 return false; 51 } 52 void print(node a,node b){ 53 printf("(%d %d %d) (%d %d %d)\n",a.x,a.y,a.z,b.x,b.y,b.z); 54 } 55 node solve(node p,node start,node pos,int t){ 56 node tmp; 57 for(int i=0;i<6;i++){ 58 tmp.x=pos.x+to[i][0]; 59 tmp.y=pos.y+to[i][1]; 60 tmp.z=pos.z+to[i][2]; 61 if(!vis[tmp]){break;} 62 } 63 while(!ans.empty()){ 64 if(t==1)print(ans.front(),tmp); 65 else print(tmp,ans.front()); 66 ans.pop(); 67 } 68 return tmp; 69 } 70 int main(){ 71 node a,b,ea,eb; 72 scanf("%d%d%d",&a.x,&a.y,&a.z); 73 scanf("%d%d%d",&ea.x,&ea.y,&ea.z); 74 scanf("%d%d%d",&b.x,&b.y,&b.z); 75 scanf("%d%d%d",&eb.x,&eb.y,&eb.z); 76 if(ea.x>a.x){to1[0][0]=1;} 77 else if(ea.x<a.x){to1[0][0]=-1;} 78 if(ea.y>a.y){to1[1][1]=1;} 79 else if(ea.y<a.y){to1[1][1]=-1;} 80 if(ea.z>a.z){to1[2][2]=1;} 81 else if(ea.z<a.z){to1[2][2]=-1;} 82 print(a,b); 83 dfs(a,ea); 84 b=solve(ea,a,b,1); 85 vis.clear(); 86 memset(to1,0,sizeof(to1)); 87 if(eb.x>b.x){to1[0][0]=1;} 88 else if(eb.x<b.x){to1[0][0]=-1;} 89 if(eb.y>b.y){to1[1][1]=1;} 90 else if(eb.y<b.y){to1[1][1]=-1;} 91 if(eb.z>b.z){to1[2][2]=1;} 92 else if(eb.z<b.z){to1[2][2]=-1;} 93 dfs(b,eb); 94 solve(eb,b,ea,2); 95 print(ea,eb); 96 return 0; 97 }