POJ2632——Crashing Robots

Crashing Robots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10064 Accepted: 4279

Description

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

Source


题目大意:

首先有T组测试数据,每一组数据首先输入a和b,分别代表坐标纸的横向长度和纵向长度,然后输入n和m,分别表示机器人数和操作数,然后n行输入,第i行表示从机器人 i 的位置以及所朝的方向(E、W、S、N分别表示东西南北),m行输入,每行表示机器人x,进行 ch(L、R、F分别表示左转90度,右转90度,前进) 操作,重复rep次。

求:若机器人 i 撞墙了  输出:Robot i crashes into the wall

     若机器人 i 撞到了机器人 j   输出:Robot i crashes into robot j

      若顺利完成,输出:OK

思路:

若进行转向操作,转四次和没转是一样的,所以只需要对重复数  rep%4 ;但是前进不能取余!!

建立坐标系要跟题目给出的一样,要不然操作的时候就乱了。。。

结构体node数组num[]储存机器人的位置以及所朝的方向;

结构体N数组od[]储存操作;

二维数组mp[][]储存坐标系中各个点的机器人编号(若没有机器人,该点值为0);

当机器人移动之后,需要对二维数组mp[][]指定位置(旧位置和新位置)进行更新。

具体各个操作如何实现见注释。。。。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>

using namespace std;

const int MX = 110;
int a, b;
int n, m;
int mp[MX][MX];
struct node
{
    int x, y;                  //x,y分别表示横纵坐标
    int dir_x, dir_y;     //dir_x, dir_y分别表示所朝方向,即前进的x, y坐标如何变化
}num[MX];

struct N
{
    int id;                     //机器人编号
    char ord;                //操作指令
    int rep;                   //重复次数
}od[MX];

bool out(int x, int y)      //判断是否越界
{
    if( x<1||x>a||y<1||y>b )
        return false;
    return true;
}

int order(int rob, char ch, int re)       //操作函数   成功返回1,失败返回0
{
    if( ch=='L' )    //左转
    {
        re = re%4;   //转向取余操作
        while(re--)
        {
            if( num[rob].dir_x==0&&num[rob].dir_y==1 )          //之前朝北,转完了朝西
            {
                num[rob].dir_x = -1;
                num[rob].dir_y = 0;
            }
            else if( num[rob].dir_x==0&&num[rob].dir_y==-1 )  //之前朝南,转完了朝东
            {
                num[rob].dir_x = 1;
                num[rob].dir_y = 0;
            }
            else if( num[rob].dir_x==-1&&num[rob].dir_y==0 )  //之前朝西,转完了朝南
            {
                num[rob].dir_x = 0;
                num[rob].dir_y = -1;
            }
            else if( num[rob].dir_x==1&&num[rob].dir_y==0 )  //之前朝东,转完了朝北
            {
                num[rob].dir_x = 0;
                num[rob].dir_y = 1;
            }
        }
    }
    else if( ch=='R' )  //右转   以下操作跟左转规律一样,用手转一下就知道了
    {
        re = re%4;
        while(re--)
        {
            if( num[rob].dir_x==0&&num[rob].dir_y==1 )
            {
                num[rob].dir_x = 1;
                num[rob].dir_y = 0;
            }
            else if( num[rob].dir_x==0&&num[rob].dir_y==-1 )
            {
                num[rob].dir_x = -1;
                num[rob].dir_y = 0;
            }
            else if( num[rob].dir_x==-1&&num[rob].dir_y==0 )
            {
                num[rob].dir_x = 0;
                num[rob].dir_y = 1;
            }
            else if( num[rob].dir_x==1&&num[rob].dir_y==0 )
            {
                num[rob].dir_x = 0;
                num[rob].dir_y = -1;
            }
        }
    }
    else       //前进操作
    {
        while(re--)
        {
            if( !out(num[rob].x+num[rob].dir_x,num[rob].y+num[rob].dir_y) )   //判断下一步是否撞墙
            {
                cout<<"Robot "<<rob<<" crashes into the wall"<<endl;
                return 0;
            }
            if( mp[num[rob].x+num[rob].dir_x][num[rob].y+num[rob].dir_y] )  //判断下一步是否撞到机器人
            {
                cout<<"Robot "<<rob<<" crashes into robot "<<mp[num[rob].x+num[rob].dir_x][num[rob].y+num[rob].dir_y]<<endl;
                return 0;
            }
            mp[num[rob].x][num[rob].y] = 0;    //更新mp[][]数组
            num[rob].x += num[rob].dir_x;
            num[rob].y += num[rob].dir_y;
            mp[num[rob].x][num[rob].y] = rob;
        }
    }
    return 1;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        memset(mp,0,sizeof(mp));   //初始化mp[][]数组

        cin>>a>>b;
        cin>>n>>m;
        for( int i = 1;i <= n;i++ )
        {
            char ch;
            scanf("%d %d %c",&num[i].x,&num[i].y,&ch);

            mp[num[i].x][num[i].y] = i;

            if( ch=='N' )                             //初始朝向判断
            {
                num[i].dir_x = 0;
                num[i].dir_y = 1;
            }
            else if( ch=='S' )
            {
                num[i].dir_x = 0;
                num[i].dir_y = -1;
            }
            else if( ch=='W' )
            {
                num[i].dir_x = -1;
                num[i].dir_y = 0;
            }
            else if( ch=='E' )
            {
                num[i].dir_x = 1;
                num[i].dir_y = 0;
            }
        }
        int flag;        //标志变量  存放order()函数的返回值
        for( int i = 1;i <= m;i++ )
        {
            scanf("%d %c %d",&od[i].id,&od[i].ord,&od[i].rep);
        }
        int i;
        for( i = 1;i <= m;i++ )
        {
            flag = order(od[i].id,od[i].ord,od[i].rep);
            if( !flag )              //若失败  直接进入下一组测试数据
                break;
        }
        if( i > m&&flag )     //如果都成功  输出OK
            cout<<"OK"<<endl;

    }

    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值