美文网首页
poj1835 模拟(找规律)

poj1835 模拟(找规律)

作者: 暖昼氤氲 | 来源:发表于2019-11-09 15:04 被阅读0次
    /*
    Time:2019.11.9
    Author: Goven
    type:模拟(找规律) 
    err:每一次都要转换为绝对位置+绝对朝向 。对于每个朝向,预先得到每次动作将变化为什么朝向(对于朝向2,5比较特殊,
    其向上可能有(0,1,3,4)这四种)//err:朝向为0,1,3,4时up同样有4中情况
    ref: 解法:https://blog.csdn.net/qq_34731703/article/details/52396225 
    */
    
    //错误版 
    #include<iostream>
    #include<map>
    #include<string>
    using namespace std;
    
    int main()
    {
        //init
        int d[][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {-1, 0, 0}, {0, -1, 0}, {0, 0, -1}};
        int a[][6] = {{2, 5, 4, 1, 3, 0},{2, 5, 0, 3, 4, 1},{0},
        {2, 5, 1, 4, 0, 3},{2, 5, 3, 0, 1, 4},{0}};
        int b[][6] = {{3, 0, 4, 1, 5, 2},{4, 1, 0, 3, 5, 2},{0},{0, 3, 1, 4, 5, 2},{1, 4, 3, 0, 5, 2},{0}};//朝向2的时候
        int c[][6] = {{0, 3, 4, 1, 2, 5},{1, 4, 0, 3, 2, 5},{0},{3, 0, 1, 4, 2, 5},{4, 1, 3, 0, 2, 5},{0}};//朝向5的时候 
        map<string, int> mp;
        mp["up"] = 0; mp["down"] = 1; mp["left"] = 2;
        mp["right"] = 3; mp["back"] = 4; mp["forward"] = 5;
        
        int n, m;
        cin >> n;
        while (n--) {
            cin >> m;
            int status, pos, x[3], dis;
            string str;
            pos = x[0] = x[1] = x[2] = 0;
            while (m--) {
                cin >> str >> dis;
                if (pos != 2 && pos != 5) {
                    status = pos;
                    pos = a[pos][mp[str]];
                }
                else if(pos == 2){
                    pos = b[status][mp[str]];
                }
                else {
                    pos = c[status][mp[str]];
                }
                for (int i = 0; i < 3; i++) {
                    x[i] += d[pos][i] * dis;
                }
                
            }
            
            printf("%d %d %d %d\n", x[0], x[1], x[2], pos);
        }
        return 0;
    }
    
    //正确版
    #include<iostream>
    #include<map>
    #include<string>
    using namespace std;
    
    int main()
    {
        //init
        int d[][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {-1, 0, 0}, {0, -1, 0}, {0, 0, -1}};
        
        int n, m;
        cin >> n;
        while (n--) {
            cin >> m;
            int x[3] = {0};
            int t, dis, forward = 0, up = 2, left = 4;
            string str;
            while (m--) {
                cin >> str >> dis;
                if (str[0] == 'u') {
                    t = forward;
                    forward = up;
                    up = (t + 3) % 6;
                }
                else if (str[0] == 'd') {
                    t = forward;
                    forward = (up + 3) % 6;
                    up = t;
                }
                else if (str[0] == 'b') {
                    forward = (forward + 3) % 6;
                    left = (left + 3) % 6;
                }
                else if (str[0] == 'l') {
                    t = forward;
                    forward = left;
                    left = (t + 3) % 6;
                }
                else if (str[0] == 'r') {
                    t = forward;
                    forward = (left + 3) % 6;
                    left = t;
                }
                
                for (int i = 0; i < 3; i++)
                    x[i] += d[forward][i] * dis;
            }
            
            printf("%d %d %d %d\n", x[0], x[1], x[2], forward);
        }
        return 0;
    } 
    
    

    相关文章

      网友评论

          本文标题:poj1835 模拟(找规律)

          本文链接:https://www.haomeiwen.com/subject/wkknbctx.html