
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.
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h> using namespace std; int key[][]; int main()
{
int a,b,p;
while(scanf("%d%d",&a,&b)&&a&&b)
{
scanf("%d",&p);
char arr[a][b];
int i,j;
for(i=;i<a;i++)
scanf("%s",&arr[i]); int s=,flag=;
i=;
j=p-;
while(true)
{
if(arr[i][j]=='N')
{
key[i][j]=s;
arr[i][j]='A';
i--;
} else if(arr[i][j]=='S')
{
key[i][j]=s;
arr[i][j]='A';
i++;
}
else if(arr[i][j]=='W')
{
key[i][j]=s;
arr[i][j]='A';
j--;
}
else if(arr[i][j]=='E')
{
key[i][j]=s;
arr[i][j]='A';
j++;
}
s++; if(arr[i][j]=='A')
{
flag=;
break;
}
if(i<||i==a||j<||j==b)
break;
}
if(flag)
printf("%d step(s) before a loop of %d step(s)\n",key[i][j],s-key[i][j]);
else
printf("%d step(s) to exit\n",s);
}
return ;
}