﻿POJ 1835 宇航员-Ocrosoft

# POJ 1835 宇航员

Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%lld & %llu

Description

forward x 　向前走x米。
back x　先转向后，再走x米。
left x 先转向左，再走x米。
right x 先转向右，再走x米。
up x 先面向上，再走x米。
down x 先面向下，再走x米。

Input

Output

Sample Input

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

Sample Output

`23 -10 12 3`

Solution

```思路：三维空间要用三个变量去记录方位，分别是面向的方向，左边(或右边)的方向，上面(或下面)的方向。

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#define ms(a) memset(a,0,sizeof(a))
typedef long long ll;
const int INF = INT_MAX / 2;
const int MAXN = 10000 + 10;
using namespace std;
int n, m;
int x, y, z;
int bd(int p)//计算负方向
{
return (p + 3) % 6;
}
void go(int p, int step)
{
if (p == 0)x += step;
else if (p == 3)x -= step;
else if (p == 1)y += step;
else if (p == 4)y -= step;
else if (p == 2)z += step;
else if (p == 5)z -= step;
}
int main()
{
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
x = y = z = 0;
int forw = 0, left = 4, up = 2;//前、左、上
while (n--)
{
int step, t;
char s[MAXN];
scanf("%s%d", s, &step);
switch (s[0])
{
case 'f':
break;
case 'b':
forw = bd(forw);
left = bd(left);
break;
case 'l':
t = forw;
forw = left;
left = bd(t);
break;
case 'r':
t = forw;
forw = bd(left);
left = t;
break;
case 'u':
t = forw;
forw = up;
up = bd(t);
break;
case 'd':
t = forw;
forw = bd(up);
up = t;
break;
default:
break;
}
go(forw, step);
}
printf("%d %d %d %d\n", x, y, z, forw);
}
return 0;
}
```