POJ1835 宇航员 模拟

一道模拟题:http://poj.org/problem?id=1835

back、forward、up、down都很好搞,而left和right困扰了我很久,最后我直接打表,将所有情况的left和right都搞出来

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;int main()
{
    int xoy[4] = {0, 4, 3, 1};
    int yoz[4] = {1, 5, 4, 2};  
    int xoz[4] = {0, 5, 3, 2};
    int left[6][6], right[6][6];
    memset(left, 0, sizeof(left));
    memset(right, 0, sizeof(right));
    //z-xoy
    for (int i = 0; i < 4; ++i) {
        right[5][xoy[i]] = left[2][xoy[i]] = xoy[(i + 1) % 4];
        right[2][xoy[i]] = left[5][xoy[i]] = xoy[(i + 3) % 4];
    }
    //x-yoz
    for (int i = 0; i < 4; ++i) {
        right[3][yoz[i]] = left[0][yoz[i]] = yoz[(i + 1) % 4];
        right[0][yoz[i]] = left[3][yoz[i]] = yoz[(i + 3) % 4];
    }
    //y-xoz
    for (int i = 0; i < 4; ++i) {
        right[1][xoz[i]] = left[4][xoz[i]] = xoz[(i + 1) % 4];
        right[4][xoz[i]] = left[1][xoz[i]] = xoz[(i + 3) % 4];
    }
    char s[10];
    int t, step;
    scanf("%d", &t);
    while(t--) {
        int point[6] = {0, 0, 0, 0, 0, 0};
        int dir = 0, head = 2, n;
        scanf("%d", &n);
        getchar();
        while(n--) {
            scanf("%s%d", s, &step);
            if(s[0] == 'f') {
                point[dir] += step;
            }
            else if(s[0] == 'b') {
                dir = (dir + 3) % 6;
                point[dir] += step;
            }
            else if(s[0] == 'l') {
                dir = left[head][dir];
                point[dir] += step;
            }
            else if(s[0] == 'r') {
                dir = right[head][dir];
                point[dir] += step;
            }
            else if(s[0] == 'u') {
                int t  = dir;
                dir = head;
                head = (t + 3) % 6;
                point[dir] += step;
            }
            else if(s[0] == 'd'){
                int t = dir;
                dir = (head + 3) % 6;
                head = t;
                point[dir] += step;
            }
        }
        printf("%d %d %d %d\n", point[0] - point[3], point[1] - point[4], point[2] - point[5], dir);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/xdaniel/p/12237307.html