台州学院acm:Crashing Robots

描述

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.

输入

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 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.

样例输入

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

样例输出

Robot 1 crashes into the wall
Robot 1 crashes into robot 2
OK
Robot 1 crashes into robot 2

题目意思:

在一个现代化的仓库,机器人需要去搬运物体,因此要认真的做一个计划安排每个机器人,在去他们目的地的时候避免相撞。当然,所有的仓库都是矩形,所有的机器人都占据了一个直径为1米的圆形地面空间,假设有N个机器人,编号从1~N,你会事先知道每个机器人要去的目的地和方向。所有的机器人都严格地遵从指令,指令时有顺序的。没有两个机器人是同时移动的。一个机器人完成移动后,另一个机器人才会开始移动。
如果一个机器人尝试越过仓库区域,就会撞墙,两个机器人试图占据同一个位置,他们就会相撞。
输入:
第一行有一个数K,代表K组测试数据。每组测试数据在地一行是由A,B,两个整数组成的。1<=A,B<=100,给出仓库的列数和行数,A是东西方向的长,对应列数,B是南北方向的长,代表行数。
第二行是由两个整数N和M组成的。N代表机器人的数目,M代表指令
接下来的N行,每行有两个整数,XI,YI,和一个字符(N,S,E,W),给出每一个机器人的起始位置和起始朝向的方向,按照1~N的顺序,任何两个机器人不可能有相同的起始位置。
数字1:
有M行,按次序给出指令,一个指令由下面组成。
机器人I,行为,重复
L:向左转九十度
R:向右转九十度
F:向前移动一米
1<=repeat<=100是机器人重复这个指令所需的次数。


题目代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
int K,A,B,N,M,flag;//K组测试数据,A(NS方向长度),B(EW方向长度),N机器人数,M指令条数
int Map[103][103]; //地图
struct robot
{
    int x,y;  //机器人的位置
    char ch;  //朝向
}R[105];
struct order
{
    int num;     //给第num个机器人下达指令
    char order;  //指令
    int repeat;  //重复该指令的次数
}O[105];
void Re(int a,char b,int c) //第I个机器人,指令,次数。
{
    Map[R[a].x][R[a].y]=0;
    if(b == 'F')//向前走
    {
        while(c--)
        {
            if(R[a].ch == 'E')  //原来方向水平向东
                R[a].y = R[a].y + 1;  //横坐标加1
            else if(R[a].ch == 'W')  //原来方向水平向西
                R[a].y = R[a].y - 1;  //横坐标减1
            else if(R[a].ch == 'N')      //原来朝北
                R[a].x = R[a].x + 1;   //纵坐标加1
            else if(R[a].ch == 'S')      //原来朝南
                R[a].x = R[a].x - 1;  //纵坐标减1
            if(R[a].y < 1 || R[a].y > A || R[a].x < 1 || R[a].x > B) //如果出界,就输出这个机器人撞墙了
            {
                flag = 1;
                printf("Robot %d crashes into the wall\n",a);
                return;
            }
            else if(Map[R[a].x][R[a].y] != 0) //如果它的新位置已经有数了,说明有其他机器人,这个机器人撞到了其他机器人了
            {
                flag = 1;
                printf("Robot %d crashes into robot %d\n",a,Map[R[a].x][R[a].y]);
                return;
            }
        }
    }
    else if(b == 'L') //向左转
    {
        c = c % 4;
        if(c == 0)
            R[a].ch = R[a].ch;//
        if(c == 1)
        {
            if(R[a].ch == 'E') R[a].ch = 'N';  //向左转,朝北
            else if(R[a].ch == 'W') R[a].ch = 'S';  //向左转,朝南
            else if(R[a].ch == 'N') R[a].ch = 'W';  //向左转,朝西
            else if(R[a].ch == 'S') R[a].ch = 'E';  //向左转,超东
        }
        if(c == 2)
        {
            if(R[a].ch == 'E') R[a].ch = 'W';
            else if(R[a].ch == 'W') R[a].ch = 'E';
            else if(R[a].ch == 'N') R[a].ch = 'S';
            else if(R[a].ch == 'S') R[a].ch = 'N';
        }
        if(c == 3)
        {
            if(R[a].ch == 'E') R[a].ch = 'S';
            else if(R[a].ch == 'W') R[a].ch = 'N';
            else if(R[a].ch == 'N') R[a].ch = 'E';
            else if(R[a].ch == 'S') R[a].ch = 'W';
        }
    }
    else //向右转
    {
        c = c % 4;
        if(c == 0)
            R[a].ch = R[a].ch;
        if(c == 1)
        {
            if(R[a].ch == 'E') R[a].ch = 'S';  //向右转朝南
            else if(R[a].ch == 'W') R[a].ch = 'N';  //向右转,朝北
            else if(R[a].ch == 'N') R[a].ch = 'E';  //向右转,朝东
            else if(R[a].ch == 'S') R[a].ch = 'W';  //向右转,朝西
        }
        if(c == 2)
        {
            if(R[a].ch == 'E') R[a].ch = 'W';
            else if(R[a].ch == 'W') R[a].ch = 'E';
            else if(R[a].ch == 'N') R[a].ch = 'S';
            else if(R[a].ch == 'S') R[a].ch = 'N';
        }
        if(c == 3)
        {
            if(R[a].ch == 'E') R[a].ch = 'N';
            else if(R[a].ch == 'W') R[a].ch = 'S';
            else if(R[a].ch == 'N') R[a].ch = 'W';
            else if(R[a].ch == 'S') R[a].ch = 'E';
        }
    }//通过这已操作,机器人已经走到下一个新的位置
    Map[R[a].x][R[a].y] = a;
    return;
}
int main()
{
    int i;
    scanf("%d",&K);  //K组测试数据
    while(K--)
    {
        scanf("%d%d",&A,&B);  //列标,行标
        scanf("%d%d",&N,&M);  //机器人的数目,指令数。
        memset(Map,0,sizeof(Map)); //将机器人在的位置放上当前机器人的编号,初始化为0
        for(i = 1; i <= N; i++)
        {
            scanf("%d%d",&R[i].y,&R[i].x);//当前机器人的坐标,列坐标,行坐标,
            scanf(" %c",&R[i].ch); //机器人的朝向
            Map[R[i].x][R[i].y] = i;
        }
        for(i = 1; i <= M; i++)
        {
           scanf("%d %c%d",&O[i].num,&O[i].order,&O[i].repeat);
        }
        flag=0;
        for(i = 1; i <= M; i++)
        {
            Re(O[i].num,O[i].order,O[i].repeat);
            if(flag)
                break;
        }
        if(flag == 0)
            printf("OK\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值