8a.c (614B)
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 30000 5 #define ISNUM(c) (c >= '0' && c <= '9') 6 7 int map[N][2]; 8 9 int index(char *c) { return c[0]-'A' + (c[1]-'A' + (c[2]-'A')*26)*26; } 10 11 int main() { 12 char *buf, dir[N], line[N]; 13 int n, i, s; 14 15 n = strlen(fgets(dir, N, stdin)) - 1; 16 17 fgets(line, N, stdin); 18 while ((buf = fgets(line, N, stdin)) != NULL) { 19 i = index(buf); 20 while (*buf != '(') buf++; 21 map[i][0] = index(buf+1); 22 while (*buf != ' ') buf++; 23 map[i][1] = index(buf+1); 24 } 25 26 for (i = index("AAA"), s = 0; i != index("ZZZ"); s++) 27 i = map[i][dir[s % n] == 'R']; 28 29 printf("%d\n", s); 30 return 0; 31 }