水题一道。
/*
* HDU-1035 robot motion
* mike-w
* 2012-9-26
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 16
int row, col, scol;
char map[MAXSIZE][MAXSIZE];
int tag[MAXSIZE][MAXSIZE];
int search(void)
{
memset(tag, 0, sizeof(tag));
int x=0, y=scol-1;
int cnt=0;
while(!tag[x][y] && x<row && x>=0 && y>=0 && y<col)
{
tag[x][y]=++cnt;
switch(map[x][y])
{
case 'N':
x--;
break;
case 'S':
x++;
break;
case 'W':
y--;
break;
case 'E':
y++;
break;
}
}
if(x==-1 || x==row || y==-1 || y==col)
printf("%d step(s) to exit\n", cnt);
else
printf("%d step(s) before a loop of %d step(s)\n",
tag[x][y]-1, cnt-tag[x][y]+1);
return 0;
}
int main(void)
{
int i;
while(scanf("%d%d", &row, &col), row && col)
{
scanf("%d", &scol);
memset(map, 0, sizeof(map));
for(i=0; i<row; i++)
scanf("%s", map[i]);
search();
}
return 0;
}