poj 2632 Crashing Robots

来源:互联网 发布:php网页自动生成html 编辑:程序博客网 时间:2024/05/21 10:05
Crashing Robots
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9905 Accepted: 4227

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

45 42 21 1 E5 4 W1 F 72 F 75 42 41 1 E5 4 W1 F 32 F 11 L 11 F 35 42 21 1 E5 4 W1 L 961 F 25 42 31 1 E5 4 W1 F 41 L 11 F 20

Sample Output

Robot 1 crashes into the wallRobot 1 crashes into robot 2OKRobot 1 crashes into robot 2

Source

Nordic 2005

提示

题意:

在一个现代化的仓库里,机器人被用来提取货物。仔细的规划是必要的,以确保机器人达到他们的目的地,而不会相互碰撞。当然,所有的仓库都是矩形,所有的机器人都有一个直径为1米的圆形地面空间。假设有n个机器人,编号从1到N。你将会知道每个机器人的位置和面对的方向。指令处理的顺序按照输入的先后顺序来,没有两个机器人同时移动,即第二个机器人总是在上一个移动之后去完成它的动作。
如果指令使一个机器人超出仓库区域,机器人将与墙发生碰撞,如果使他们试图占据同一个位置,两个机器人也会发生相互碰撞。

你的任务是输入一堆指令来判断是否会使机器人发生碰撞。对机器人的指令如下:(指令格式:机器人编号 指令 重复次数)

1.L:向左转90度。

2.R:向右转90度。

3.F:向前移动一格。

思路:

模拟题,照着敲就是。

示例程序

Source CodeProblem: 2632Code Length: 2593BMemory: 392KTime: 0MSLanguage: GCCResult: Accepted#include <stdio.h>struct{    int x,y,d;} robot[100];int judge(int id,int a,int b,int n){    int i;    if(robot[id].x>=a||robot[id].x<0||robot[id].y<0||robot[id].y>=b)//判断边界    {        printf("Robot %d crashes into the wall\n",id+1);        return 1;    }    for(i=0;n>i;i++)//判断是否阻挡    {        if(robot[i].x==robot[id].x&&robot[i].y==robot[id].y&&i!=id)        {            printf("Robot %d crashes into robot %d\n",id+1,i+1);            return 1;        }    }    return 0;}int main(){    int t,i,a,b,i1,id,re,k,n,m;    char op;    scanf("%d",&t);    for(i=1; t>=i; i++)    {        k=0;        scanf("%d %d",&a,&b);        scanf("%d %d",&n,&m);        for(i1=0; n>i1; i1++)        {            scanf("%d %d %c",&robot[i1].x,&robot[i1].y,&op);            robot[i1].x--;            robot[i1].y--;            if(op=='E')            {                robot[i1].d=0;            }            else if(op=='N')            {                robot[i1].d=1;            }            else if(op=='W')            {                robot[i1].d=2;            }            else            {                robot[i1].d=3;            }        }        for(i1=0; m>i1; i1++)        {            scanf("%d %c %d",&id,&op,&re);            id--;            if(k==1)//输出第一次碰撞,之后就不管了            {                continue;            }            if(op=='F')            {                for(; re>0; re--)                {                    if(robot[id].d==0)                    {                        robot[id].x++;                    }                    else if(robot[id].d==1)                    {                        robot[id].y++;                    }                    else if(robot[id].d==2)                    {                        robot[id].x--;                    }                    else                    {                        robot[id].y--;                    }                    k=judge(id,a,b,n);                    if(k==1)                    {                        break;                    }                }            }            else if(op=='L')            {                robot[id].d=(robot[id].d+re)%4;            }            else            {                re=re%4;                if(robot[id].d>=re)                {                    robot[id].d=robot[id].d-re;                }                else                {                    robot[id].d=4+(robot[id].d-re);                }            }        }        if(k==0)        {            printf("OK\n");        }    }    return 0;}


0 0