模拟 Crashing Robots

本文介绍了一种模拟机器人在仓库环境中避免碰撞的算法。通过详细解析输入输出格式、机器人动作指令及碰撞检测逻辑,实现了一个能够处理多个机器人并确保它们在执行任务过程中不发生碰撞的系统。

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

In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destinations without crashing into each other. Of course, all warehouses are rectangular, and all robots occupy a circular floor space with a diameter of 1 meter. Assume there are N robots, numbered from 1 through N. You will get to know the position and orientation of each robot, and all the instructions, which are carefully (and mindlessly) followed by the robots. Instructions are processed in the order they come. No two robots move simultaneously; a robot always completes its move before the next one starts moving.
A robot crashes with a wall if it attempts to move outside the area of the warehouse, and two robots crash with each other if they ever try to occupy the same spot.
Input
The first line of input is K, the number of test cases. Each test case starts with one line consisting of two integers, 1 <= A, B <= 100, giving the size of the warehouse in meters. A is the length in the EW-direction, and B in the NS-direction.
The second line contains two integers, 1 <= N, M <= 100, denoting the numbers of robots and instructions respectively.
Then follow N lines with two integers, 1 <= Xi <= A, 1 <= Yi <= B and one letter (N, S, E or W), giving the starting position and direction of each robot, in order from 1 through N. No two robots start at the same position.

Figure 1: The starting positions of the robots in the sample warehouse

Finally there are M lines, giving the instructions in sequential order.
An instruction has the following format:
< robot #> < action> < repeat>
Where is one of
  • L: turn left 90 degrees,
  • R: turn right 90 degrees, or
  • F: move forward one meter,

and 1 <= < repeat> <= 100 is the number of times the robot should perform this single move.
Output
Output one line for each test case:
  • Robot i crashes into the wall, if robot i crashes into a wall. (A robot crashes into a wall if Xi = 0, Xi = A + 1, Yi = 0 or Yi = B + 1.)
  • Robot i crashes into robot j, if robots i and j crash, and i is the moving robot.
  • OK, if no crashing occurs.

Only the first crash is to be reported.
Sample Input
4
5 4
2 2
1 1 E
5 4 W
1 F 7
2 F 7
5 4
2 4
1 1 E
5 4 W
1 F 3
2 F 1
1 L 1
1 F 3
5 4
2 2
1 1 E
5 4 W
1 L 96
1 F 2
5 4
2 3
1 1 E
5 4 W
1 F 4
1 L 1
1 F 20
Sample Output
Robot 1 crashes into the wall
Robot 1 crashes into robot 2
OK
Robot 1 crashes into robot 2

第一次写这么长的模拟题把,思路很简单,但是两天才完全搞定,我是跟着思路一步一步来的,这次的坑点主要在

if  else 语句上的失败,

举个例子

if(ok1)

if(ok2)

else

{

}

我原本的意思是想要分三种情况,而上述代码却执行了两种情况,找了半天才找出来。

还有一个也是 if 语句,举个例子

if(dir=='n')

{

somefunction()...

}

if(dir=='e')

{

...

}

也不能用if,而要用else if

因为可能第一个操作导致dir变成了e,而又执行下一个操作。



注意到这点其他的没什么可说的了,按步骤赖就行。当然我的代码还没优化,可以自己优化。















#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
using namespace std;
int num;
int n,m;
int p,c,flag,step;
int vis[110][110];
struct Node
{
    int x;
    int y;
    char dir;
} node;
map<int,Node>mp;
void solve()
{
    int tx=node.x;
    int ty=node.y;
    char dir=node.dir;
    int cnt=1;
    int nx=tx,ny=ty;
    //cout<<dir<<" "<<tx<<" "<<ty<<endl;
    if(dir=='W')
    {
        for(int i=tx-1; i>=1&&cnt<=step; --i,++cnt)
        {
            if(vis[i][ty])
            {
                cout<<"Robot "<<num<<" crashes into robot "<<vis[i][ty]<<endl;
                flag=1;
                break;
            }
        }
        if(!flag&&tx-step<1)
        {
            cout<<"Robot "<<num<<" crashes into the wall"<<endl;
            flag=1;
        }
        if(!flag)
        {
            nx=tx-step;
        }
    }
    else if(dir=='N')
    {
        for(int i=ty+1; i<=m&&cnt<=step; ++i,++cnt)
        {
            if(vis[tx][i])
            {
                cout<<"Robot "<<num<<" crashes into robot "<<vis[tx][i]<<endl;
                flag=1;
                break;
            }
        }
        if(!flag&&ty+step>m)
        {
            cout<<"Robot "<<num<<" crashes into the wall"<<endl;
            flag=1;
        }
        if(!flag)
        {
            ny=ty+step;
        }
    }
    else if(dir=='E')
    {
        for(int i=tx+1; i<=n&&cnt<=step; ++i,++cnt)
        {
            if(vis[i][ty])
            {
                cout<<"Robot "<<num<<" crashes into robot "<<vis[i][ty]<<endl;
                flag=1;
                break;
            }
        }
        if(!flag&&tx+step>n)
        {
            cout<<"Robot "<<num<<" crashes into the wall"<<endl;
            flag=1;
        }
        if(!flag)
        {
            nx=tx+step;
        }
    }
    else if(dir=='S')
    {
        //cout<<tx<<" "<<ty<<endl;
        for(int i=ty-1; i>=1&&cnt<=step; --i,++cnt)
        {
            if(vis[tx][i])
            {
                cout<<"Robot "<<num<<" crashes into robot "<<vis[tx][i]<<endl;
                flag=1;
                break;
            }
        }
        if(!flag&&ty-step<1)
        {
            cout<<"Robot "<<num<<" crashes into the wall"<<endl;
            flag=1;
        }
        if(!flag)
        {
            ny=ty-step;
        }
    }
    if(!flag)
    {
        vis[tx][ty]=0;
        vis[nx][ny]=num;
        node.x=nx;
        node.y=ny;
        node.dir=dir;
        mp[num]=node;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        memset(vis,0,sizeof(vis));
        flag=0;
        cin>>p>>c;
        int x,y;
        char d;
        for(int i=1; i<=p; ++i)
        {
            scanf("%d %d %c",&x,&y,&d);
            vis[x][y]=i;
            node.x=x;
            node.y=y;
            node.dir=d;
            mp[i]=node;
        }
        for(int i=1; i<=c; ++i)
        {
            scanf("%d %c %d",&num,&d,&step);
            if(!flag)
            {
                if(d=='L')
                {
                    step%=4;
                    if(step)
                    {
                        if(mp[num].dir=='W')
                        {
                            if(step==1)
                            {
                                mp[num].dir='S';
                            }
                            if(step==2)
                            {
                                mp[num].dir='E';
                            }
                            if(step==3)
                            {
                                mp[num].dir='N';
                            }
                        }
                        else if(mp[num].dir=='N')
                        {
                            if(step==1)
                            {
                                mp[num].dir='W';
                            }
                            if(step==2)
                            {
                                mp[num].dir='S';
                            }
                            if(step==3)
                            {
                                mp[num].dir='E';
                            }
                        }
                        else if(mp[num].dir=='S')
                        {
                            if(step==1)
                            {
                                mp[num].dir='E';
                            }
                            if(step==2)
                            {
                                mp[num].dir='N';
                            }
                            if(step==3)
                            {
                                mp[num].dir='W';
                            }
                        }
                        else  if(mp[num].dir=='E')
                        {
                            if(step==1)
                            {
                                mp[num].dir='N';

                            }
                            if(step==2)
                            {
                                mp[num].dir='W';
                            }
                            if(step==3)
                            {
                                mp[num].dir='S';
                            }
                        }
                    }
                }
                else if(d=='R')
                {
                    step%=4;
                    if(step)
                    {
                        if(mp[num].dir=='W')
                        {
                            if(step==1)
                            {
                                mp[num].dir='N';
                            }
                            if(step==2)
                            {
                                mp[num].dir='E';
                            }
                            if(step==3)
                            {
                                mp[num].dir='S';
                            }
                        }
                        else if(mp[num].dir=='N')
                        {
                            if(step==1)
                            {
                                mp[num].dir='E';
                            }
                            if(step==2)
                            {
                                mp[num].dir='S';
                            }
                            if(step==3)
                            {
                                mp[num].dir='W';
                            }
                        }
                        else if(mp[num].dir=='S')
                        {
                            if(step==1)
                            {
                                mp[num].dir='W';
                            }
                            if(step==2)
                            {
                                mp[num].dir='N';
                            }
                            if(step==3)
                            {
                                mp[num].dir='E';
                            }
                        }
                        else if(mp[num].dir=='E')
                        {
                            if(step==1)
                            {
                                mp[num].dir='S';
                            }
                            if(step==2)
                            {
                                mp[num].dir='W';
                            }
                            if(step==3)
                            {
                                mp[num].dir='N';
                            }
                        }
                    }
                }
                else
                {
                    node=mp[num];
                    solve();
                }
            }
        }
        if(!flag)
            cout<<"OK"<<endl;
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值