<div class="ptt" lang="en-US">Robot Motion</div><div class="plm"><table align="center"><tbody><tr><td><strong>Time Limit:</strong> 1000MS</td><td width="10px"> </td><td><strong>Memory Limit:</strong> 10000K</td></tr><tr><td><strong>Total Submissions:</strong> 12761</td><td width="10px"> </td><td><strong>Accepted:</strong> 6180</td></tr></tbody></table></div><p class="pst">Description</p><div class="ptx" lang="en-US"><center><img src="http://poj.org/images/1573_1.jpg" alt="" /></center>
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.
</div><p class="pst">Input</p><div class="ptx" lang="en-US">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.</div><p class="pst">Output</p><div class="ptx" lang="en-US">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.</div><p class="pst">Sample Input</p><pre class="sio">3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 0
Sample Output
10 step(s) to exit 3 step(s) before a loop of 8 step(s)
Source
#include<string.h>
#include<stdio.h>
#include<iostream>
using namespace std;
int m,n,s;
int step;
char map[100][100];
int map2[110][110];
int panduan(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m)
return 1;
return 0;
}
void fun(int x,int y)
{
map2[x][y] = ++step;
if(map[x][y]=='E')//nan
{
// printf("++++++");
if(panduan(x,y+1)==0)//如果下一步出去
{
//if(step>1)
printf("%d step(s) to exit\n",map2[x][y]);
//else if(step == 1)
// printf("%d step to exit\n",map2[x][y]);
return;
}
else if(panduan(x,y+1)&&map2[x][y+1]!=0)//如果下一步碰到以前走过
{
printf("%d step(s) before a loop of %d step(s)\n",
map2[x][y+1]-1,step-map2[x][y+1]+1);
return;
}
else //正常的走
{
//map2[x][y]=step++;
fun(x,y+1);
}
}
if(map[x][y]=='W')//nan
{
if(panduan(x,y-1)==0)
{
//if(map2[x][y]>1)
printf("%d step(s) to exit\n",map2[x][y]);
//else if(step == 1)
// printf("%d step to exit\n",map2[x][y]);
return;
}
else if(panduan(x,y-1)&&map2[x][y-1]!=0)
{
printf("%d step(s) before a loop of %d step(s)\n",
map2[x][y-1]-1,map2[x][y]-map2[x][y-1]+1);
return;
}
else
{
//map2[x][y]=step++;
fun(x,y-1);
}
}
if(map[x][y]=='N')//xi
{
//printf("-------\n");
if(panduan(x-1,y)==0)
{
// printf("))))00 %d %d\n",x-1,y);
//if(map2[x][y]>1)
printf("%d step(s) to exit\n",map2[x][y]);
//else if(step == 1)
// printf("%d step to exit\n",map2[x][y]);
return;
}
else if(panduan(x-1,y)&&map2[x-1][y]!=0)
{
printf("%d step(s) before a loop of %d step(s)\n",
map2[x-1][y]-1,map2[x][y]-map2[x-1][y]+1);
return;
}
else
{
//map2[x][y]=step++;
fun(x-1,y);
}
}
if(map[x][y]=='S')//bei
{
if(panduan(x+1,y)==0)
{
//if(map2[x][y]>1)
printf("%d step(s) to exit\n",map2[x][y]);
//else if(map[x][y] == 1)
// printf("%d step to exit\n",map2[x][y]);
return;
}
else if(panduan(x+1,y)&&map2[x+1][y]!=0)
{
//if(map2[x+1][y]-1)
printf("%d step(s) before a loop of %d step(s)\n",
map2[x+1][y]-1,map2[x][y]-map2[x+1][y]+1);
return;
}
else
{
//map2[x][y]=step;
fun(x+1,y);
}
}
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&s),n,m,s)
{
int i,j;
memset(map,0,sizeof(map));
memset(map2,0,sizeof(map2));
for(i = 0 ;i <n; i++)
{
scanf("%s",map[i]);
}
map2[0][s-1]=1;
step = 0;
fun(0,s-1);
}
}
本文介绍了一个关于机器人在网格中寻找出路或循环路径的问题。通过编程让机器人根据指令移动,并判断其是否能离开网格或者陷入无限循环。输入包括网格布局及起始位置,输出则为机器人完成任务的具体步骤。
468

被折叠的 条评论
为什么被折叠?



