#define row 12
#define col 12
int step, r, c;
char motion[row][col];
int record[row][col];
void move(int x, int y)
{
int i, j;
record[x][y] = step ++;
switch (motion[x][y])
{
case 'N':
x --;
break;
case 'E':
y ++;
break;
case 'W':
y --;
break;
case 'S':
x ++;
break;
}
if (x < 0 || x >= r || y < 0 || y >= c)
{
printf("%d step(s) to exit\n", step - 1);
return;
}
if (record[x][y] != -1 )
{
int k = step - record[x][y];
printf("%d step(s) before a loop of %d step(s)\n", step - k - 1, k);
return;
}
move(x, y);
}最後在主程式內傳入開始的 x, y 座標即可。
By David.K
p10116題目連結
回ACM題庫目錄
回首頁
沒有留言:
張貼留言