首页 > ACM题库 > HDU-杭电 > hdu 2300 Crashing Robots-模拟-[解题报告]C++
2014
01-05

hdu 2300 Crashing Robots-模拟-[解题报告]C++

Crashing Robots

问题描述 :

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.

输出:

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.

样例输入:

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

#include <iostream>
using namespace std;
const int M = 110;
int map[M][M];
int xy[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
bool reported;
int A,B,n,m;
struct Robot
{
	int x, y;
	int d;
}r[M];

int dir(char d)
{
	if(d=='S') return 0;
	if(d=='E') return 1;
	if(d=='N') return 2;
	if(d=='W') return 3;
}
void move(int id, int times)
{
	int d = r[id].d;
	map[r[id].x][r[id].y]=0;
	for(int i=0; i<times; i++)
	{
		r[id].x += xy[d][0];
		r[id].y += xy[d][1];
		if((!r[id].x||r[id].x>A) || (!r[id].y||r[id].y>B))
		{
			reported = true;
			printf("Robot %d crashes into the wall\n", id);
			return ;
		}
		if(map[r[id].x][r[id].y]!=0)
		{
			reported = true;
			printf("Robot %d crashes into robot %d\n", id, map[r[id].x][r[id].y]);
			return ;
		}
	}
	map[r[id].x][r[id].y] = id;
}
int main()
{
	int i,t,id,times;
	char d;
	scanf("%d", &t);
	while(t--)
	{
		memset(map, 0, sizeof(map));
		reported = false;
		scanf("%d %d", &A, &B);
		scanf("%d %d", &n, &m);
		for(i=1; i<=n; i++)
		{
			scanf("%d %d %c", &r[i].x, &r[i].y, &d);
			r[i].d = dir(d);
			map[r[i].x][r[i].y] = i;
		}
		for(i=0; i<m; i++)
		{
			scanf("%d %c %d",&id, &d, ×);
			if(d=='F' && !reported) move(id, times);
			else if(d=='L' && !reported)
			{
				times %=4;
				r[id].d = (r[id].d+times)%4;
			}
			else if(d=='R' && !reported)
			{
				times %= 4;
				r[id].d = (r[id].d-times)%4;
				if(r[id].d<0) r[id].d +=4;
			}
		}
		if(!reported) printf("OK\n");
	}
	return 0;
}

解题转自:http://blog.csdn.net/jun_sky/article/details/7006151


  1. I like your publish. It is great to see you verbalize from the coronary heart and clarity on this essential subject matter can be easily noticed.

  2. 代码是给出了,但是解析的也太不清晰了吧!如 13 abejkcfghid jkebfghicda
    第一步拆分为 三部分 (bejk, cfghi, d) * C(13,3),为什么要这样拆分,原则是什么?

  3. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。