POJ2632——Crashing Robots

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

题目大意:给定一个A*B的棋盘,N个机器人,每个机器人都有起始位置,M个指令(x,C,r)代表第x个机器人执行指令C重复r次。
    F->向前走一步
    L->向左转
    R->向右转

    若i号机器人撞墙,输出:Robot i crashes into the wall
    若i号机器人撞到j号机器人,输出:Robot i
crashes into robot j
    若M个指令执行完仍无事故发生 输出:OK

结题思路:模拟,写的比较长。。。

Code:


 1 #include<string>
2 #include<iostream>
3 #include<stdio.h>
4 #define INTO_WALL 0
5 #define INTO_ROBIT -1
6 #define SAFE 1
7 using namespace std;
8 struct Robit
9 {
10 int x,y;
11 int dir;
12 } R[10000];
13 int date1,date2,N,T,M,A,B;;
14 void step(int i,int dir)
15 {
16 if (dir==1) R[i].y++;
17 if (dir==2) R[i].x++;
18 if (dir==3) R[i].y--;
19 if (dir==4) R[i].x--;
20 }
21 int move(int i,char dir,int dis)
22 {
23 if (dir==‘L‘)
24 for (int j=1; j<=dis; j++)
25 R[i].dir=(R[i].dir-1)?(R[i].dir-1):4;
26 else if (dir==‘R‘)
27 for (int j=1; j<=dis; j++)
28 R[i].dir=(R[i].dir-4)?(R[i].dir+1):1;
29 else
30 {
31 for (int j=1; j<=dis; j++)
32 {
33 step(i,R[i].dir);
34 if ((R[i].x>=A+1||R[i].x<=0)||(R[i].y>=B+1||R[i].y<=0))
35 {
36 date1=i;
37 return INTO_WALL;
38 }
39 for (int k=1; k<=N; k++)
40 {
41 if (k==i) continue;
42 if (R[k].x==R[i].x&&R[k].y==R[i].y)
43 {
44 date1=i,date2=k;
45 return INTO_ROBIT;
46 }
47 }
48 }
49 }
50 return SAFE;
51 }
52 int main()
53 {
54 int flag=0,tmp,meter,ok,i;
55 char tdir,ctmp;
56 cin>>T;
57 while (T--)
58 {
59 flag=0;
60 cin>>A>>B;
61 cin>>N>>M;
62 for (i=1; i<=N; i++)
63 {
64 cin>>R[i].x>>R[i].y>>ctmp;
65 if (ctmp==‘N‘) R[i].dir=1;
66 if (ctmp==‘E‘) R[i].dir=2;
67 if (ctmp==‘S‘) R[i].dir=3;
68 if (ctmp==‘W‘) R[i].dir=4;
69 }
70 for (i=1; i<=M; i++)
71 {
72 cin>>tmp>>tdir>>meter;
73 if (flag) continue;
74 ok=move(tmp,tdir,meter);
75 if (ok==SAFE) continue;
76 else if (ok==INTO_ROBIT)
77 {
78 printf("Robot %d crashes into robot %d\n",date1,date2);
79 flag=1;
80 }
81 else if (ok==INTO_WALL)
82 {
83 printf("Robot %d crashes into the wall\n",date1);
84 flag=1;
85 }
86 }
87 if (!flag) printf("OK\n");
88 }
89 return 0;
90 }

POJ2632——Crashing Robots,布布扣,bubuko.com

时间: 2024-11-04 21:04:03

POJ2632——Crashing Robots的相关文章

poj 2632 Crashing Robots, 模拟

点击打开链接 简单 模拟机器人的移动,发生碰撞时输出相应的信息. code #include <cstdio> #include <cstring> using namespace std; struct node{ int k; int s; } mtx[200][200]; struct node1{ int x, y; } rob[200]; int n, m; int dir[4][2]= {{0,1},{1,0},{0,-1},{-1,0}}; //N, E, S, W

Crashing Robots(imitate)

Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8124   Accepted: 3528 Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destination

POJ 2632 Crashing Robots(模拟题)

Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7880   Accepted: 3429 Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destination

poj -2632 Crashing Robots

http://poj.org/problem?id=2632 Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7470   Accepted: 3265 Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the

POJ 2632:Crashing Robots

Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8424   Accepted: 3648 Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is needed to ensure that the robots reach their destination

POJ 2632 Crashing Robots (模拟 坐标调整)(fflush导致RE)

题目链接:http://poj.org/problem?id=2632 先话说昨天顺利1Y之后,直到今天下午才再出题 TAT,真是刷题计划深似海,从此AC是路人- - 本来2632是道稍微恶心点的模拟,但毕竟是模拟,一般模拟都是只要示例过了基本就AC了,但这个题很特殊 我开始的时候一直跑不出测试示例,才发现题目中的插图和我构想的坐标系是不一样的(看来还不能轻易忽视掉插图啊) 这个题给出的坐标系是纵轴为y,横轴为x,y从下到上依次递增,x轴是从左到右递增 而我用的二维数组记录的地图,也就是说我的纵

Poj OpenJudge 百练 2632 Crashing Robots

1.Link: http://poj.org/problem?id=2632 http://bailian.openjudge.cn/practice/2632/ 2.Content: Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7912   Accepted: 3441 Description In a modernized warehouse, robots are used to

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 withou

poj2632(Crashing Robots)

题目大意: 一个仓库有N个机器人在移动,仓库的大小为A,B.会有M次操作,(E,W,S,N)分别代表东西南北,机器人在仓库里会有三种情况:1.正在移动的机器人i撞墙.2.正在移动的机器人i遇见机器人j.3.所有机器人没发生任何意外(OK).根据操作,输出可能的结果. 数据分析 4 //代表几组测试案例 5 4 //代表仓库的大小 2 2  //代表有两个机器人进行两次操作    1 1 E  //代表第一个机器人的位置和方向    5 4 W //代表第二个机器人的位置和方向    1 F 7