模拟题目,地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=1573
#include <stdio.h>
struct
{
char done;//动作
int depth;//深度
}map[11][11];
int hash[26][2];
int ans;
int last;
int dfs ( int s, int r, int c )
{
int now[2] = {0, s};
ans = 0;
while ( now[0]>=0 && now[0]<r && now[1]>=0 && now[1]<c )
{
if ( map[ now[0] ][ now[1] ].done == 0 )
{
last = ans;
ans = map[ now[0] ][ now[1] ].depth;
return 0;
}
map[ now[0] ][ now[1] ].depth = ans++;
int temp[2];
temp[0] = hash[ map[now[0]][now[1]].done-'A' ][0];
temp[1] = hash[ map[now[0]][now[1]].done-'A' ][1];
map[ now[0] ][ now[1] ].done = 0;
now[0] += temp[0];
now[1] += temp[1];
}
return 1;
}
int main ()
{
int n, m, s;
hash[ 'E'-'A' ][0] = 0, hash[ 'E'-'A' ][1] = 1;
hash[ 'S'-'A' ][0] = 1, hash[ 'S'-'A' ][1] = 0;
hash[ 'W'-'A' ][0] = 0, hash[ 'W'-'A' ][1] = -1;
hash[ 'N'-'A' ][0] = -1, hash[ 'N'-'A' ][1] = 0;
while ( scanf ( "%d%d%d", &n, &m, &s ) != EOF && ( n || m || s ) )
{
getchar ();
for ( int i=0; i<n; i++ )
{
for ( int j=0; j<m; j++ )
{
scanf ( "%c", &map[i][j].done );
}
getchar ();
}
if ( dfs ( s-1, n, m ) )
{
printf ( "%d step(s) to exit\n", ans );
}
else
{
printf ( "%d step(s) before a loop of %d step(s)\n", ans, last-ans );
}
}
return 0;
}