POJ

来源:互联网 发布:软件数据接口开发合同 编辑:程序博客网 时间:2024/05/19 19:42

宇航员
Time Limit: 2000MS Memory Limit: 30000KTotal Submissions: 6505 Accepted: 2793

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

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

Sample Output

23 -10 12 3

Source

qinlu@POJ


刚开始想是想要模拟一下,但是状况太多,感觉很难写就放弃了

实在想不出来就查了题解,只用记录宇航员当前的上下左右前后分别对应的是原来的哪个方向就行了


#include<iostream>#include<string.h>#include<stdio.h>using namespace std;int T,n,x,y,z,dir;int d[6][3] = {{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}};char s[10];int up,down,back,front,lt,rt;int main(){    scanf("%d",&T);    while(T--){        front = 0; back = 3; rt = 1; lt = 4; up = 2; down = 5;        x = y = z = dir = 0;        scanf("%d",&n);        while(n--){            int dist;            scanf("%s%d",s,&dist);            if(!strcmp(s, "forward")){                            }else if(!strcmp(s, "back")){                swap(back,front);                swap(lt,rt);            }else if(!strcmp(s, "left")){                int temp = front;                front = lt; lt = back; back = rt; rt = temp;            }else if(!strcmp(s, "right")){                int temp = front;                front = rt; rt = back; back = lt; lt = temp;            }else if(!strcmp(s, "down")){                int temp = up;                up = front; front = down; down = back; back = temp;            }else if(!strcmp(s, "up")){                int temp = down;                down = front; front = up; up = back; back = temp;            }            x += d[front][0]*dist;            y += d[front][1]*dist;            z += d[front][2]*dist;        }        printf("%d %d %d %d\n",x,y,z,front);    }    return 0;}






原创粉丝点击