问题连接
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"是一个经典的编程竞赛题目,源自北京大学的在线编程平台POJ(Problem Set of Peking University)。这个题目主要涉及算法设计与实现,特别是图论中的最短路径问题,以及动态规划的...
【标题】"poj_1699.rar_1699_poj_poj1699" 提供的是一个关于POJ(编程在线判题系统)第1699题的解决方案,其中包含了该问题的代码实现和解题思路。POJ是一个流行的在线编程竞赛平台,它为参赛者提供了各种算法题目,...
标签"poj poj_27 poj27 poj2775"进一步确认了这是一道关于POJ平台的编程挑战,其中"poj_27"可能是表示第27类问题或者某种分类,而"poj27"可能是对"poj2775"的简写。 压缩文件中的"www.pudn.com.txt"可能是一个链接...
标题中的"poj_2682(3).rar"是一个指向编程竞赛问题的引用,通常这类问题在网站如POJ(Programming Online Judge)上出现,供程序员解决并提交代码进行测试。这个问题的编号是2682,可能涉及到特定的数据结构或算法...
标题"Poj_1102_.rar_poj11"暗示了这是一个关于解决Poj_1102问题的资源包,通常在编程竞赛或在线判题系统如POJ(Problem Set of Judge Online)上遇到。POJ是中国的一个在线编程训练平台,提供了各种算法题目供用户...
标题中的“POJ_3131.zip_POJ 八数码_poj”指的是一个与编程竞赛网站POJ(Problem Set Algorithm)相关的项目,具体是针对3131号问题的解决方案,该问题涉及到了八数码游戏。八数码游戏,又称滑动拼图,是一个经典的...
2遍dp poj_3613解题报告 poj_3613解题报告
【标题】"poj_3310.rar_3310_poj"是一个与编程竞赛相关的压缩包,其中包含了对POJ(Problem Online Judge)3310问题的解决方案和详细解析。POJ是一个著名的在线编程竞赛平台,提供各种算法题目供参赛者练习和提交...
标题 "ACM.zip_ACM_poj_poj3187_poj3669" 提供的信息表明,这个压缩包包含的是与ACM(国际大学生程序设计竞赛)相关的编程题目解决方案,具体是POJ(Programming Online Judge)平台上的两道题目,编号分别为poj3187...
《POJ 1010 Stamps:解题思路与陷阱分析》 POJ 1010,也被称为“邮票问题”,是编程竞赛中的一道经典题目,旨在考察参赛者的动态规划和数学思维能力。这个题目在编程爱好者中具有较高的知名度,因为它涉及到有趣的...
标题中的“pku_poj_2187.rar_poj 2187_凸包问题”表明这是一个关于北京大学(Peking University, PKU)编程竞赛平台POJ上的第2187题,题目主要涉及凸包问题。描述中的“O(nlogn)凸包问题 poj2187”提示我们解决这个...
【标题】"poj_1002_487.rar_poj 1002"指的是北京大学在线编程平台上的第1002道题目,这道题目涉及到计算机科学中的算法设计与实现,特别是字符串处理和哈希映射。在这个问题中,我们需要编写一个程序,该程序能够...
标题中的“POJ3277.rar_3277 poj_poj3277_多个面积_线段树”是指一个与编程竞赛相关的压缩文件,特别提到了POJ(Problemset Online Judge)的第3277号问题。POJ是一个著名的在线编程竞赛平台,参与者需要解决各种...
C_(POJ_1854)(分治).cpp
《POJ 1990:树状数组解题策略详解》 在编程竞赛的世界里,POJ(Programming Online Judge)是一个备受瞩目的在线评测系统,它提供了丰富的编程题目供参赛者挑战。其中,编号为1990的题目是一道涉及数据结构与算法...
D_(POJ_1723)(思维)(sort).cpp
D_(POJ_1723)(思维)(第k大数).cpp
标题中的"jihe.rar_2289_POJ 3714_poj3714_poj3714 Ra_visual c" 提到了一个压缩文件,可能包含有关编程竞赛或算法解决的资源,特别是与POJ(Problem On Judge)平台上的问题3714相关的。"Ra_visual c"可能指的是...
标题“POJ_keptsl6_C++_”表明这是一个关于编程竞赛(POT, Problem Set of Judges)的资源,特别关注C++语言的解决方案。这些竞赛通常涉及到算法设计和实现,以解决各种计算机科学问题。"keptsl6"可能是用户特定的标记...
【标题】"poj_cpp.zip_MS_292AC_" 提示我们这是一份与POJ(编程在线判题系统)相关的压缩包,主要包含C++语言的解答,且可能涉及了MS(Microsoft)和292AC这两个特定的关键词。在POJ平台上,"MS_292AC_"可能是某个...