标签:简单模拟 c++ 机器人大法好 每日一水 不知道说什么
Robot Motion
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7825 Accepted Submission(s): 3582
Problem Description
A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are
N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)
For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.
Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.
You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.
Input
There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in
which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions
contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.
Output
For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions
on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.
Sample Input
3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0
Sample Output
10 step(s) to exit
3 step(s) before a loop of 8 step(s)
Source
Recommend
We have carefully selected several similar problems for you:
1016 1010 2553 1258 1045
每日一水,觉得脑子不好使的时候就来做做题.......
题目的大意就是:
给你一个机器人,和一个地图,这个地图是由字母,‘N’,‘S’,E’,‘W’,组成,机器人遇到N的时候往北走,遇到S的时候往南边走,遇到E的时候往东边走,遇到W的时候往西边走,问你机器人走出边界的时候走了多少步,或者在机器人走不出边界的情况如图2,(即陷入死循环),求出机器人在死循环前走的步伐和机器人和陷入多少步的死循环.
题目输入:地图行数,地图列数,机器人进入的起点(start),(机器人每次都从第一行进入,输入的起点为第0行第start-1列)
然后有人可能会想到DFS,记忆化数组。这些博主都虽然接触过但是也掌握不好,其实这题用简单模拟就行了......然后就不做过多解释了,
题目比较简单,代码如下有注释.....
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char map[20][20]; //存取地图
int step[20][20]; //记录当前走的总步数
int n,m,start;
bool is_inmap(int x,int y) //判断是否走出边界了
{
if(!(x>=0&&y>=0&&x<n&&y<m))
{
return false;
}
else
{
return true;
}
}
int main()
{
while(scanf("%d%d",&n,&m)&&n)
{
scanf("%d",&start);
memset(map,0,sizeof(map));
memset(step,-1,sizeof(step)); //这里把数组里面每一个值设置为-1
for(int i=0;i<n;i++)
scanf("%s",map[i]);
int AnsSteps = 0; //用来存取走的总步数
step[0][start-1]=0;
int x=0,y=start-1; //记录起点的坐标
while(1)
{
if(map[x][y]=='N')
{
x--;
}
else if(map[x][y]=='S')
{
x++;
}
else if(map[x][y]=='E')
{
y++;
}
else if(map[x][y]=='W')
{
y--;
}
AnsSteps++;
if(!is_inmap(x,y)) //是否还在地图当中
{
printf("%d step(s) to exit\n",AnsSteps);
break;
}
if(step[x][y]==-1) //step[x][y]等于-1说明当前点没有走过则换成AnsSteps
step[x][y]=AnsSteps;
else //否则已经访问过,有死循环,输出
{
printf("%d step(s) before a loop of %d step(s)\n",step[x][y],AnsSteps-step[x][y]);
break;
}
}
}
}
HDU-1035-Robot Motion(开两个数组简单模拟,话说最近一直再做模拟......C++)
标签:简单模拟 c++ 机器人大法好 每日一水 不知道说什么
原文地址:http://blog.csdn.net/qq_16542775/article/details/46273295