acm 10116 robot motion 機器人運動

這題其實也只要多建立一個相同大小的陣列紀錄步數以及重複步數值就可以了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#define col 12
int step, r, c;
char motion[row][col];
int record[row][col];
void (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 exitn", 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 座標即可。