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 }

原文地址:https://www.cnblogs.com/htmrc1/p/8481349.html

时间: 2024-10-10 19:44:17

Crashing Robots(水题,模拟)的相关文章

CodeForces 686A Free Ice Cream (水题模拟)

题意:给定初始数量的冰激凌,然后n个操作,如果是“+”,那么数量就会增加,如果是“-”,如果现有的数量大于等于要减的数量,那么就减掉,如果小于, 那么孩子就会离家.问你最后剩下多少冰激凌,和出走的孩子数量. 析:多水的一个题,就是一个模拟,如果是+,就加上,如果是‘-’,就判断一下,如果不够,就记录下来. 代码如下: #include <iostream> #include <cmath> #include <cstdlib> #include <set>

ACM: NBUT 1105 多连块拼图 - 水题 - 模拟

NBUT 1105  多连块拼图 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format: Practice Appoint description:  System Crawler  (Aug 12, 2016 9:32:14 AM) Description 多连块是指由多个等大正方形边与边连接而成的平面连通图形. -- 维基百科 给一个大多连块和小多连块,你的任务是判断大多连块是否可以由两个这样的小多连块拼成.小多连块只能

CodeForces 342B Xenia and Spies (水题模拟,贪心)

题意:给定 n 个间谍,m个区间,一个 s,一个f,然后从 s开始传纸条,然后传到 f,然后在每个 t 时间在区间内的不能传,问你最少的时间传过去. 析:这个题,就模拟一下就好,贪心策略,能传就传,找好方向,一直传就行,传到 f 就结束. 代码如下: #include <bits/stdc++.h> using namespace std; struct node{ int l, r, t; bool operator < (const node &p) const{ retur

CodeForces 731B Coupons and Discounts (水题模拟)

题意:有n个队参加CCPC,然后有两种优惠方式,一种是一天买再次,一种是买两天,现在让你判断能不能找到一种方式,使得优惠不剩余. 析:直接模拟,如果本次是奇数,那么就得用第二种,作一个标记,再去计算下一个. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #inclu

CodeForces 723B Text Document Analysis (水题模拟)

题意:给定一行字符串,让你统计在括号外最长的单词和在括号内的单词数. 析:直接模拟,注意一下在左右括号的时候有没有单词.碰到下划线或者括号表示单词结束了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include

2017.9.10所谓“切题如切菜杯”水题模拟赛(;&#180;д`)ゞ

T1:MHM LGL今天一共要上n节课,这n节课由0标号至n.由于过度劳累,除了第0节课和第n节课,LGL还打算睡上m节课,所以他做了一个睡觉计划表.通过小道消息,LGL得知WQ今天会在学校中检查,所以他想少睡k节课.但是由于某些原因,他又想使相邻的两节睡觉的课之间上的课数量的最小值最大.由于他很困,所以他请你来帮他计算这个值.   输入格式: 第一行为三个整数 n.m.k,接下来的m行为m个整数ai,表示睡觉计划表中LGL想要睡觉的课. 输出格式: 一个整数,表示题目所求的值. 样例输入 样例

CodeForces 339B Xenia and Ringroad(水题模拟)

题意:给定 n 个地方,然后再给 m 个任务,每个任务必须在规定的地方完成,并且必须按顺序完成,问你最少时间. 析:没什么可说的,就是模拟,记录当前的位置,然后去找和下一个位置相差多长时间,然后更新当前位置即可. 代码如下: #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; typedef long long LL; int main(){ int n, m, x; while(cin >>

20170925“切题如切菜杯”水题模拟赛 第二弹

T1:短 给出一张有n个点和m条双向边的图,要求求出1到n的次短路的长度.一条边可以多次通过. 输入格式: 第一行为两个整数n和m.接下来的m行每行三个整数ai,bi,vi,分别表示这条路连着的两个点和他的长度. 输出格式: 一个整数,表示次短路的长度. 样例输入 样例输出 4 41 2 1002 4 2002 3 2503 4 100 450 样例解释: 最短:1->2->4. 次短:1->2->3->4. 数据范围: 对于 100%的数据:1<=n.vi<=5

Codeforces-Translation(水题)

The translation from the Berland language into the Birland language is not an easy task. Those languages are very similar: a berlandish word differs from a birlandish word with the same meaning a little: it is spelled (and pronounced) reversely. For