`
Coco_young
  • 浏览: 120016 次
  • 性别: Icon_minigender_1
  • 来自: 湖南长沙
社区版块
存档分类
最新评论

POJ_2632 Crashing Robots

阅读更多
问题连接  http://poj.org/problem?id=2632

Crashing Robots
Time Limit: 1000MS  Memory Limit: 65536K
Total Submissions: 4687  Accepted: 2054

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

题目大意:模拟对在一个房间里的机器人进行操纵,输出机器人第一次发生碰撞的信息,如果没有碰撞,输出OK。

分析:简单模拟题,注意处理旋转,和坐标系对应。。。。

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;

typedef int B[3];
B rb[110];
typedef int MAZE[110][110];
MAZE mz;
typedef int INFO[2];
INFO c_info;
int N,M;
int W,H;

bool execute(int a,char b,int c)
{
    if(b=='L')
    {
        c = c%4;
        rb[a][2] = (rb[a][2]+c)%4;
    }
    else if(b=='R')
    {
        c = c%4;
        rb[a][2] = (rb[a][2]-c);
        if(rb[a][2]<0)
        rb[a][2]+=4;
    }
    else
    {
        for(int i=0;i<c;i++)
        {

            mz[rb[a][1]][rb[a][0]] = -1;//level that place
            switch(rb[a][2])
            {
            case 0:rb[a][1]--;break;
            case 1:rb[a][0]--;break;
            case 2:rb[a][1]++;break;
            case 3:rb[a][0]++;break;
            }
            if(rb[a][0]<0||rb[a][0]>=W||rb[a][1]<0||rb[a][1]>=H)
            {
                c_info[0] = a+1;
                c_info[1] = 0;
                return true;
            }
            if(mz[rb[a][1]][rb[a][0]]!=-1)
            {
                c_info[0] = a+1;
                c_info[1] = mz[rb[a][1]][rb[a][0]]+1;
                return true;
            }
            mz[rb[a][1]][rb[a][0]]=a;
        }
    }
    return false;
}

void see()
{
    for(int i=0;i<H;i++)
    {
        for(int j=0;j<W;j++)
        cout<<mz[i][j]<<" ";
        cout<<endl;
    }
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>W>>H>>N>>M;
        memset(mz,-1,sizeof(mz));//初始化棋盘
        for(int i=0;i<N;i++)
        {
            int x,y;
            char d;
            cin>>x>>y>>d;
            rb[i][0] = (x-1);
            rb[i][1] = H-1-(y-1);
            switch(d)
            {
            case 'N':rb[i][2]=0;break;
            case 'W':rb[i][2]=1;break;
            case 'S':rb[i][2]=2;break;
            case 'E':rb[i][2]=3;break;
            }
            mz[rb[i][1]][rb[i][0]] = i;//标记棋盘
        }
        //see();
        //读入问题
        bool cash = false;
        for(int i=0;i<M;i++)
        {
            int a,c;
            char b;
            cin>>a>>b>>c;
            if(!cash)
            cash = execute(a-1,b,c);
        }
        if(cash)
        {
            if(c_info[1]==0)
            printf("Robot %d crashes into the wall\n",c_info[0]);
            else
            printf("Robot %d crashes into robot %d\n",c_info[0],c_info[1]);
        }
        else
        printf("OK\n");
    }
    return 0;
}

0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics