void go(int &x,int &y,int &z,int &f,int &r,int &u,int d,int move)
{
int t;
switch(d)
{
case 0:break; //前
case 1:f=-f;r=-r;break; //后
case 2:t=f;f=-r;r=t;break; //向左
case 3:t=f;f=r;r=-t;break; //向右
case 4:t=f;f=u;u=-t;break; //向上
case 5: t=f;f=-u;u=-t;break; //向下
}
switch(f)
{
case 7:x+=move;break;
case 8:y+=move;break;
case 9:z+=move;break;
case -7:x-=move;break;
case -8:y-=move;break;
case -9:z-=move;break;
}
}
ACM poj 1835宇航员题,我写的代码已经运行啦,但一提交就是WA,高手们帮我看看吧
答案:2 悬赏:40 手机版
解决时间 2021-12-28 11:46
- 提问者网友:王者佥
- 2021-12-28 02:55
最佳答案
- 五星知识达人网友:不想翻身的咸鱼
- 2021-12-28 04:16
#include
typedef struct
{
int x,y,z;
}dir;
void forward(dir* loc, dir* face, dir* top, int x)
{
loc->x += face->x * x;
loc->y += face->y * x;
loc->z += face->z * x;
}
void back(dir* loc, dir* face, dir* top, int x)
{
face->x *= -1;
face->y *= -1;
face->z *= -1;
forward(loc,face,top,x);
}
void right(dir* loc, dir* face, dir* top, int x)
{
dir temp;
temp.x = top->y * face->z - face->y * top->z;
temp.y = top->z * face->x - face->z * top->x;
temp.z = top->x * face->y - face->x * top->y;
*face = temp;
forward(loc,face,top,x);
}
void left(dir* loc, dir* face, dir* top, int x)
{
dir temp;
temp.x = face->y * top->z - top->y * face->z;
temp.y = face->z * top->x - top->z * face->x;
temp.z = face->x * top->y - top->x * face->y;
*face = temp;
forward(loc,face,top,x);
}
void up(dir* loc, dir* face, dir* top, int x)
{
dir temp = *top;
*top = *face;
top->x *= -1;
top->y *= -1;
top->z *= -1;
*face = temp;
forward(loc,face,top,x);
}
void down(dir* loc, dir* face, dir* top, int x)
{
dir temp = *face;
*face = *top;
face->x *= -1;
face->y *= -1;
face->z *= -1;
*top = temp;
forward(loc,face,top,x);
}
int main()
{
int m,n,x;
dir loc,top,face;
char cmd[8];
scanf("%d",&m);
while(m--)
{
loc.x = loc.y = loc.z = 0;
top.x = top.y = 0;
top.z = 1;
face.z = face.y = 0;
face.x = 1;
scanf("%d",&n);
while(n--)
{
scanf("%s%d",cmd,&x);
switch(cmd[0])
{
case 'l':
left(&loc,&face,&top,x);
break;
case 'r':
right(&loc,&face,&top,x);
break;
case 'u':
up(&loc,&face,&top,x);
break;
case 'd':
down(&loc,&face,&top,x);
break;
case 'b':
back(&loc,&face,&top,x);
break;
case 'f':
forward(&loc,&face,&top,x);
break;
}
}
printf("%d %d %d ",loc.x,loc.y,loc.z);
if(face.x == 1)
{
puts("0");
}
else if(face.y == 1)
{
puts("1");
}
else if(face.z == 1)
{
puts("2");
}
else if(face.x == -1)
{
puts("3");
}
else if(face.y == -1)
{
puts("4");
}
else if(face.z == -1)
{
puts("5");
}
}
}
typedef struct
{
int x,y,z;
}dir;
void forward(dir* loc, dir* face, dir* top, int x)
{
loc->x += face->x * x;
loc->y += face->y * x;
loc->z += face->z * x;
}
void back(dir* loc, dir* face, dir* top, int x)
{
face->x *= -1;
face->y *= -1;
face->z *= -1;
forward(loc,face,top,x);
}
void right(dir* loc, dir* face, dir* top, int x)
{
dir temp;
temp.x = top->y * face->z - face->y * top->z;
temp.y = top->z * face->x - face->z * top->x;
temp.z = top->x * face->y - face->x * top->y;
*face = temp;
forward(loc,face,top,x);
}
void left(dir* loc, dir* face, dir* top, int x)
{
dir temp;
temp.x = face->y * top->z - top->y * face->z;
temp.y = face->z * top->x - top->z * face->x;
temp.z = face->x * top->y - top->x * face->y;
*face = temp;
forward(loc,face,top,x);
}
void up(dir* loc, dir* face, dir* top, int x)
{
dir temp = *top;
*top = *face;
top->x *= -1;
top->y *= -1;
top->z *= -1;
*face = temp;
forward(loc,face,top,x);
}
void down(dir* loc, dir* face, dir* top, int x)
{
dir temp = *face;
*face = *top;
face->x *= -1;
face->y *= -1;
face->z *= -1;
*top = temp;
forward(loc,face,top,x);
}
int main()
{
int m,n,x;
dir loc,top,face;
char cmd[8];
scanf("%d",&m);
while(m--)
{
loc.x = loc.y = loc.z = 0;
top.x = top.y = 0;
top.z = 1;
face.z = face.y = 0;
face.x = 1;
scanf("%d",&n);
while(n--)
{
scanf("%s%d",cmd,&x);
switch(cmd[0])
{
case 'l':
left(&loc,&face,&top,x);
break;
case 'r':
right(&loc,&face,&top,x);
break;
case 'u':
up(&loc,&face,&top,x);
break;
case 'd':
down(&loc,&face,&top,x);
break;
case 'b':
back(&loc,&face,&top,x);
break;
case 'f':
forward(&loc,&face,&top,x);
break;
}
}
printf("%d %d %d ",loc.x,loc.y,loc.z);
if(face.x == 1)
{
puts("0");
}
else if(face.y == 1)
{
puts("1");
}
else if(face.z == 1)
{
puts("2");
}
else if(face.x == -1)
{
puts("3");
}
else if(face.y == -1)
{
puts("4");
}
else if(face.z == -1)
{
puts("5");
}
}
}
全部回答
- 1楼网友:雪起风沙痕
- 2021-12-28 05:21
搜一下:ACM poj 1835宇航员题,我写的代码已经运行啦,但一提交就是WA,高手们帮我看看吧
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯