2014-10-26 解題區/解題區 - UVa UVa 12811 - The Turtle's Journey contents 1. Problem2. Sample Input3. Sample Output4. Solution Problem left X right X forward X repeat N [ INST ] 上述總共有四種指令架構,分別輸出前三個指令的 X 總和值。其中第四個為迴圈架構。 Sample Input1234567891011121314151617181920212beginforward 10left 90forward 10left 90forward 10left 90forward 10left 90endbeginforward 10left 90forward 10left 90forward 10left 90forward 10left 90end Sample Output12360 0 40360 0 40 Solution由於輸入沒有迴圈,差點忘了有 repeat 指令。遞迴讀進輸入即可。 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869#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 */ Newer UVa 12812 - The Largest Diamond-Shaped Kite Older UVa 12810 - Sumthing