-
#include <stdio.h>
-
#include <string.h>
-
-
#define MAX 101
-
#define CRASH_WALL -1
-
#define OK 0
-
-
int a[101][101];
-
-
typedef struct
-
{
-
int x;
-
int y;
-
int dirc;
-
}pos;
-
-
/***********************************************
-
|func: 机器人按指令行动函数
-
|args: 第i个机器人状态,执行指令ins,走s步
-
|retn: 撞墙->CRASH_WALL,撞机器人->编号,其它->OK
-
************************************************/
-
int Act(pos *r, int i, char ins, int s)
-
{
-
int xoff, yoff, t;
-
-
if ('L' == ins)
-
{
-
s %= 4;
-
r[i].dirc = (r[i].dirc + s + 4) % 4;
-
}
-
-
else if ('R' == ins)
-
{
-
s %= 4;
-
r[i].dirc = (r[i].dirc - s + 4) % 4;
-
}
-
-
else
-
{
-
switch(r[i].dirc)
-
{
-
case 0 :
-
{
-
xoff = 0;
-
yoff = 1;
-
break;
-
}
-
case 1 :
-
{
-
xoff = 1;
-
yoff = 0;
-
break;
-
}
-
case 2 :
-
{
-
xoff = 0;
-
yoff = -1;
-
break;
-
}
-
case 3 :
-
{
-
xoff = -1;
-
yoff = 0;
-
}
-
}
-
while(s--) //模拟前进
-
{
-
t = a[r[i].x + xoff][r[i].y + yoff];
-
if (-1 == t)
-
{
-
return CRASH_WALL;
-
}
-
else if (t != 0)
-
{
-
return t; //撞到其它robot
-
}
-
a[r[i].x + xoff][r[i].y + yoff] = i; //更新当前robot序号到移动后的位置
-
a[r[i].x][r[i].y] = 0; //清除上一步的足迹
-
r[i].x += xoff; //更新新的坐标
-
r[i].y += yoff;
-
}
-
}
-
-
return OK;
-
}
-
-
-
int main()
-
{
-
pos robot[101]
-
char instruct, direction;
-
int k, ew, ns, num, m, i, j, step, failed;
-
-
scanf("%d", &k);
-
while(k--)
-
{
-
memset(a, -1, sizeof(a)); //初始化-1为Wall
-
failed = 0; //crash标记
-
scanf("%d%d%d%d", &ew, &ns, &num, &m);
-
for(i=1; i<=ns; i++)
-
{
-
for (j=1; j<=ew; j++)
-
{
-
a[i][j] = 0; //无阻挡物,标记为0
-
}
-
}
-
-
i = 1;
-
while(i <= num)
-
{
-
scanf("%d%d %c", &robot[i].y, &robot[i].x, &direction); //数组的x为ns方向,数组y为ew方向
-
a[robot[i].x][robot[i].y] = i; //标记robot位置
-
switch(direction)
-
{ //数组水平方向和题目一致, 垂直方向和题目相反.
-
case 'E' : robot[i].dirc = 0; break;
-
case 'N' : robot[i].dirc = 1; break;
-
case 'W' : robot[i].dirc = 2; break;
-
case 'S' : robot[i].dirc = 3; break;
-
}
-
i++;
-
}
-
-
while(m)
-
{
-
while((m != 0) && failed) //这个很重要,在crash后要读完后面的指令才break
-
{
-
scanf("%d %c%d", &i, &instruct, &step);
-
m-- ;
-
}
-
if (0 == m)
-
{
-
break;
-
}
-
scanf("%d %c%d", &i, &instruct, &step);
-
j = Act(robot, i, instruct, step);
-
if (CRASH_WALL == j)
-
{
-
printf("Robot %d crashes into the walln", i);
-
failed = 1;
-
}
-
else if (j != OK)
-
{
-
printf("Robot %d crashes into robot %dn", i, j);
-
failed = 1;
-
}
-
m--;
-
}
-
-
if (!failed)
-
{
-
printf("OKn");
-
}
-
}
-
-
return 0;
-
}
2011-04-07 13:19 发表于百度空间,今搬至CU。
阅读(1772) | 评论(0) | 转发(0) |