zoukankan      html  css  js  c++  java
  • 【POJ】1835:宇航员【模拟】【三维行走】

    宇航员
    Time Limit: 2000MS   Memory Limit: 30000K
    Total Submissions: 7228   Accepted: 3050

    Description

    问题描述: 
      宇航员在太空中迷失了方向,在他的起始位置现在建立一个虚拟xyz坐标系,称为绝对坐标系,宇航员正面的方向为x轴正方向,头顶方向为z轴正方向,则宇航员的初始状态如下图所示: 

    现对六个方向分别标号,x,y,z正方向分别为0,1,2,负方向分别为3,4,5;称它们为绝对方向。宇航员在宇宙中只沿着与绝对坐标系xyz轴平行的方向行走,但是他不知道自己当前绝对坐标和自己面向的绝对方向。 

    任务描述: 
      请根据宇航员对自己在相对方向上移动的描述确定宇航员最终的绝对坐标和面向的绝对方向。对在相对方向上移动的描述及意义如下: 
    forward x  向前走x米。 
    back x 先转向后,再走x米。 
    left x 先转向左,再走x米。 
    right x 先转向右,再走x米。 
    up x 先面向上,再走x米。 
    down x 先面向下,再走x米。 
    其中向上和向下如下图所示: 

    Input

    第一行一个正整数m,表示测试数据的组数。每组测试数据第一行是一个正整数n(1<=n<=10000)表示宇航员行走的次数,下面n行每行输入一次相对行走,格式如上所述,其中( 1 <= x <= 10000 为正整数)。

    Output

    对于每组输入数据输出一行,x y z p, 中间用空格隔开,x y z是宇航员的位置的绝对坐标,p是宇航员面向的绝对方向编号(0<=p <=5)。

    Sample Input

    1
    6
    left 10
    right 11
    up 12
    down 13
    forward 14
    back 15
    

    Sample Output

    23 -10 12 3
     

    Solution

    一开始思考写法的时候心态崩了QAQ 数一数有24种情况??!!天哪快问问做了的人QAQ

    然后他们说其实很好写,网上题解有30行结束的???陷入震惊与沉思....

    一开始写定了两个状态,向前和向左,然后发现还不够,在上下走的时候如果不定上下的方向很难转移QAQ

    所以再加个向上就行了QAQ(头顶指的方向)

    代码不长 然而还是写的心态崩掉QAQAQAQ

    Code

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define LL long long
    using namespace std;
    
    LL x, y, z;
    int to, lft, up;
    
    void change(int w) {
        if(to == 0)    x += w;
        if(to == 1)    y += w;
        if(to == 2)    z += w;
        if(to == 3)    x -= w;
        if(to == 4)    y -= w;
        if(to == 5)    z -= w;
    }
    
    int main() {
        int T;
        scanf("%d", &T);
        while(T --) {
            int n;
            scanf("%d", &n);
            x = 0, y = 0, z = 0, to = 0, lft = 4, up = 2;
            for(int i = 1; i <= n; i ++) {
                char s[10];    int w;
                scanf("%s", s); scanf("%d", &w);
                if(s[0] == 'f')    {
                    change(w);
                } else if(s[0] == 'b') {
                    to = (to + 3) % 6, lft = (lft + 3) % 6;
                    change(w);
                } else if(s[0] == 'l') {
                    int t = lft; lft = (to + 3) % 6; to = t;
                    change(w);
                } else if(s[0] == 'r') {
                    int t = lft; lft = to; to = (t + 3) % 6;
                    change(w);
                } else if(s[0] == 'u') {
                    int t = to; to = up; up = (t + 3) % 6;
                    change(w);
                } else if(s[0] == 'd') {
                    int t = to; to = (up + 3) % 6; up = t;
                    change(w);
                }
            }
            printf("%lld %lld %lld %d
    ", x, y, z, to);
        }
        return 0;
    }
  • 相关阅读:
    [LeetCode#91]Decode Ways
    [LeetCode#130]Surrounded Regions
    [LeetCode#84]Largest Rectangle in Histogram
    [LeetCode#179]Largest Number
    [LeetCode#187]Repeated DNA Sequences
    [LeetCode#200]Number of Islands
    [LeetCode#268]Missing Number
    [LeetCode#44]Wildcard Matching
    [LeetCode#128]Longest Consecutive Sequence
    1如何给devexpress的gridview控件绘制全选按钮
  • 原文地址:https://www.cnblogs.com/wans-caesar-02111007/p/9845725.html
Copyright © 2011-2022 走看看