问题连接
http://poj.org/problem?id=2632
Crashing Robots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4687 Accepted: 2054
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
题目大意:模拟对在一个房间里的机器人进行操纵,输出机器人第一次发生碰撞的信息,如果没有碰撞,输出OK。
分析:简单模拟题,注意处理旋转,和坐标系对应。。。。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef int B[3];
B rb[110];
typedef int MAZE[110][110];
MAZE mz;
typedef int INFO[2];
INFO c_info;
int N,M;
int W,H;
bool execute(int a,char b,int c)
{
if(b=='L')
{
c = c%4;
rb[a][2] = (rb[a][2]+c)%4;
}
else if(b=='R')
{
c = c%4;
rb[a][2] = (rb[a][2]-c);
if(rb[a][2]<0)
rb[a][2]+=4;
}
else
{
for(int i=0;i<c;i++)
{
mz[rb[a][1]][rb[a][0]] = -1;//level that place
switch(rb[a][2])
{
case 0:rb[a][1]--;break;
case 1:rb[a][0]--;break;
case 2:rb[a][1]++;break;
case 3:rb[a][0]++;break;
}
if(rb[a][0]<0||rb[a][0]>=W||rb[a][1]<0||rb[a][1]>=H)
{
c_info[0] = a+1;
c_info[1] = 0;
return true;
}
if(mz[rb[a][1]][rb[a][0]]!=-1)
{
c_info[0] = a+1;
c_info[1] = mz[rb[a][1]][rb[a][0]]+1;
return true;
}
mz[rb[a][1]][rb[a][0]]=a;
}
}
return false;
}
void see()
{
for(int i=0;i<H;i++)
{
for(int j=0;j<W;j++)
cout<<mz[i][j]<<" ";
cout<<endl;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>W>>H>>N>>M;
memset(mz,-1,sizeof(mz));//初始化棋盘
for(int i=0;i<N;i++)
{
int x,y;
char d;
cin>>x>>y>>d;
rb[i][0] = (x-1);
rb[i][1] = H-1-(y-1);
switch(d)
{
case 'N':rb[i][2]=0;break;
case 'W':rb[i][2]=1;break;
case 'S':rb[i][2]=2;break;
case 'E':rb[i][2]=3;break;
}
mz[rb[i][1]][rb[i][0]] = i;//标记棋盘
}
//see();
//读入问题
bool cash = false;
for(int i=0;i<M;i++)
{
int a,c;
char b;
cin>>a>>b>>c;
if(!cash)
cash = execute(a-1,b,c);
}
if(cash)
{
if(c_info[1]==0)
printf("Robot %d crashes into the wall\n",c_info[0]);
else
printf("Robot %d crashes into robot %d\n",c_info[0],c_info[1]);
}
else
printf("OK\n");
}
return 0;
}
分享到:
相关推荐
北大POJ2632-Crashing Robots 解题报告+AC代码
2遍dp poj_3613解题报告 poj_3613解题报告
poj题目2775文件子目录源代码,递归经典题目,
poj典型题目解题思路详解 包含源代码和解题时应注意的问题及题目陷阱设计分析
poj 1699的代码和方法说明,个人原创
C_(POJ_1854)(分治).cpp
poj上第1990题目源码,用到了2个树状数组,这题数据结构是关键,想到了题目就很简单了
D_(POJ_1723)(思维)(sort).cpp
原理为:以数链思想,移动数组中的内容 使数组在没有扩充情况下,达成移动的效果 当然,有更简单的,大牛不要笑哦
D_(POJ_1723)(思维)(第k大数).cpp
O(nlogn)凸包问题 poj2187
POJ 3131 双向BFS解立体八数码问题
poj两道题的c++实现。已经测试过可以通过oj
问题:求平面上多个矩形的总面积。 算法:线段树(经典的线段树题目)
POJ题目及算法,包括动态规划、深搜广搜等算法。含相关注释。
这是北大在线测试的第1002题,方便记忆的电话号码的解题例程,题目中有一个列表,记录着许多方便记忆的电话号码。不同的方便记忆的电话号码可能对应相同的标准号码,这个程序的任务就是找到它们
poj 3310 的代码和方法说明,个人原创
http://poj.grids.cn/problem?id=2774 POJ 2774 木棒加工 木材厂有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目是给定了。当然,我们希望得到的小段越长越好,你的任务是计算能够...
看了测试用例后,大家估计已经明白题目的意思了吧!题目很简单,但是就是很烦。 开始直接是没思路,不知道怎么模拟,但是想了带该半个小时,就搞定了。就是把每个数存到数组里面。
POJ上面题目的解题报告。涵盖挺多的。可作参考。代码都正确。ACM新手入门必下~~ 加油...