zoukankan      html  css  js  c++  java
  • Crashing Robots(水题,模拟)

    1020: Crashing Robots

    时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte

    总提交: 207            测试通过:101

    描述

    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.

    输入

    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 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.

    样例输入

    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

    样例输出

    Robot 1 crashes into the wall
    Robot 1 crashes into robot 2
    OK
    Robot 1 crashes into robot 2

    题意:在一个长A宽B的矩形房间内有n个机器人,给m条指令判断机器人是否会和墙相撞或是否和其他机器人相撞。

    此题就是纯粹的模拟,先把4个方向改为数字存,每次移动并记录,注意往左和右移。注意每一次移动都要判断是否相撞,而不是最后指令全部执行才判断。

     1 #include "iostream"
     2 using namespace std;
     3 int a,b,n,m;
     4 struct wxl
     5 {
     6     int x,y;
     7     int s;
     8 }ht[110];
     9 bool panduan(int h)//判断机器人是否会相撞 
    10 {
    11     if(ht[h].x<=0||ht[h].x>a||ht[h].y<=0||ht[h].y>b)
    12     {
    13         cout<<"Robot "<<h<<" crashes into the wall"<<endl;
    14         return false;
    15     }
    16     for(int i=1;i<=n;i++)
    17     {
    18         if(i==h)continue;
    19         if(ht[i].x==ht[h].x&&ht[i].y==ht[h].y)
    20         {
    21             cout<<"Robot "<<h<<" crashes into robot "<<i<<endl;
    22             return false;
    23         }
    24     }
    25     return true;
    26 }
    27 int main()
    28 {
    29     int i,t,p,j,k;
    30     bool flag;
    31     string str;
    32     cin>>k;
    33     while(k--)
    34     {
    35         cin>>a>>b>>n>>m;
    36         for(i=1;i<=n;i++)//将方向转为数字存 
    37         {
    38             cin>>ht[i].x>>ht[i].y>>str;
    39             if(str=="N")ht[i].s=0;
    40             else if(str=="E")ht[i].s=1;
    41             else if(str=="S")ht[i].s=2;
    42             else if(str=="W")ht[i].s=3;
    43         }
    44         flag=true;//开始全为不会相撞 
    45         for(i=0;i<m;i++)
    46         {
    47             cin>>t>>str>>p;//t为机器人,str表示往哪个方向,p表示前进几步 
    48             if(str=="F")//F最为简单,先判断 
    49             {
    50                 if(flag)
    51                 {
    52                     for(j=0;j<p;j++)
    53                     {
    54                         if(ht[t].s==0)ht[t].y++;
    55                         if(ht[t].s==1)ht[t].x++;
    56                         if(ht[t].s==2)ht[t].y--;
    57                         if(ht[t].s==3)ht[t].x--;
    58                         flag=panduan(t);//此处开始判断 
    59                         if(!flag)break;//因为在函数中写了输出函数,此处直接退出 
    60                     }
    61                 }
    62             }
    63             else if(str=="L")
    64             {
    65                 ht[t].s=(ht[t].s-p%4+4)%4;//注意 
    66             }
    67             else if(str=="R")
    68             {
    69                 ht[t].s=(ht[t].s+p%4)%4;
    70             }
    71         }
    72         if(flag)cout<<"OK"<<endl;
    73     }
    74 }
  • 相关阅读:
    现在的代码,贴一下
    2014年七月写过的代码,现在看来,还有待改进呀
    第一次做技术论坛发博文,不知道说点啥
    04-树6. Huffman Codes--优先队列(堆)在哈夫曼树与哈夫曼编码上的应用
    04-树5. File Transfer--并查集
    04-树4. Root of AVL Tree-平衡查找树AVL树的实现
    03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历
    03-树2. List Leaves (25) 二叉树的层序遍历
    二叉树的遍历:先序中序后序遍历的递归与非递归实现及层序遍历
    最大子序列和问题之算法优化
  • 原文地址:https://www.cnblogs.com/htmrc1/p/8481349.html
Copyright © 2011-2022 走看看