Crashing Robots

Time Limit: 1000MS Memory Limit: 65536K

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


  • 题意就是给你一个图里面有很多机器人,每个机器人可以自身向左转或者右转(90度)或者向前走每个动作重复k次。输出机器人第一次撞到墙或者第一次撞到其他的机器人(机器人按照指令依次执行)。只输出第一次撞击,之后的不管。

  • 真的是反向建图日神仙,本来就是东西南北傻傻分不清,xy还反着输入,图的行列也是反着的。对于方向感不强的人来说简直是折磨啊。其实只要弄清楚方向,这个题很简单就可以过了。


#include<stdio.h>
#include<cstring> using namespace std;
struct roob
{
int x,y;
char dir[10];
} r[110];
bool r_pos[310][310];//标记图中机器人的位置
char Dir[10]= "0NESW";//用来计算机器人的转向
int n,m,x,y; void init()
{
memset(r_pos,0,sizeof(r_pos));
scanf("%d%d",&y,&x);//xy反着输入的
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
scanf("%d%d%s",&r[i].y,&r[i].x,&r[i].dir);
r_pos[r[i].x][r[i].y] = true;
}
} bool check(int w,int times)//机器人w向前走times步
{
roob now = r[w];
int x1 = now.x;
int y1 = now.y;
r_pos[x1][y1] = false;//走了之后记得取消标记
if(now.dir[0] == 'N')
{
for(int i=1; i<=times; i++)
{
int x2 = x1+i;
if(r_pos[x2][y1])
{
for(int i=1; i<=n; i++)
if(r[i].x == x2 && r[i].y == y1)
{
printf("Robot %d crashes into robot %d\n",w,i);
return true;
}
}
}
x1 += times;
}
else if(now.dir[0] == 'S')
{
for(int i=1; i<=times; i++)
{
int x2 = x1-i;
if(r_pos[x2][y1])
{
for(int i=1; i<=n; i++)
if(r[i].x == x2 && r[i].y == y1)
{
printf("Robot %d crashes into robot %d\n",w,i);
return true;
}
}
}
x1 -= times;
}
else if(now.dir[0] == 'E')
{
for(int i=1; i<=times; i++)
{
int y2 = y1+i;
if(r_pos[x1][y2])
{
for(int i=1; i<=n; i++)
if(r[i].x == x1 && r[i].y == y2)
{
printf("Robot %d crashes into robot %d\n",w,i);
return true;
}
}
}
y1 += times;
}
else if(now.dir[0] == 'W')
{
for(int i=1; i<=times; i++)
{
int y2 = y1-i;
if(y2 < 1)
continue;
if(r_pos[x1][y2])
{
for(int i=1; i<=n; i++)
if(r[i].x == x1 && r[i].y == y2)
{
printf("Robot %d crashes into robot %d\n",w,i);
return true;
}
}
}
y1 -= times;
}
if(x1<1 || y1<1 || x1>x || y1>y)//走到地图外面去了
{
printf("Robot %d crashes into the wall\n",w);
return true;
}
r_pos[x1][y1] = true;//到达新的地点被标记
r[w].x = x1;
r[w].y = y1;
return false;
} void solve()
{
bool flag = false;//标记是否发生第一次撞击
while(m--)
{
int w,times;
char d[10];
scanf("%d%s%d",&w,d,&times);
if(flag)//发生第一次撞击之后都不用管了
continue;
int temp2;
if(r[w].dir[0] == 'N')
temp2 = 1;
else if(r[w].dir[0] == 'E')
temp2 = 2;
else if(r[w].dir[0] == 'S')
temp2 = 3;
else if(r[w].dir[0] == 'W')
temp2 = 4;
if(d[0] == 'F')
flag = check(w,times);
else if(d[0] == 'L')//向左转的计算
{
times %= 4;
temp2 -= times;
if(temp2 < 1)
temp2 += 4;
r[w].dir[0] = Dir[temp2];
}
else if(d[0] == 'R')//向右转的计算
{
times %= 4;
temp2 += times;
if(temp2 > 4)
temp2 %= 4;
r[w].dir[0] = Dir[temp2];
}
}
if(!flag)
printf("OK\n");
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
solve();
}
return 0;
}

最新文章

  1. MyBatis中出现Mapped Statements collection does not contain value
  2. 【iCore3 双核心板】例程二十五:LAN_DNS实验——域名解析
  3. Web Design:欧美人形剪影的404界面
  4. java理论基础学习二
  5. SGU 174.wall
  6. hdu 2141 Can you find it?(二分查找变例)
  7. Front-End(一)
  8. 密钥public/private key登陆linux
  9. Struts 2 之类型转换器
  10. 网上Java总结
  11. C# 之 索引器
  12. c++基本数据类型及其取值范围
  13. Docker Kubernetes 容器扩容与缩容
  14. LNMP(二)
  15. eclipse修改工作目录颜色
  16. Docker实践:python应用容器化
  17. Docker部署微服务
  18. C++学习(十九)(C语言部分)之 指针3
  19. S2算法应用
  20. [转]详解C#组件开发的来龙去脉

热门文章

  1. jar包生成exe可执行程序
  2. 关于dataTable 生成JSON 树
  3. 微信开发 config:invalid url domain
  4. Wireshark漫谈(一)
  5. jsp跳转标签&lt;jsp:forward&gt;
  6. 恢复为TrustedInstaller权限
  7. BZOJ 3992: [SDOI2015]序列统计 NTT+快速幂
  8. CDOJ 485 UESTC 485 Game (八数码变形,映射,逆cantor展开)
  9. python_96_类的继承1
  10. 【转】瓜娃(guava)的API快速熟悉使用