poj 1835 宇航员

题目链接:http://poj.org/problem?id=1835

这是一道模拟题,不过不是太好做,主要原因就是方向不好控制,三维空间,六个方向。 本题的关键就是要控制好头和脸的方向,利用打表的方法做(表中把所有的可能结果都列出来)。

具体是:其中左转和右转要特别注意脸的方向(打表给出),头的方向是不变的。up其实就是脸的方向变成了头的方向,头的方向变成了脸原来的方向向后。down是头的方向变成了脸的方向,脸的方向变成了头原来方向的反方向。向后直接头不变,脸反向。

View Code
 1 #include<cstdio>
2 #include<iostream>
3 using namespace std;
4
5 int hl[6][6]={{0,5,1,0,2,4},
6 {2,0,3,5,0,0},
7 {4,0,0,1,3,0},
8 {0,2,4,0,5,1},
9 {5,0,0,2,0,3},
10 {1,3,0,4,0,0}};
11
12 int bk[6]={3,4,5,0,1,2};
13 int head,face,temp,step;
14 int x,y,z;
15
16 void forward(int f)
17 {
18 switch(f)
19 {
20 case 0:x+=step;
21 break;
22 case 1:y+=step;
23 break;
24 case 2:z+=step;
25 break;
26 case 3:x-=step;
27 break;
28 case 4:y-=step;
29 break;
30 case 5:z-=step;
31 break;
32 }
33 }
34
35 int main()
36 {
37 int T,n,i;
38 char dir[10];
39 scanf("%d",&T);
40 while(T--)
41 {
42 scanf("%d",&n);
43 head=2;
44 face=0;
45 x=y=z=0;
46 for(i=0;i<n;i++)
47 {
48 scanf("%s%d",dir,&step);
49 switch(dir[0])
50 {
51 case 'f':forward(face);
52 break;
53 case 'b':
54 face=bk[face];
55 forward(face);
56 break;
57 case 'r':
58
59 face=bk[hl[head][face]];
60 forward(face);
61 break;
62 case 'l':
63
64 face=hl[head][face];
65 forward(face);
66 break;
67 case 'u':
68 temp=face;
69 face=head;
70 head=bk[temp];
71 forward(face);
72 break;
73 case 'd':
74 temp=head;
75 head=face;
76 face=bk[temp];
77 forward(face);
78 break;
79 }
80 }
81 printf("%d %d %d %d\n",x,y,z,face);
82 }
83 return 0;
84 }

 

posted @ 2012-02-25 16:19  我们一直在努力  阅读(593)  评论(0编辑  收藏  举报