UVA 10116 Solution- Robot Motion
UVA 10116 Solution- Robot Motion
uva id: shoaib05
Accepted Time: 0.000
#include<stdio.h>
char table[105][105];
int visit[105][105];
int main()
{
int row,col,i,j,x,y,total_step,flag;
char ch;
while(scanf("%d %d %d",&row,&col,&y))
{
if(row==0 && col==0 && y==0)
break;
y--;
x=0;
total_step=0;
row--;
col--;
for(i=0;i<=row;i++)
for(j=0;j<=col;j++)
visit[i][j]=0;
for(i=0;i<=row;i++)
scanf("%s",&table[i]);
flag=0;
while(1)
{
if(visit[x][y]!=0)
{
flag=1;
break;
}
visit[x][y]=++total_step;
ch=table[x][y];
switch(ch)
{
case 'E':
y++;
break;
case 'W':
y--;
break;
case 'N':
x--;
break;
case 'S':
x++;
break;
}
if(x<0 || x>row ||y<0 ||y>col)
break;
}
if(flag)
{
visit[x][y]--;
printf("%d step(s) before a loop of %d step(s)\n",visit[x][y],total_step-visit[x][y]);
}
else
printf("%d step(s) to exit\n",total_step);
}
return 0;
}
Comments
Post a Comment