Mutant Flatworld Explorers

本文介绍了一个关于机器人探索模拟的问题,涉及确定机器人在一个预设网格上的最终位置。机器人可以根据指令集进行移动、转向,并且考虑到边界限制及之前的机器人留下的标记。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接模拟

Background

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.

This problem involves determining the position of a robot exploring a pre-Columbian flat world.

The Problem

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:

  • Left: the robot turns left 90 degrees and remains on the current grid point.
  • Right: the robot turns right 90 degrees and remains on the current grid point.
  • Forward: the robot moves forward one grid point in the direction of the current orientation and mantains the same orientation.

The direction North corresponds to the direction from grid point (x,y) to grid point (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 Input

The first line of input is the upper-right coordinates of the rectangular world, the lower-left coordinates are assumed to be 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.

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.

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.

The Output

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.

Sample Input

5 3
1 1 E
RFRFRFRF
3 2 N
FRRFLLFFRRFLL
0 3 W
LLFFFLFLFL

Sample Output

1 1 E
3 3 N LOST
2 3 S

#include<bits/stdc++.h>

using namespace std;

char net[1000][1000];

int main()
{
	memset(net,'.',sizeof(net));
	int x,y;
	cin >> x >> y;
	char pos;
	int x_,y_;
	string str;
	while(cin >> x_ >> y_ >> pos >> str){
		int len=str.length();
		bool check=true;
		for(int i=0; i < len; i++){
			if(str[i]=='L'){
				if(pos=='N') pos='W';
				else if(pos=='S') pos='E';
				else if(pos=='W') pos='S';
				else if(pos=='E') pos='N';
			}
			else if(str[i]=='R'){
				if(pos=='N') pos='E';
				else if(pos=='S') pos='W';
				else if(pos=='W') pos='N';
				else if(pos=='E') pos='S';
			}
			else if(str[i]=='F'){
				int x_a=x_,y_a=y_;
				if(pos=='N') y_++;
				else if(pos=='S') y_--;
				else if(pos=='W') x_--;
				else if(pos=='E') x_++;
				if(net[x_a][y_a]=='#'&&(x_<0||x_>x||y_<0||y_>y)){
					x_=x_a;
					y_=y_a;
					continue;
				}
				if(x_<0||x_>x||y_<0||y_>y){
					x_=x_a;
					y_=y_a;
					check=false;
					net[x_a][y_a]='#';
					break;
				}
			}
		}
		cout << x_ << " " << y_ << " " << pos;
		if(check==false) cout << " LOST";
		cout << endl;
	}
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值