OpenJudge / Poj 1835 宇航员 C++

链接地址:

Poj:http://poj.org/problem?id=1835

OpenJudge:http://bailian.openjudge.cn/practice/1835/

题目:

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 4286   Accepted: 1840

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

Source

思路:

(1)记录x,y,z 和前方,头顶方向,右前方

模拟题,按要求一步步来即可,关键是要搞清楚方向怎么变化

代码:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 char arr_dir[][10] = {"forward","back","left","right","up","down"};
 7 int main()
 8 {
 9     int m;
10     int n;
11     int x,y,z,f,fr,h;
12     char dir[10];
13     int mov;
14     int temp;
15     scanf("%d",&m);
16     while(m--)
17     {
18         scanf("%d",&n);
19         x = 0; y = 0; z = 0; f = 0; fr = 1; h = 2;//f forword,h head,fr forward right
20         while(n--)
21         {
22             scanf("%s%d",dir,&mov);
23             if(!strcmp(dir,arr_dir[1]))//back
24             {
25                 f = (f + 3) % 6;
26                 fr = (fr + 3) % 6;
27             }
28             else if(!strcmp(dir,arr_dir[2]))//left
29             {
30                 temp = f;
31                 f = (fr + 3) % 6;
32                 fr = temp;
33             }
34             else if(!strcmp(dir,arr_dir[3]))//right
35             {
36                 temp = f;
37                 f = fr;
38                 fr = (temp + 3) % 6;
39             }
40             else if(!strcmp(dir,arr_dir[4]))//up
41             {
42                 temp = f;
43                 f = h;
44                 h = (temp + 3) % 6;
45             }
46             else if(!strcmp(dir,arr_dir[5]))//down
47             {
48                 temp = f;
49                 f = (h + 3) % 6;
50                 h = temp;
51             }
52             if(f==0) x+=mov;
53             else if(f == 1) y+=mov;
54             else if(f == 2) z+=mov;
55             else if(f == 3) x-=mov;
56             else if(f == 4) y-=mov;
57             else z-=mov;
58         }
59         printf("%d %d %d %d
",x,y,z,f);
60     }
61     return 0;
62 }
原文地址:https://www.cnblogs.com/mobileliker/p/3512481.html