• POJ 2632 Crashing Robots(模拟)


    Crashing Robots
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4825   Accepted: 2115

    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

    Source

    解题报告:这道题是典型的模拟题,就是在一定的区域内有若干个机器人,在执行命令时机器人必须在不同的格子中并且不能越界,转向时有规律,找出规律就减少代码量,要不然很麻烦的,其次做模拟题要仔细!
    代码如下:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    const int N = 110;
    int map[N][N];//标记变量,功能记录点的位置
    struct node1//记录点的信息
    {
    int x;
    int y;
    }a[N];
    struct node2//记录方向
    {
    int movex;
    int movey;
    }dir[N];
    void Dir(int i, char c)//确定初始化得方向
    {
    if (c == 'E')
    {
    dir[i].movex = 1;
    }
    else if (c == 'W')
    {
    dir[i].movex = -1;
    }
    else if (c == 'N')
    {
    dir[i].movey = 1;
    }
    else
    {
    dir[i].movey = -1;
    }
    }
    int main()
    {
    int t, n, m, A, B, i, flag;
    int p, q, j, nx, ny;
    char str[10];
    scanf("%d", &t);
    while (t--)
    {
    scanf("%d%d", &A, &B);
    memset(a, 0, sizeof(a));
    memset(dir, 0, sizeof(dir));
    memset(map, 0, sizeof(map));
    scanf("%d%d", &n, &m);
    for (i = 1; i <= n; ++i)
    {
    scanf("%d%d%s", &a[i].x, &a[i].y, str);
    map[a[i].x][a[i].y] = i;//记录点的位置
    Dir(i, str[0]);//确定点的方向
    }
    flag = 1;
    for (i = 1; i <= m; ++i)
    {
    scanf("%d%s%d", &p, str, &q);
    if (str[0] == 'F')
    {
    for (j = 1; j <= q && flag; ++j)
    {
    nx = a[p].x + dir[p].movex;
    ny = a[p].y + dir[p].movey;
    if (map[nx][ny])
    {
    flag = 0;
    printf("Robot %d crashes into robot %d\n", p, map[nx][ny]);
    }
    else if (nx < 1 || ny < 1 || nx > A || ny > B)
    {
    flag = 0;
    printf("Robot %d crashes into the wall\n", p);
    }
    else//点信息的更新
    {
    map[nx][ny] = p;
    map[a[p].x][a[p].y] = 0;
    a[p].x = nx;
    a[p].y = ny;
    }
    }
    }
    else if (str[0] == 'L')
    {
    if (q % 4 == 0)//转四次相当于没有变化
    {
    continue;
    }
    else if (q % 4 == 1)
    {
    if (dir[p].movey == 0)
    {
    dir[p].movey = dir[p].movex;
    dir[p].movex = 0;
    }
    else
    {
    dir[p].movex = -dir[p].movey;
    dir[p].movey = 0;
    }
    }
    else if (q % 4 == 2)
    {
    dir[p].movex = -dir[p].movex;
    dir[p].movey = -dir[p].movey;
    }
    else if (q % 4 == 3)
    {
    if (dir[p].movey == 0)
    {
    dir[p].movey = -dir[p].movex;
    dir[p].movex = 0;
    }
    else
    {
    dir[p].movex = dir[p].movey;
    dir[p].movey = 0;
    }
    }
    }
    else if (str[0] == 'R')
    {
    if (q % 4 == 0)
    {
    continue;
    }
    else if (q % 4 == 1)
    {
    if (dir[p].movex == 0)
    {
    dir[p].movex = dir[p].movey;
    dir[p].movey = 0;
    }
    else
    {
    dir[p].movey = - dir[p].movex;
    dir[p].movex = 0;
    }
    }
    else if (q % 4 == 2)
    {
    dir[p].movex = - dir[p].movex;
    dir[p].movey = - dir[p].movey;
    }
    else if (q % 4 == 3)
    {
    if (dir[p].movex == 0)
    {
    dir[p].movex = -dir[p].movey;
    dir[p].movey = 0;
    }
    else
    {
    dir[p].movey = dir[p].movex;
    dir[p].movex = 0;
    }
    }
    }
    }
    if (flag)
    {
    printf("OK\n");
    }
    }
    return 0;
    }
  • 相关阅读:
    时空权衡之计数排序
    何时发生隐式类型转换
    常量指针与指针常量的区别
    虚函数有关面试题
    C++中数组定义及初始化
    InputStream类的available()方法
    FORK()函数
    面向对象三大基本特性,五大基本原则
    SpringMVC工作原理
    java文件的上传
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2367584.html
Copyright © 2020-2023  润新知