UVa 12811 - The Turtle's Journey

contents

  1. 1. Problem
  2. 2. Sample Input
  3. 3. Sample Output
  4. 4. Solution

Problem

  • left X
  • right X
  • forward X
  • repeat N [ INST ]

上述總共有四種指令架構,分別輸出前三個指令的 X 總和值。其中第四個為迴圈架構。

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
begin
forward 10
left 90
forward 10
left 90
forward 10
left 90
forward 10
left 90
end
begin
forward 10
left 90
forward 10
left 90
forward 10
left 90
forward 10
left 90
end

Sample Output

1
2
360 0 40
360 0 40

Solution

由於輸入沒有迴圈,差點忘了有 repeat 指令。遞迴讀進輸入即可。

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <string.h>
#include <assert.h>
#include <map>
using namespace std;
const long long mod = 1000003;
char s[128];
void dfs(long long cmd[]) {
scanf("%s", s);
if (s[0] == 'e' || s[0] == ']') return;
long long x, ncmd[3] = {};
scanf("%lld", &x);
if (s[0] == 'l')
cmd[0] += x, dfs(ncmd);
else if (s[0] == 'r' && s[1] == 'i')
cmd[1] += x, dfs(ncmd);
else if (s[0] == 'f')
cmd[2] += x, dfs(ncmd);
else if (s[0] == 'r' && s[1] == 'e'){
scanf("%*s");
dfs(cmd);
for (int i = 0; i < 3; i++)
cmd[i] = (cmd[i] * x) %mod;
dfs(ncmd);
}
for (int i = 0; i < 3; i++)
cmd[i] = (cmd[i] + ncmd[i]) %mod;
}
int main() {
int testcase;
scanf("%d", &testcase);
while (testcase--) {
scanf("%*s");
long long cmd[3] = {};
dfs(cmd);
printf("%lld %lld %lld\n", cmd[0], cmd[1], cmd[2]);
}
return 0;
}
/*
2
begin
forward 10
left 90
forward 10
left 90
forward 10
left 90
forward 10
left 90
end
begin
forward 10
left 90
forward 10
left 90
forward 10
left 90
forward 10
left 90
end */