嘿嘿,一次AC……哦耶~~~
总得来说不算是难题,不过在处理坐标的时候出了问题,呃,这个代码中会有略微提到,因为比较简单,就不多说了,相信通过自己测试数据,也能理解的。
/* THE PROGRAM IS MADE BY PYY */
/*----------------------------------------------------------------------------//
Copyright (c) 2011 panyanyany All rights reserved.
URL : http://acm.hdu.edu.cn/showproblem.php?pid=1035
Name : 1035 Robot Motion
Date : Wednesday, August 10, 2011
Time Stage : 2 hours
Result:
4381936 2011-08-10 21:22:25 Accepted 1035 0MS 212K 1666 B C++ pyy
Test Data:
Review:
//----------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#define infinity 0x7f7f7f7f
#define minus_inf 0x8f8f8f8f
#define MAXSIZE 11
char c ;
int row, col, start, step, loop ;
char grip[MAXSIZE][MAXSIZE], used[MAXSIZE][MAXSIZE] ;
int route[MAXSIZE][MAXSIZE] ;
void dfs (int x, int y)
{
++step ;
if (used[x][y])
{
loop = step - route[x][y] ;
step = route[x][y] - 1 ;
}
else if (1 <= x && x <= col && 1 <= y && y <= row)
{
used[x][y] = 1 ;
route[x][y] = step ;
if ((c = grip[x][y]) == 'E')
dfs (x+1, y) ;
else if (c == 'W')
dfs (x-1, y) ;
else if (c == 'S')
dfs (x, y+1) ;
else if (c == 'N')
dfs (x, y-1) ;
}
else
{
--step ;
}
}
int main ()
{
int i, j ;
while (scanf ("%d%d%d", &row, &col, &start), row | col | start)
{
getchar () ;
for (i = 1 ; i <= row ; ++i)
{
for (j = 1 ; j <= col ; ++j)
scanf ("%c", &grip[j][i]) ; // 此处注意j 和i 的顺序
scanf ("%c", &c) ;
}
memset (used, 0, sizeof (used)) ;
step = loop = 0 ;
dfs (start, 1) ;
printf ("%d step(s) ", step) ;
if (loop)
printf ("before a loop of %d step(s)\n", loop) ;
else
printf ("to exit\n") ;
}
return 0 ;
}