POJ 2632 Crashing Robots 模拟的方法

   题目:http://poj.org/problem?id=2632

   题目大意:在一个棋盘上,几只坦克在上面轮流运动,有三种运动方式,L,R,F,L:向左转90度,t次,R:向右转90度,t次,F:向前走一步,t次。找出第一次发生碰撞的可能性。思路很简单,使用模拟的方法。

   代码如下:

#include <stdio.h>
#include <string>

#define  ONLINE

void online()
{
#ifdef ONLINE
#else
	freopen("2632.in","r", stdin);
	freopen("2632.out", "w", stdout);
#endif
}

struct Robot
{
	int x;
	int y;
	int d;
};

struct Instructor
{
	int r; //坦克
	char o; //指令
	int t; // 时间
};

const int N = 0;
const int E= 1;
const int S = 2;
const int W = 3;

const int LEN = 101;

int a, b;
int n, m;
int map[LEN][LEN];
Robot robots[LEN];
int cas;
Instructor ins[LEN];

//打印结果
void print(int r1, int r2)
{
	if (r1 == -1 && r2==-1)
	{
		printf("OK\n");
	}
	else if (r1 != -1 && r2 == -1)
	{
		printf("Robot %d crashes into the wall\n", r1);
	}
	else
		printf("Robot %d crashes into robot %d\n", r1, r2);

}

//查找相撞的坦克
int search(int x, int y, int idx)
{
	for (int i=1; i <= n; i ++)
	{
		if (robots[i].x == x && robots[i].y == y&& i != idx)
		{
			return i;
		}
	}
	return -1;
}

//开始模拟
void imitate()
{
	for (int i=0; i < m; i ++)
	{
		int idx = ins[i].r;

		if (ins[i].o == 'L')
		{
			robots[idx].d = (robots[idx].d + 3 * ins[i].t) % 4;
		}
		else if (ins[i].o == 'R')
		{
			robots[idx].d = (robots[idx].d + ins[i].t) % 4;
		}
		else if (ins[i].o == 'F')
		{
			for (int j=0; j < ins[i].t; j ++)
			{
				if (robots[idx].d == N)
				{
					map[robots[idx].x][robots[idx].y] = 0;
					robots[idx].y ++;

					if (robots[idx].y  > b)
					{
						print(idx, -1);
						return;
					}
					else if (map[robots[idx].x][robots[idx].y] == 1)
					{
						int r2 = search(robots[idx].x, robots[idx].y, idx);
						print(idx, r2);
						return;
					}
					else
					{
						map[robots[idx].x][robots[idx].y] = 1;
					}//end else
				}//end N
				else if (robots[idx].d == E)
				{
					map[robots[idx].x][robots[idx].y] = 0;
					robots[idx].x ++;

					if (robots[idx].x  > a)
					{
						print(idx, -1);
						return;
					}
					else if (map[robots[idx].x][robots[idx].y] == 1)
					{
						int r2 = search(robots[idx].x, robots[idx].y, idx);
						print(idx, r2);
						return;
					}
					else
					{
						map[robots[idx].x][robots[idx].y] = 1;
					}//end else
				}//end E
				else if (robots[idx].d == S)
				{
					map[robots[idx].x][robots[idx].y] = 0;
					robots[idx].y --;

					if (robots[idx].y  <= 0)
					{
						print(idx, -1);
						return;
					}
					else if (map[robots[idx].x][robots[idx].y] == 1)
					{
						int r2 = search(robots[idx].x, robots[idx].y, idx);
						print(idx, r2);
						return;
					}
					else
					{
						map[robots[idx].x][robots[idx].y] = 1;
					}//end else
				}//end S
				else if (robots[idx].d == W)
				{
					map[robots[idx].x][robots[idx].y] = 0;
					robots[idx].x --;

					if (robots[idx].x  <= 0)
					{
						print(idx, -1);
						return;
					}
					else if (map[robots[idx].x][robots[idx].y] == 1)
					{
						int r2 = search(robots[idx].x, robots[idx].y, idx);
						print(idx, r2);
						return;
					}
					else
					{
						map[robots[idx].x][robots[idx].y] = 1;
					}//end else
				}//end W
			}
		}//end F
	}//end for
	print(-1, -1);
}

//读入数据
void read()
{
	scanf("%d", &cas);

	while(cas > 0)
	{
		scanf("%d%d", &a, &b);
		scanf("%d%d", &n, &m);

		memset(map,0, LEN * LEN * sizeof(int));
		for(int i=1; i <= n; i ++)
		{
			int x, y;
			char c;
			scanf( "%d%d %c", &x, &y, &c);
			map[x][y] = 1;
			robots[i].x = x;
			robots[i].y = y;

			if (c == 'N')
			{
				robots[i].d = N;
			}
			else if (c == 'E')
			{
				robots[i].d = E;
			}
			else if (c == 'S')
			{
				robots[i].d = S;
			}
			else if (c == 'W')
			{
				robots[i].d = W;
			}
		}//end for

		for (int i=0; i < m; i ++)
		{
			scanf("%d %c %d", &ins[i].r, &ins[i].o, &ins[i].t);
		}//end for instructor

		imitate();
		cas --;
	}
}

int main()
{
	online();
	read();

	return 0;
}
   运行结果如下:

2632 Accepted 208K 0MS C++ 3940B 2011-08-02 13:44:18


你可能感兴趣的:(POJ 2632 Crashing Robots 模拟的方法)