poj-2632 Crashing Robots

7 篇文章 0 订阅

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

    这道题主要注意题中给的坐标和数组的坐标不是相同的,要用二维数组存机器人位置的话首先找好题中坐标所指的东西南北在二维数组坐标中的方向。题中输入数据可能也会出现问题,因为输入的数据包括整形和字符,连续输入可能会让字符吃掉输入的空格如果要解决这个问题有两个方法。一个是用字符串存一个字符然后用字符串的第0位,另一个用scanf("%d%*c%c%d",&a,&b,&c);或scanf("%d %c%d",&a,&b,&c);的输入形式输入。本人错了好几次是因为用了 fflush(stdin);-清空输入输出缓存 删掉后就能AC。

以下是代码以及32组测试数据的网址:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

using namespace std;

struct node
{
    int x,y;
    int c;
}robo[150];

int map[150][150];
int ask_no,ask_step,n,m;
char ask;
int flag;
int rem_no1,rem_no2;

void askdo()
{
    int x,y;
    x=robo[ask_no].x;
    y=robo[ask_no].y;
    if(ask=='L')
    {
        ask_step %= 4;
        if(ask_step!=0)
        robo[ask_no].c += (4-ask_step);
        if(robo[ask_no].c>4)
        robo[ask_no].c %= 4 ;
        //printf("״̬%d %d %d\n",robo[ask_no].x,robo[ask_no].y,robo[ask_no].c);
    }
    else if(ask=='R')
    {
        ask_step %= 4;
        robo[ask_no].c += ask_step;
        if(robo[ask_no].c>4)
        robo[ask_no].c %= 4 ;
        //printf("״̬%d %d %d\n",robo[ask_no].x,robo[ask_no].y,robo[ask_no].c);
    }
    else if(ask=='F')
    {
        switch(robo[ask_no].c)
        {
        case 1:
            {
                map[x][y]=0;
                while (ask_step--)
                {
                    x--;
                    if(map[x][y]!=0)
                    {
                        flag=1;
                        rem_no1=ask_no;
                        rem_no2=map[x][y];
                        break;
                    }
                    else if(x==0||y==0||x>n||y>m)
                    {
                        rem_no1=ask_no;
                        flag=2;
                        break;
                    }
                }
                robo[ask_no].x=x;
                robo[ask_no].y=y;
                //printf("״̬%d %d %d\n",robo[ask_no].x,robo[ask_no].y,robo[ask_no].c);
                map[x][y]=ask_no;
                break;
            }
        case 2:
            {
                map[x][y]=0;
                while (ask_step--)
                {
                    y++;
                    if(map[x][y]!=0)
                    {
                        flag=1;
                        rem_no1=ask_no;
                        rem_no2=map[x][y];
                        break;
                    }
                    else if(x==0||y==0||x>n||y>m)
                    {
                        rem_no1=ask_no;
                        flag=2;
                        break;
                    }
                }
                robo[ask_no].x=x;
                robo[ask_no].y=y;
                //printf("״̬%d %d %d\n",robo[ask_no].x,robo[ask_no].y,robo[ask_no].c);
                map[x][y]=ask_no;
                break;
            }
        case 3:
            {
                map[x][y]=0;
                while (ask_step--)
                {
                    x++;
                    if(map[x][y]!=0)
                    {
                        flag=1;
                        rem_no1=ask_no;
                        rem_no2=map[x][y];
                        break;
                    }
                    else if(x==0||y==0||x>n||y>m)
                    {
                        rem_no1=ask_no;
                        flag=2;
                        break;
                    }
                }
                robo[ask_no].x=x;
                robo[ask_no].y=y;
                //printf("״̬%d %d %d\n",robo[ask_no].x,robo[ask_no].y,robo[ask_no].c);
                map[x][y]=ask_no;
                break;
            }
        case 4:
            {
                map[x][y]=0;
                while (ask_step--)
                {
                    y--;
                    if(map[x][y]!=0)
                    {
                        flag=1;
                        rem_no1=ask_no;
                        rem_no2=map[x][y];
                        break;
                    }
                    else if(x==0||y==0||x>n||y>m)
                    {
                        rem_no1=ask_no;
                        flag=2;
                        break;
                    }
                }
                robo[ask_no].x=x;
                robo[ask_no].y=y;
                //printf("״̬%d %d %d\n",robo[ask_no].x,robo[ask_no].y,robo[ask_no].c);
                map[x][y]=ask_no;
                break;
            }
        }
    }

}
int main()
{
    //freopen("out.txt","w",stdout);
    int t,pos_num,ask_num;
    int x,y,i;
    char c[2];
    scanf("%d",&t);
    while(t--)
    {
        flag=0;
        memset(map,0,sizeof(map));
        scanf("%d%d",&n,&m);
        scanf("%d%d",&pos_num,&ask_num);

        for (i=1;i<=pos_num;i++)
        {
            scanf("%d%d%s",&x,&y,c);
            map[x][y]=i;
            robo[i].x=x;
            robo[i].y=y;
            switch(c[0])
            {
                case 'N':robo[i].c=2;break;
                case 'E':robo[i].c=3;break;
                case 'S':robo[i].c=4;break;
                case 'W':robo[i].c=1;break;
            }

        }
        for (i=1;i<=ask_num;i++)
        {
            scanf("%d %c%d",&ask_no,&ask,&ask_step);
            if(flag==0)
            {
                askdo();
                //printf("flag=%d\n",flag);
            }

        }
        if(flag==1)
        {
            printf("Robot %d crashes into robot %d\n",rem_no1,rem_no2);
        }
        else if(flag==2)
        {
            printf("Robot %d crashes into the wall\n",rem_no1);
        }
        else if(flag==0) printf("OK\n");
    }
    return 0;
}


32组测试数据:http://poj.org/showmessage?message_id=130353

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值