您的位置:首页 > 其它

poj 2632 Crashing Robots 模拟

2014-12-21 13:07 429 查看
题目链接:

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

题目描述:

  有一个B*A的厂库,分布了n个机器人,机器人编号1~n。我们知道刚开始时全部机器人的位置和朝向,我们可以按顺序操控机器人,没有两个机器人可以同时执行命令。如果机器人走到厂库边界,他将碰到墙,如果两个机器人走到同一位置,则表示他们两个相撞。问m个命令内最先发生的碰撞,如果没有碰撞输出“OK”。

解题思路:

  

由图可知,本题的矩阵与平时的不太一样,所以我们在对厂库进行操作的时候,可以先把厂库顺时针旋转90°,当然,方向也要跟着旋转90°。左右方向是不会跟着变的。然后老老实实的按照命令去模拟,就大功告成啦。

代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;

#define maxn 105

struct node
{
int x, y, s;
} stu[maxn];//每个机器人的位置(x,y),和方向s

int dir[4][2] = {-1,0, 0,1, 1,0, 0,-1};//翻转后的四个方向:W,N,E,S
int map[maxn][maxn];

int main()
{
int t, A, B, n, m, i, j, flag, x, y, num1, num2;
char ch[2];

scanf ("%d", &t);
while (t --)
{
flag = 0;//是否发生碰撞
scanf ("%d %d", &A, &B);
scanf ("%d %d", &n, &m);
memset (map, 0, sizeof(map));
for (i=1; i<=n; i++)
{
scanf ("%d %d %s", &stu[i].x, &stu[i].y, ch);
if (ch[0] == 'W')
stu[i].s = 0;
else if (ch[0] == 'N')
stu[i].s = 1;
else if (ch[0] == 'E')
stu[i].s = 2;
else
stu[i].s = 3;
map[stu[i].x][stu[i].y] = i;
}

while (m --)
{
scanf ("%d%s%d", &x, ch, &y);
if (flag)
continue;
if (ch[0] == 'L')//对机器人进行转弯
{
stu[x].s -= y % 4;//这里有可能是负数,会导致下面运行re,必须要处理
stu[x].s = (stu[x].s + 4) % 4;
}
else if (ch[0] == 'R')
}
stu[x].s += y;
stu[x].s = (stu[x].s + 4) % 4;
}
else
{
map[stu[x].x][stu[x].y] = 0;
while (y --)//一定要对沿途经过的地方进行判断,是否会有碰撞发生
{
stu[x].x = stu[x].x + dir[stu[x].s][0];
stu[x].y = stu[x].y + dir[stu[x].s][1];
if (stu[x].x<=0 || stu[x].x>A || stu[x].y<=0 || stu[x].y>B)//是否碰撞到墙
{
num1 = x;
flag = 1;
break;
}
else if (map[stu[x].x][stu[x].y])//是否碰撞到别的机器人
{
num1 = x;
num2 = map[stu[x].x][stu[x].y];
flag = 2;
break;
}
}
map[stu[x].x][stu[x].y] = x;
}
}
if (! flag)
printf ("OK\n");
else if (flag == 1)
printf ("Robot %d crashes into the wall\n", num1);
else
printf ("Robot %d crashes into robot %d\n", num1, num2);
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: