宇航员 【POJ - 1835】【模拟】

题目链接


问题描述: 
  宇航员在太空中迷失了方向,在他的起始位置现在建立一个虚拟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米。 
 


  我们知道,每次改变的朝向,是从当前的方向作为参考方向的上下左右(因为宇航员在太空中是没有重心的),然后就是将每个方向X、Y、Z设置8个方向朝向,正方向上对应四个,负方向上对应四个,然后,我们直接维护一个24*6的变向方案即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int dir[24][3] =
{
    1, 0, 0,
    1, 0, 0,
    1, 0, 0,
    1, 0, 0,
    0, 1, 0,
    0, 1, 0,
    0, 1, 0,
    0, 1, 0,
    0, 0, 1,
    0, 0, 1,
    0, 0, 1,
    0, 0, 1,
    -1, 0, 0,
    -1, 0, 0,
    -1, 0, 0,
    -1, 0, 0,
    0, -1, 0,
    0, -1, 0,
    0, -1, 0,
    0, -1, 0,
    0, 0, -1,
    0, 0, -1,
    0, 0, -1,
    0, 0, -1
};
const int Fro_To[24][6] =
{
    0, 12, 16, 4, 8, 20,
    1, 15, 21, 9, 17, 5,
    2, 14, 6, 18, 22, 10,
    3, 13, 11, 23, 7, 19,
    4, 16, 0, 12, 9, 23,
    5, 19, 20, 10, 1, 13,
    6, 18, 14, 2, 21, 11,
    7, 17, 8, 22, 15, 3,
    8, 22, 17, 7, 14, 0,
    9, 21, 1, 15, 18, 4,
    10, 20, 5, 19, 2, 12,
    11, 23, 13, 3, 6, 16,
    12, 0, 4, 16, 10, 22,
    13, 3, 23, 11, 5, 17,
    14, 2, 18, 6, 20, 8,
    15, 1, 9, 21, 19, 7,
    16, 4, 12, 0, 11, 21,
    17, 7, 22, 8, 13, 1,
    18, 6, 2, 14, 23, 9,
    19, 5, 10, 20, 3, 15,
    20, 10, 19, 5, 0, 14,
    21, 9, 15, 1, 16, 6,
    22, 8, 7, 17, 12, 2,
    23, 11, 3, 13, 4, 18
};
int N;
struct node
{
    int fx, fy, fz, D;
    node(int a=0, int b=0, int c=0, int d=0):fx(a), fy(b), fz(c), D(d) {}
} now, nex;
int main()
{
    int T; scanf("%d", &T);
    while(T--)
    {
        now = node(); nex = node();
        scanf("%d", &N);
        char s[10]; int x;
        for(int i=1; i<=N; i++)
        {
            scanf("%s%d", s, &x);
            if(s[0] == 'f')
            {
                nex.D = Fro_To[now.D][0];
                nex.fx = now.fx + dir[nex.D][0] * x;
                nex.fy = now.fy + dir[nex.D][1] * x;
                nex.fz = now.fz + dir[nex.D][2] * x;
            }
            else if(s[0] == 'b')
            {
                nex.D = Fro_To[now.D][1];
                nex.fx = now.fx + dir[nex.D][0] * x;
                nex.fy = now.fy + dir[nex.D][1] * x;
                nex.fz = now.fz + dir[nex.D][2] * x;
            }
            else if(s[0] == 'l')
            {
                nex.D = Fro_To[now.D][2];
                nex.fx = now.fx + dir[nex.D][0] * x;
                nex.fy = now.fy + dir[nex.D][1] * x;
                nex.fz = now.fz + dir[nex.D][2] * x;
            }
            else if(s[0] == 'r')
            {
                nex.D = Fro_To[now.D][3];
                nex.fx = now.fx + dir[nex.D][0] * x;
                nex.fy = now.fy + dir[nex.D][1] * x;
                nex.fz = now.fz + dir[nex.D][2] * x;
            }
            else if(s[0] == 'u')
            {
                nex.D = Fro_To[now.D][4];
                nex.fx = now.fx + dir[nex.D][0] * x;
                nex.fy = now.fy + dir[nex.D][1] * x;
                nex.fz = now.fz + dir[nex.D][2] * x;
            }
            else
            {
                nex.D = Fro_To[now.D][5];
                nex.fx = now.fx + dir[nex.D][0] * x;
                nex.fy = now.fy + dir[nex.D][1] * x;
                nex.fz = now.fz + dir[nex.D][2] * x;
            }
            now = nex;
        }
        printf("%d %d %d %d\n", now.fx, now.fy, now.fz, now.D / 4);
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值