1. 程式人生 > >poj 2632 Crashing Robots【模擬】【略坑~】

poj 2632 Crashing Robots【模擬】【略坑~】

att sed other dia star contain width seq map

Crashing Robots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11233 Accepted: 4753

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

題意:讀入t組數據,每組讀入地圖的列數c和行數r,再讀入n和m,接下來輸入n行(機器人編號為1——n),每行輸入機器人的起始位置x,y,和起始方向,
最後輸入m行,每行輸入一條命令,命令內容包括機器人的號數numebr和機器人的方向direction以及該條命令的執行次數step。
如果有兩個機器人相撞,按題目輸出,如果有機器人越界,按題目輸出,如果沒有撞擊產生,輸出OK。
題目需要註意的地方是:
1.只需要輸出第一次相撞的情況.
2.機器人執行向左或向右的step次命令後,還是在原地,只是方向不停改變(這點好坑啊,自己理解錯了,以為是向左轉走step步)
3.由於二維數組建圖後坐標原點也會不同,所以我們的方向也需要改變,具體依照自己的習慣建圖就好(這個容易暈,最好先在草稿上寫好)
思路:
從東方E開始把方向逆時針標記為0~3,向左時,方向+1,向右時,方向-1,判斷存儲地圖的二維數組在相應坐標下是否有機器人即可判斷兩機器人是否相撞。。。。嗯嗯,感覺瑣碎啊,還是看註釋好了
-------------------
老師:每次寫題前想想出題人的意圖。
我:大概出模擬題的出題人都是想讓我死。
還是保命要緊。
#include<stdio.h>
#include<string.h>
#define N 110

struct node{
    int x,y,dir;
}num[N];

struct dir{
    int number,step;
    char direction;
}str[N];

int main()
{
    int t,i,j;
    int map[N][N];
    int k[4][2] = {0,1,1,0,0,-1,-1,0};
    int n,m,c,r,step,number,flag;
    char direction;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&c,&r);
        scanf("%d%d",&n,&m);
        memset(map,0,sizeof(map));
        for(i = 1; i <= n; i ++)
        {
            scanf("%d %d %c",&num[i].y,&num[i].x ,&direction);
            map[num[i].x][num[i].y] = i;//機器人未移動時,地圖上標記為機器人的號數 
            if(direction == E)
                num[i].dir = 0;
            else if(direction == N)
                num[i].dir = 1;
            else if(direction == W)
                num[i].dir = 2;
            else if(direction == S)
                num[i].dir = 3;
        }
        for(i = 1; i <= m; i ++)
            scanf("%d %c %d",&str[i].number,&str[i].direction ,&str[i].step );
        flag = 0;//標記機器人是否相撞 
        for(i = 1; i <= m; i ++)
        {
            direction = str[i].direction ;
            number = str[i].number ;
            step = str[i].step ;
            if(direction == L)//執行左轉命令 
            {
                for(j = 1; j <= step; j ++)
                    num[number].dir = (num[number].dir+1)%4;
            }    
            else if(direction == R)//執行右轉命令 
                for(j = 1; j <= step; j ++)
                    num[number].dir = (num[number].dir-1+4)%4;
            if(step > 0&&direction == F)//當移動步數大於1時 
            {
                map[num[number].x][num[number].y] = 0;//地圖上標記的機器人位置清空
                for(j = 1; j <= step; j ++)
                {
                    num[number].x += k[num[number].dir][0];
                    num[number].y += k[num[number].dir][1];
                    if(map[num[number].x][num[number].y] != 0)//地圖上該處不為空,說明有機器人 
                    {
                        flag = 1;
                        break;
                    }
                    else if(num[number].x < 1||num[number].x > r||num[number].y < 1||num[number].y > c)
                    {//越界 
                        flag = 2;
                        break;
                    }
                }
                if(flag == 1)//如果機器人相撞 
                {
                    printf("Robot %d crashes into robot %d\n",number,map[num[number].x][num[number].y]);
                    break;
                }
                else if(flag == 2)//如果機器人撞到墻 
                {
                    printf("Robot %d crashes into the wall\n",number);
                    break;
                }
            }
            map[num[number].x][num[number].y ] = number;//地圖上機器人的停止點標記為機器人號數     
        }
        if(!flag)
            printf("OK\n");
    }
    return 0;
}

poj 2632 Crashing Robots【模擬】【略坑~】