SCAU2020春季个人排位赛 #6--I

题目描述

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

解法和思路

看到这道题目的时候,我很感兴趣,,可能是因为这是实现机器人的功能吧,hh。

这道题目看完题目以后,就会清楚,这是一道模拟题,那么这道题目怎么模拟呢?首先我们先给机器人定义一个结构体数组,这个结构体里面存放机器人的横纵坐标还有机器人现在的方向。
做题前我们先规定一个东西(其实这个想怎么规定就怎么规定),就是规定什么来代表方向,我用的是0,90,180,270,分别代表东北西南这四个方向。
然后给左转和右转和向前分别定义一个函数,实现这些功能(很容易实现,在这里就不用我说了吧)。
我觉得这个题唯一需要讲的地方就是如何做标记,我是给地图赋了一个初值,如果一个坐标处的值不等于这个初值,说明这个地方是有机器人的(可以用来判断是否两个机器人相撞)。然后需要注意的是,我没每次进行机器人移动这个操作的时候,需要将刚开始的时候机器人的坐标给抹掉,每次移动进行一次判断是否相撞,到移动完了,才将这个机器人对应的编号输入到对应的地图中(这样可以有利于直接从地图中找到相撞的机器人的编号)。
还有需要注意的是给地图初始化的时候,要将有机器人的地方标记上。
最后,为了方便,我们用一个int类型的flag来记录没有相撞,和墙壁相撞,和机器人相撞这三种状态,分别对应,0,1,2.
这道题目差不多就是这样了。
关键就是实现各种函数的功能和做对应的标记。
代码如下:

#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
#define ll long long
typedef struct
{
    int fx,x,y;
} robot;
int flag=0;
int a,b,pz,CASE;
robot r[105];
int map1[105][105];
void L(int number,int num)
{
    for(int i=0; i<num; i++)
    {
        if(r[number].fx+90==360)
            r[number].fx=0;
        else
            r[number].fx=r[number].fx+90;
        }
}
void R(int number,int num)
{
    for(int i=0; i<num; i++)
    {
        if(r[number].fx==0)
            r[number].fx=270;
        else
            r[number].fx=r[number].fx-90;
    }
}
void F(int number,int num)
{
    map1[r[number].x][r[number].y]=404;
    for(int i=0; i<num; i++)
    {
        if(r[number].fx==0)
            r[number].x+=1;
        else if(r[number].fx==180)
            r[number].x-=1;
        else if(r[number].fx==270)
            r[number].y-=1;
        else
            r[number].y+=1;
            if(r[number].x<1||r[number].x>a||r[number].y>b||r[number].y<1)
            {
                flag=1;
                break;
            }
            else if(map1[r[number].x][r[number].y]!=404)
            {
                pz=map1[r[number].x][r[number].y];
                flag=2;
                break;
            }
    }
    if(!flag)
    map1[r[number].x][r[number].y]=number;
}
int main()
{
    cin>>CASE;
    while(CASE--)
    {
        cin>>a>>b;
        for(int i=1; i<=a; i++) //创建地图
            for(int j=1; j<=b; j++)
                map1[i][j]=404;
        int n,m;
        cin>>n>>m;
        for(int i=1; i<=n; i++) //初始化每个机器
        {
            char temp;
            cin>>r[i].x>>r[i].y>>temp;
            if(temp=='E')
                r[i].fx=0;
            else if(temp=='N')
                r[i].fx=90;
            else if(temp=='W')
                r[i].fx=180;
            else
                r[i].fx=270;
                map1[r[i].x][r[i].y]=i;
        }
        for(int i=0; i<m; i++) //执行指令
        {
            int number,num;
            char temp;
            cin>>number>>temp>>num;
            if(flag)
                continue;
            if(temp=='L')
                L(number,num);
            else if(temp=='R')
                R(number,num);
            else
                F(number,num);
            if(flag==1)
                    printf("Robot %d crashes into the wall\n",number);
            else if(flag==2)
                    printf("Robot %d crashes into robot %d\n",number,pz);
        }
        if(flag)
            flag=0;
        else cout<<"OK"<<endl;
    }
    return 0;
}
发布了43 篇原创文章 · 获赞 26 · 访问量 3070

猜你喜欢

转载自blog.csdn.net/Leo_zehualuo/article/details/104735963