码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1835 宇航员

时间:2015-05-06 21:03:30      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

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

宇航员
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 4802   Accepted: 2058

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

 
 
分析:
暴力模拟每种状态。
 
 
AC代码:
  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<cstring>
  4 #include<queue>
  5 #include<iostream>
  6 #include<stack>
  7 #include<map>
  8 #include<string>
  9 using namespace std;
 10 int main(){
 11     char ch[20];
 12     int n, t, tcase;
 13     scanf("%d", &tcase);
 14     while(tcase--){
 15         scanf("%d", &n);
 16         int face, head, x, y, z, num;
 17         x = y = z = face = 0;
 18         head = 2;
 19         for(int i = 0; i < n; i++){
 20             scanf("%s%d", ch, &num);
 21             if(ch[0] == f){
 22                 if(face == 0) x += num;
 23                 if(face == 1) y += num;
 24                 if(face == 2) z += num;
 25                 if(face == 3) x -= num;
 26                 if(face == 4) y -= num;
 27                 if(face == 5) z -= num;
 28             }
 29             else if(ch[0] == b){
 30                 face = (face+3)%6;
 31                 if(face == 0) x += num;
 32                 if(face == 1) y += num;
 33                 if(face == 2) z += num;
 34                 if(face == 3) x -= num;
 35                 if(face == 4) y -= num;
 36                 if(face == 5) z -= num;
 37             }
 38             else if(ch[0] == l){
 39                 if(face == 0 && head == 1) face = 2;
 40                 else if(face == 0 && head == 4) face = 5;
 41                 else if(face == 0 && head == 2) face = 4;
 42                 else if(face == 0 && head == 5) face = 1;
 43                 
 44                 else if(face == 1 && head == 0) face = 5;
 45                 else if(face == 1 && head == 3) face = 2;
 46                 else if(face == 1 && head == 2) face = 0;
 47                 else if(face == 1 && head == 5) face = 3;
 48                 
 49                 else if(face == 2 && head == 0) face = 1;
 50                 else if(face == 2 && head == 3) face = 4;
 51                 else if(face == 2 && head == 1) face = 3;
 52                 else if(face == 2 && head == 4) face = 0;
 53                 
 54                 else if(face == 3 && head == 1) face = 5;
 55                 else if(face == 3 && head == 4) face = 2;
 56                 else if(face == 3 && head == 2) face = 1;
 57                 else if(face == 3 && head == 5) face = 4;
 58                 
 59                 else if(face == 4 && head == 0) face = 2;
 60                 else if(face == 4 && head == 3) face = 5;
 61                 else if(face == 4 && head == 2) face = 3;
 62                 else if(face == 4 && head == 5) face = 0;
 63                 
 64                 else if(face == 5 && head == 0) face = 4;
 65                 else if(face == 5 && head == 3) face = 1;
 66                 else if(face == 5 && head == 4) face = 3;
 67                 else if(face == 5 && head == 1) face = 0;
 68                 
 69                 if(face == 0) x += num;
 70                 if(face == 1) y += num;
 71                 if(face == 2) z += num;
 72                 if(face == 3) x -= num;
 73                 if(face == 4) y -= num;
 74                 if(face == 5) z -= num;
 75             }
 76             else if(ch[0] == r){
 77                 if(face == 0 && head == 1) face = 5;
 78                 else if(face == 0 && head == 4) face = 2;
 79                 else if(face == 0 && head == 2) face = 1;
 80                 else if(face == 0 && head == 5) face = 4;
 81                 
 82                 else if(face == 1 && head == 0) face = 2;
 83                 else if(face == 1 && head == 3) face = 5;
 84                 else if(face == 1 && head == 2) face = 3;
 85                 else if(face == 1 && head == 5) face = 0;
 86                 
 87                 else if(face == 2 && head == 0) face = 4;
 88                 else if(face == 2 && head == 3) face = 1;
 89                 else if(face == 2 && head == 1) face = 0;
 90                 else if(face == 2 && head == 4) face = 3;
 91                 
 92                 else if(face == 3 && head == 1) face = 2;
 93                 else if(face == 3 && head == 4) face = 5;
 94                 else if(face == 3 && head == 2) face = 4;
 95                 else if(face == 3 && head == 5) face = 1;
 96                 
 97                 else if(face == 4 && head == 0) face = 5;
 98                 else if(face == 4 && head == 3) face = 2;
 99                 else if(face == 4 && head == 2) face = 0;
100                 else if(face == 4 && head == 5) face = 3;
101                 
102                 else if(face == 5 && head == 0) face = 1;
103                 else if(face == 5 && head == 3) face = 4;
104                 else if(face == 5 && head == 4) face = 0;
105                 else if(face == 5 && head == 1) face = 3;
106                 
107                 if(face == 0) x += num;
108                 if(face == 1) y += num;
109                 if(face == 2) z += num;
110                 if(face == 3) x -= num;
111                 if(face == 4) y -= num;
112                 if(face == 5) z -= num;
113             }
114             else if(ch[0] == u){
115                 t = face;
116                 face = head;
117                 head = (t+3)%6;
118                 if(face == 0) x += num;
119                 if(face == 1) y += num;
120                 if(face == 2) z += num;
121                 if(face == 3) x -= num;
122                 if(face == 4) y -= num;
123                 if(face == 5) z -= num;
124             }
125             else if(ch[0] == d){
126                 t = face;
127                 face = (head+3)%6;
128                 head = t;
129                 if(face == 0) x += num;
130                 if(face == 1) y += num;
131                 if(face == 2) z += num;
132                 if(face == 3) x -= num;
133                 if(face == 4) y -= num;
134                 if(face == 5) z -= num;
135             }
136             //printf("%d %d %d %d %d\n", x, y, z, face, head);
137         }
138         printf("%d %d %d %d\n", x, y, z, face);
139     }
140     return 0;
141 }

 

poj 1835 宇航员

标签:

原文地址:http://www.cnblogs.com/jeff-wgc/p/4482994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!