Time limit: 3.000 seconds
限时:3.000秒
Robotics, robot motion planning, and machine learning are areas that cross the boundaries of many of the subdisciplines that comprise Computer Science: artificial intelligence, algorithms and complexity, electrical and mechanical engineering to name a few. In addition, robots as "turtles" (inspired by work by Papert, Abelson, and diSessa) and as "beeper-pickers" (inspired by work by Pattis) have been studied and used by students as an introduction to programming for many years.
机器人学、机器人运动规划以及机器学习所涉及的范围横跨了计算机学中的许多子学科:包括人工智能、算法与复杂度、电子与机械工程等,仅举几例。此外,“海龟”机器人(由Papert、Abelson和diSessa创造)和“蜂鸣拾取”机器人?(由Pattis创造)已经作为研究和编程教学使用了很多年。
This problem involves determining the position of a robot exploring a pre-Columbian flat world.
这个问题是让机器人去探索一个“前哥伦布时代”(哥伦布发现新大陆之前)的扁平世界,确定在此过程中的所在位置。
Given the dimensions of a rectangular grid and a sequence of robot positions and instructions, you are to write a program that determines for each sequence of robot positions and instructions the final position of the robot.
给定一个矩形格网和一组机器人的位置信息和指令,你要写一个程序来确定机器人在执行完每组位置信息和指令时的最终位置。
A robot position consists of a grid coordinate (a pair of integers: x-coordinate followed by y-coordinate) and an orientation (N,S,E,W for north, south, east, and west). A robot instruction is a string of the letters 'L', 'R', and 'F' which represent, respectively, the instructions:
一个机器人的位置信息由一个格网坐标(一对整数:x在前y在后)和一个方向值(N、S、E、W对应北、南、东、西)构成的。一条机器人指令是一个由字母“L”、“R”和“F”组成的字符串,这些字母分别表示的指令为:
The direction North corresponds to the direction from grid point (x,y) to grid point (x,y+1).
方向“北”对应的方向为从格网坐标(x, y)到格网坐标(x, y + 1)。
Since the grid is rectangular and bounded, a robot that moves "off" an edge of the grid is lost forever. However, lost robots leave a robot "scent" that prohibits future robots from dropping off the world at the same grid point. The scent is left at the last grid position the robot occupied before disappearing over the edge. An instruction to move "off" the world from a grid point from which a robot has been previously lost is simply ignored by the current robot.
格网是矩形且有边界的,一个机器人移出格网的边缘时就永远的损失掉了。然而,损失的机器人会留下一些“信息素”,以免后来的机器人再从同一格坠落。信息素留在机器人消失在边缘外的前一格。如果有一条指令企图让机器人从一个格网坐标移出世界,然而之前已经有机器人从该坐标移出并损失,那么这条指令就应被忽略。
The first line of input is the upper-right coordinates of the rectangular world, the lower-left coordinates are assumed to be 0,0.
输入的第一行是矩形世界的右上角坐标,左下角坐标设为(0, 0)。
The remaining input consists of a sequence of robot positions and instructions (two lines per robot). A position consists of two integers specifying the initial coordinates of the robot and an orientation (N,S,E,W), all separated by white space on one line. A robot instruction is a string of the letters 'L', 'R', and 'F' on one line.
其余的输入由一组机器人位置信息和指令构成(每个机器人占两行)。位置信息是由表示机器人初始坐标的两个整数和一个方向值(N、S、E、W)组成。
Each robot is processed sequentially, i.e., finishes executing the robot instructions before the next robot begins execution.
要依次处理每个机器人,也就是说,在下一个机器人开始执行之前必须完成当前的机器人执行。
Input is terminated by end-of-file.
输入由EOF结束。
You may assume that all initial robot positions are within the bounds of the specified grid. The maximum value for any coordinate is 50. All instruction strings will be less than 100 characters in length.
你可以认为所有机器人的初始坐标都在限定的格网范围之内。最大的坐标值为50。所有指令字符串都少于100个字符长度。
For each robot position/instruction in the input, the output should indicate the final grid position and orientation of the robot. If a robot falls off the edge of the grid the word "LOST" should be printed after the position and orientation.
对应于每行机器人坐标/指令的输入,要出输机器人最后的坐标和方向。如果一个机器人从格网边缘跌落,则应在输出坐标和方向之后输出“LOST”。
5 3
1 1 E
RFRFRFRF
3 2 N
FRRFLLFFRRFLL
0 3 W
LLFFFLFLFL
1 1 E
3 3 N LOST
2 3 S
又是一道模拟类型的题,比#101更简单。唯一要说明的是:注意机器人可以移入已有留有“信息素”的网格,只是不能从该网格移出边界。算法在代码的注释中已经很清楚了,没有办法更加清楚了。
#include <algorithm> #include <iostream> #include <vector> using namespace std; struct POINT {int x; int y;}; int main(void) { //vecScent用于记录之前损失的机器人留下的信息 vector<int> vecScent; //szIns为指令集,szOri为方向转换表 char szIns[100], szOri[4] = {'N', 'E', 'S', 'W'}; //ptSize记录地图尺寸,ptPos记录当前位置,ptOri为对应的方向偏移量 POINT ptSize, ptPos, ptOri[4] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; //输入地图尺寸 cin >> ptSize.x >> ptSize.y; //循环处理输入的每一个机器人 for (char cOri; cin >> ptPos.x >> ptPos.y >> cOri >> szIns; ) { //确定初始方向对应的编号 int nOri = find(&szOri[0], &szOri[4], cOri) - &szOri[0], i = 0; //循环处理每一步移动 for (; szIns[i] != 0; ++i) { //向左转或向右转 if (szIns[i] != 'F') { nOri = (nOri + (szIns[i] == 'L' ? 3 : 1)) % 4; continue; } //向前移动,计算出将要移动到的坐标 POINT ptNew = {ptPos.x + ptOri[nOri].x, ptPos.y + ptOri[nOri].y}; //如果本次移动未造成损失,则保存新坐标 if (ptNew.x >= 0 && ptNew.x <= ptSize.x && ptNew.y >= 0 && ptNew.y <= ptSize.y) { ptPos = ptNew; continue; } //否则,按损失前的位置和方向进行损失编码 int nScent = ptPos.y * 51 + ptPos.x;// + nOri * 51 * 51; //在历史记录中查找是否有机器人在此损失 vector<int>::iterator iEnd = vecScent.end(); //如果在此尚未有过损失,则损失当前机器人 if (find(vecScent.begin(), iEnd, nScent) == iEnd) { //记录损失信息 vecScent.push_back(nScent); break; } } //按要求输出结果 cout << ptPos.x << ' ' << ptPos.y << ' ' << szOri[nOri]; cout << ((szIns[i] == 0) ? "" : " LOST") << endl; } return 0; }