pku 1573 Robot Motion(水题)
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAXN 15
#define MAXM 15
//const int dir[4][2] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} }; //east, south, west, north
char grid[MAXN][MAXM];
int step[MAXN][MAXM];
bool canExit(const int N, const int M, int i, int j)
{
if(i < 0 || j < 0 || i >= N || j >= M)
return true;
else
return false;
}
void solve(const int N, const int M, const int S)
{
memset(step, 0, sizeof(step));
int i, j;
for(i = 0; i < N; i++)
{
scanf("%s", grid[i]);
}
i = 0;
j = S;
int st = 0;
step[i][j] = ++st;
while(1)
{
switch(grid[i][j])
{
case 'E':
j++;
break;
case 'S':
i++;
break;
case 'W':
j--;
break;
case 'N':
i--;
break;
default:
break;
}
if(canExit(N, M, i, j))
{//10 step(s) to exit
// if(st > 1) printf("%d steps to exit\n", st); //WA,那句话干啥的,靠
// else printf("%d step to exit\n", st);
printf("%d step(s) to exit\n", st);
return;
}
else if(step[i][j] != 0)
{//3 step(s) before a loop of 8 step(s)
int x = step[i][j] - 1;
int y = st - x;
// if(x > 1) printf("%d steps before a loop of %d steps\n", x, y);
// else printf("%d step before a loop of %d steps\n", x, y);
printf("%d step(s) before a loop of %d step(s)\n", x, y);
return;
}
else step[i][j] = ++st;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("indata.txt", "r", stdin);
#endif
int n, m, s;
while(scanf("%d %d %d", &n, &m, &s), n + m + s)
{
solve(n, m, s - 1);
}
return 0;
}
浙公网安备 33010602011771号