• Crashing Robots


    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
    解释一下输入输出
    这是一个机器人跑动的问题,给出机器人坐标和场地范围,以及行动指令;如果撞墙或者撞到其它机器人就停止(注意,输入要完成)
    输入:4是4组数据
    5 4 是场地范围
    2 2  2个机器人,两个指令;
    1 1 E    1,1 是坐标,E是机器人此时面临的方向;
    1 F 7    1是机器人序号   F  是指令   7是执行次数
    关于指令
              F: 直走;
              L:左转90度;
              R:右转90度;
     
     
    下面的代码是我同学的  他写的比我的简洁
      1 #include<cstdio>
      2 #include<cstring>
      3 #include<iostream>
      4 using namespace std ;
      5 int len,high;
      6 int a,b,j ;
      7 struct node
      8 {
      9     int x;
     10     int y ;
     11     int dire;
     12 } s[10001];
     13 int judge(int k)
     14 {
     15     int i;
     16     if(s[k].x > len||s[k].x< 1||s[k].y>high||s[k].y<1)
     17     {
     18         printf("Robot %d crashes into the wall
    ",k);
     19         return 0;
     20     }
     21     for(i = 1 ; i <= a ; i++)
     22     {
     23         if(i == k)
     24             continue;
     25         if(s[i].x == s[k].x&&s[i].y == s[k].y)
     26         {
     27             printf("Robot %d crashes into robot %d
    ",k,i);
     28             return 0 ;
     29         }
     30     }
     31     return 1 ;
     32 }
     33 int main()
     34 {
     35     int n ;
     36     cin>>n;
     37     for(int i = 1 ; i <= n ; i++)
     38     {
     39         cin>>len>>high;
     40         cin>>a>>b ;
     41         char dire ;
     42         for(j = 1 ; j <= a ; j++)
     43         {
     44             cin>>s[j].x>>s[j].y>>dire;
     45             if(dire == 'N')
     46                 s[j].dire = 0 ;
     47             if(dire == 'W')
     48                 s[j].dire = 1 ;
     49             if(dire == 'S')
     50                 s[j].dire = 2 ;
     51             if(dire == 'E')
     52                 s[j].dire = 3 ;
     53         }
     54         int num,repeat,flag = 1 ;
     55         char order ;
     56         for(j = 1 ; j <= b ; j++)
     57         {
     58             cin>>num>>order>>repeat ;
     59             for(int h = 1 ; h <= repeat ; h++ )//把这个放在外边是为了底下的左右指令时比较好处理
     60             {
     61                 if(order == 'F')
     62                 {
     63                     if(s[num].dire == 0)
     64                     {
     65                         s[num].y++ ;
     66                         if(!judge(num))
     67                         {
     68                             flag = 0 ;
     69                             break ;
     70                         }
     71                     }
     72                     else if(s[num].dire == 1)
     73                     {
     74                         s[num].x--;
     75                         if(!judge(num))
     76                         {
     77                             flag = 0 ;
     78                             break ;
     79                         }
     80                     }
     81                     else if(s[num].dire == 2)
     82                     {
     83                         s[num].y--;
     84                         if(!judge(num))
     85                         {
     86                             flag = 0 ;
     87                             break ;
     88                         }
     89                     }
     90                     else if(s[num].dire == 3)
     91                     {
     92                         s[num].x++ ;
     93                         if(!judge(num))
     94                         {
     95                             flag = 0 ;
     96                             break ;
     97                         }
     98                     }
     99                 }
    100                 if(order == 'L')
    101                     s[num].dire = (1+s[num].dire)%4 ;
    102                 if(order == 'R')
    103                     s[num].dire = (s[num].dire-1+4)%4;
    104             }
    105             if(flag == 0)
    106                 break ;
    107         }
    108         if(j < b)
    109             for(++j ; j <= b ; j++)
    110                 cin>>num>>order>>repeat ;
    111         if(flag == 1)
    112             printf("OK
    ");
    113     }
    114     return  0 ;
    115 }
    View Code
  • 相关阅读:
    HTML screenX 事件属性
    CSS Display(显示) 与 Visibility(可见性)
    JavaScript手册 | JS Array 对象中的sort() 方法
    IntelliJ IDEA收费版本Ultimate的安装和破解
    ASP.NET Style 控件
    HTML area hreflang 属性
    Shell test 命令
    ftplib (Internet) – Python 中文开发手册
    Java 之 Collection 接口
    java 之 集合概述
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/3241890.html
Copyright © 2020-2023  润新知