Description
Some robots are walking in the house. They need to complete the commands without colliding with each other. The map is rectangular, and each Robot occupies a grid, we know the initial location and direction of the machine operator, as well as a series of commands. commands are ordered, and no two commands occur simultaneously. For example:
Input
Multiple groups of data, number of the first row of Data K
Each group of data starts with two integers n, m, indicating the size of the map, n is the size of the W-E direction, M is the size of the N-S direction (n, m <= 100)
The next line contains two integers: p, q (p, q <= 100), indicating P robots and Q commands.
Next, P lines describe the initial information, location (x, y) of the robot)
(1 <= x <= N, 1 <= Y <= m), and a letter indicates the direction ('n', 's', 'E ', 'W ')
Next, the Q-line describes the instruction. The instruction structure is as follows:
<Robot I> <action> <repeat>
Indicates the robot, Action type, and number of repetitions.
There are three types of actions
- 'L': 90 degrees left
- 'R': Right turn 90 degrees
- 'F': Forward a grid
Repeat times will not exceed 100.
In the event of a collision, the robot stops running and outputs relevant error information.
Output
There are 3 types:
If the I-th robot (x = 0 or Y = 0 or X = n + 1 or Y = m + 1), output the robot I crashes into the wall
If I collide with J, I is the mobile robot that outputs robot I crashes into robot J
If no collision occurs, output OK
Sample input 2
5 4
2 2
1 1 E
5 4 W
1 F 7
2 F 7
5 4
2 2
1 1 E
5 4 W
1 L 96
1 F 2 sample output
Robot 1 crashes into the wall
OK
Solution:
This is a simulated question. You only need to follow the meaning of the question.
I used two arrays of structures, one for storing the location and orientation of the robot, and the other for marking the map size and whether there are other robots in each grid of the map.
When a robot moves, it first determines whether it is out of bounds and then whether it hits another robot. Follow the input sample of the program.
#include <stdio.h>#include <stdlib.h>#include <string.h>struct s{char f;int x;int y;};struct pp{int up;int ss;};main(){struct s robot[101];struct pp a[102][102]; int number,te; int i,j;int n,m;int p,q;int tee;int x1,y1;char face;int num;char action;int repeat;int flag;scanf("%d",&number);for(te=1;te<=number;te++){scanf("%d %d",&n,&m);for(i=0;i<102;i++)for(j=0;j<102;j++){ a[i][j].up=-1;a[i][j].ss=0; }for(i=1;i<=m;i++)for(j=1;j<=n;j++)a[i][j].up=0;scanf("%d %d",&p,&q);for(tee=1;tee<=p;tee++){scanf("%d %d %c",&x1,&y1,&face);robot[tee].f=face;robot[tee].x=x1;robot[tee].y=y1;a[m-y1+1][x1].up=1;a[m+1-y1][x1].ss=tee;}flag=0;for(tee=1;tee<=q;tee++){scanf("%d %c %d",&num,&action,&repeat);if(action=='L'){repeat=repeat%4;if(repeat==1){if(robot[num].f=='E'){robot[num].f='N';goto ABC;}if(robot[num].f=='N'){robot[num].f='W';goto ABC;}if(robot[num].f=='W'){robot[num].f='S';goto ABC; }if(robot[num].f=='S'){robot[num].f='E';goto ABC;}}if(repeat==2){if(robot[num].f=='E'){robot[num].f='W';goto ABC;}if(robot[num].f=='N'){robot[num].f='S';goto ABC;}if(robot[num].f=='W'){robot[num].f='E';goto ABC; }if(robot[num].f=='S'){robot[num].f='N';goto ABC;}}if(repeat==3){if(robot[num].f=='E'){robot[num].f='S';goto ABC;}if(robot[num].f=='N'){robot[num].f='E';goto ABC;}if(robot[num].f=='W'){robot[num].f='N';goto ABC; }if(robot[num].f=='S'){robot[num].f='W';goto ABC;}}} if(action=='R'){repeat=repeat%4;if(repeat==1){if(robot[num].f=='E'){robot[num].f='S';goto ABC;}if(robot[num].f=='N'){robot[num].f='E';goto ABC;}if(robot[num].f=='W'){robot[num].f='N';goto ABC; }if(robot[num].f=='S'){robot[num].f='W';goto ABC;}}if(repeat==2){if(robot[num].f=='E'){robot[num].f='W';goto ABC;}if(robot[num].f=='N'){robot[num].f='S';goto ABC;}if(robot[num].f=='W'){robot[num].f='E';goto ABC; }if(robot[num].f=='S'){robot[num].f='N';goto ABC;}}if(repeat==3){if(robot[num].f=='E'){robot[num].f='N';goto ABC;}if(robot[num].f=='N'){robot[num].f='W';goto ABC;}if(robot[num].f=='W'){robot[num].f='S';goto ABC; }if(robot[num].f=='S'){robot[num].f='E';goto ABC;}}}if(action=='F'){ if(robot[num].f=='E'){for(i=1;i<=repeat;i++){ a[m+1-robot[num].y][robot[num].x].up=0;a[m+1-robot[num].y][robot[num].x].ss=0;robot[num].x++;if(a[m+1-robot[num].y][robot[num].x].up==-1){printf("Robot %d crashes into the wall\n",num);flag=1;goto ABC;}if(a[m+1-robot[num].y][robot[num].x].ss!=0){printf("Robot %d crashes into robot %d\n",num,a[m+1-robot[num].y][robot[num].x].ss);flag=1;goto ABC;}a[m+1-robot[num].y][robot[num].x].up=1;a[m+1-robot[num].y][robot[num].x].ss=num;}}if(robot[num].f=='N'){for(i=1;i<=repeat;i++){ a[m+1-robot[num].y][robot[num].x].up=0;a[m+1-robot[num].y][robot[num].x].ss=0;robot[num].y++;if(a[m+1-robot[num].y][robot[num].x].up==-1){printf("Robot %d crashes into the wall\n",num);flag=1;goto ABC;}if(a[m+1-robot[num].y][robot[num].x].ss!=0){printf("Robot %d crashes into robot %d\n",num,a[m+1-robot[num].y][robot[num].x].ss);flag=1;goto ABC;}a[m+1-robot[num].y][robot[num].x].up=1;a[m+1-robot[num].y][robot[num].x].ss=num;}}if(robot[num].f=='W'){for(i=1;i<=repeat;i++){ a[m+1-robot[num].y][robot[num].x].up=0;a[m+1-robot[num].y][robot[num].x].ss=0;robot[num].x--;if(a[m+1-robot[num].y][robot[num].x].up==-1){printf("Robot %d crashes into the wall\n",num);flag=1;goto ABC;}if(a[m+1-robot[num].y][robot[num].x].ss!=0){printf("Robot %d crashes into robot %d\n",num,a[m+1-robot[num].y][robot[num].x].ss);flag=1;goto ABC;}a[m+1-robot[num].y][robot[num].x].up=1;a[m+1-robot[num].y][robot[num].x].ss=num;}}if(robot[num].f=='S'){for(i=1;i<=repeat;i++){ a[m+1-robot[num].y][robot[num].x].up=0;a[m+1-robot[num].y][robot[num].x].ss=0;robot[num].y--;if(a[m+1-robot[num].y][robot[num].x].up==-1){printf("Robot %d crashes into the wall\n",num);flag=1;goto ABC;}if(a[m+1-robot[num].y][robot[num].x].ss!=0){printf("Robot %d crashes into robot %d\n",num,a[m+1-robot[num].y][robot[num].x].ss);flag=1;goto ABC;}a[m+1-robot[num].y][robot[num].x].up=1;a[m+1-robot[num].y][robot[num].x].ss=num;}}}ABC: if(flag==1) break; else continue; }for(i=tee;i<q;i++) scanf("%d %c %d",&num,&action,&repeat);if(flag==0) printf("OK\n");}}