您的位置:首页 > 其它

poj_2632_Crashing Robots(模拟)

2013-07-14 22:01 501 查看
Crashing Robots
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status

Description

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.

Input

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.

Output

Output one line for each test case:

Robot i crashes into the wall, if robot i crashes into a wall. (A robot crashes into a wall if Xi = 0, Xi = A + 1, Yi = 0 or Yi = B + 1.)

Robot i crashes into robot j, if robots i and j crash, and i is the moving robot.

OK, if no crashing occurs.

Only the first crash is to be reported.

Sample Input

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


Sample Output

Robot 1 crashes into the wall
Robot 1 crashes into robot 2
OK
Robot 1 crashes into robot 2


题型:模拟

题意:对若干机器人进行操控,先输入矩形地图的大小,再输入机器人的数量和指令的数量。机器人根据指令进行移 动。如果在指令执行的中途中,某一机器人撞上了墙,则输出Robot i crashes into the wall;如果某一机器人撞上了其他机器人,则输出Robot i crash into robot j;如果当所有的指令均能平安完成,则输出“OK”。

分析:直接模拟,标记下当前要移动的机器人的状态,若是占了其他机器人的位置,那么就代表与其他机器人撞上了,若是坐标超出了地图的范围,那么就代表装上墙了。

注意点:当遇到撞上其他机器人或是撞上墙时,不能直接break,否则会奖励你个WA,要continue将接下来的指令输入完毕。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstring>
using namespace std;
struct robot
{
int x,y;
char d;
};
typedef struct robot ro;
int mop[200][200];
ro rob[200];
int flag,kkk,cnt;

int main()
{
int t;
int a,b;
int n,m;
int xx,yy;
char dd,c;
int r,f;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&a,&b);//a is lenth,b is width
scanf("%d%d",&n,&m);//first follow n lines,than m lines
for(int i=1; i<=b; i++)
{
for(int j=1; j<=a; j++)
{
mop[i][j]=0;
}
}
for(int i=1; i<=n; i++)
{
cin>>xx>>yy>>dd;//坐标和方向
rob[i].x=xx;
rob[i].y=b-yy+1;//y的位置要翻一下,因为是从下往上1234...
rob[i].d=dd;
mop[b-yy+1][xx]=i;//标记下这个位置是哪个robot
}

/*for(int i=1;i<=b;i++){
for(int j=1;j<=a;j++){
cout<<mop[i][j]<<" ";
}
cout<<endl;
}*/

flag=1;
kkk=0;
while(m--)
{
cin>>r>>c>>f;//当前的robot,指令,步骤重复数目
if(flag==0) continue;//若有了相撞或撞墙的,继续输入但是不执行
else
{
cnt=r;//记一下当前是哪个robot
for(int i=0; i<f; i++)
{
if(flag==0) break;
if(c=='F') //forward
{
if(rob[r].d=='N')
{
mop[rob[r].y][rob[r].x]=0;//标记还原
rob[r].y--;//移动
if(rob[r].y<=0)
flag=0;
else
{
if(mop[rob[r].y][rob[r].x]!=0)
{
kkk=mop[rob[r].y][rob[r].x];//装的是kkk这个robot
flag=0;
}
else{
mop[rob[r].y][rob[r].x]=r;
}
}
}
else if(rob[r].d=='S')
{
mop[rob[r].y][rob[r].x]=0;
rob[r].y++;
if(rob[r].y>b)
flag=0;
else
{
if(mop[rob[r].y][rob[r].x]!=0)
{
kkk=mop[rob[r].y][rob[r].x];
flag=0;
}
else{
mop[rob[r].y][rob[r].x]=r;
}
}
}
else if(rob[r].d=='W')
{
mop[rob[r].y][rob[r].x]=0;
rob[r].x--;
if(rob[r].x<=0)
flag=0;
else
{
if(mop[rob[r].y][rob[r].x]!=0)
{
kkk=mop[rob[r].y][rob[r].x];
flag=0;
}
else{
mop[rob[r].y][rob[r].x]=r;
}
}
}
else
{
mop[rob[r].y][rob[r].x]=0;
rob[r].x++;
if(rob[r].x>a)
flag=0;
else
{
if(mop[rob[r].y][rob[r].x]!=0)
{
kkk=mop[rob[r].y][rob[r].x];
flag=0;
}
else{
mop[rob[r].y][rob[r].x]=r;
}
//cout<<"****"<<endl;
//cout<<rob[r].x<<" "<<rob[r].y<<endl;
//cout<<"KKK "<<kkk<<endl;
//cout<<"****"<<endl;
}
//cout<<"flag: "<<flag<<endl;
}
}
else if(c=='L') //turn left
{
if(rob[r].d=='N')
rob[r].d='W';
else if(rob[r].d=='W')
rob[r].d='S';
else if(rob[r].d=='S')
rob[r].d='E';
else
rob[r].d='N';
}
else //turn right
{
if(rob[r].d=='N')
rob[r].d='E';
else if(rob[r].d=='W')
rob[r].d='N';
else if(rob[r].d=='S')
rob[r].d='W';
else
rob[r].d='S';
}
}
}
}
if(flag==0)
{
if(kkk!=0)
printf("Robot %d crashes into robot %d\n",cnt,kkk);
else
{
printf("Robot %d crashes into the wall\n",cnt);
}
}
else
{
printf("OK\n");
}
}
return 0;
}
/*
4
5 4
2 1
1 1 E
2 1 E
1 F 10
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: