【校队排位赛#6 I】POJ Crashing Robots

17 篇文章 0 订阅
7 篇文章 0 订阅

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

题意已经很明显了,就是给你指令问你有没有crash。
模拟起来其实也很基础,不过对于题目,处理个小操作会做起来方便很多。
重建坐标轴:
在这里插入图片描述
将题中图示向右旋转90°,那么就可以恰好对应题目中的AB(行列),而且矩阵下标也满足左上到右下递增。方便我们遍历和移动。

这样左右转也不会受影响。不过要注意的是这个时候 W对应向上,E对应向下,N对应向右,S对应向左,这个点我一开始疏漏的debug半天。

然后就是普通的模拟了,这里我将字符转化为方向数组的对应一维下标。详见代码:

#include <iostream>
#include <vector>
#include <cstdio>
#include <map>
#include <climits>
#include <string>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#define maxn  150

using namespace std;
typedef  long long ll;
int dir[4][2] = {{-1,0} ,{0,1},{1,0},{0,-1}};       //设置方向数组 表示 上、右、下、左 , 按此顺序方便转体

struct pos      //结构体存放当前位置及方向
{
    int x;
    int y;
    int  di;
} a[maxn];

int grid[maxn][maxn];       //矩阵表示当前位置有无robot

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        memset(grid,0,sizeof(grid));        //初始化为0
        int  n,m;
        cin>>n>>m;          //矩阵行列
        int num , ord;
        cin>>num>>ord;      //robot 数量及 指令数量
        for(int i=1; i<=num; i++)
        {
            int x1, y1;
            char d;
            cin>>x1>>y1>>d;
            if(d=='N')   a[i].di =1 ;           //将方向字符转化为方向数组的对应一维下标         N是往右, S往左 , E是往下,  W是往上
            if(d=='S')  a[i].di = 3;
            if(d=='E') a[i].di = 2;
            if(d=='W') a[i].di = 0;
            a[i].x = x1;
            a[i].y = y1 ;

            grid[a[i].x][a[i].y] = i;           //这个位置标记为第i个 robot 的占位
        }
        int flag =1;        //标记
        for(int i=1; i<=ord; i++)
        {
            int idx ;
            char mov;
            int times;
            cin>>idx>>mov>>times;       //输入下标、行动指令、以及次数
            if(mov=='L')
            {
                a[idx].di = (abs(a[idx].di - times%4+4))%4;     //往左的话,就是 向右(1)->向上(0)->向左(3)->向下(2) 这个循环 ,因为是逆时针,所以和方向数组方向相反,用减法
            }
            else if(mov=='R')
            {
                a[idx].di = (abs(a[idx].di +times%4))%4;            //同上,方向相反
            }
            else  if(flag)          //因为只用输出一个crash,所以发生crash后只读入,不操作
            {

                for(int j=1; j<=times; j++ )            //前进
                {
                    int posx = dir[a[idx].di][0] * j + a[idx].x;
                    int posy = dir[a[idx].di][1] * j + a[idx].y;
                    if(posx<1||posy<1||posx>n||posy>m)      //越界break
                    {
                        flag = 0;
                        printf("Robot %d crashes into the wall\n",idx);
                        break;
                    }
                    else if(grid[posx][posy])       //crash together ,break
                    {
                        flag = 0;
                        printf("Robot %d crashes into robot %d\n",idx,grid[posx][posy]);
                        break;
                    }
                }


                if(flag)            //经历了上面判断仍为crash,就说明可以走
                {
                    int posx = dir[a[idx].di][0] * times + a[idx].x;
                    int posy = dir[a[idx].di][1] * times + a[idx].y;
                    grid[a[idx].x][a[idx].y] = 0;
                    a[idx]. x =  posx;
                    a[idx].y = posy;
                    grid[a[idx].x][a[idx].y] = idx;//更新

                }
            }

        }
        if(flag)
            cout<<"OK"<<endl;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值