蓝桥杯练习系统(算法训练)ALGO-946 Q神的足球赛

发布于:2024-05-06 ⋅ 阅读:(28) ⋅ 点赞:(0)

资源限制

内存限制:256.0MB   C/C++时间限制:1.0s   Java时间限制:3.0s   Python时间限制:5.0s

问题描述

  足球赛上,只见Q神如闪电般的速度带球时而左,时而右,时而前,时而后,时而上,时而下……等等,有什么奇怪的东西混进去了?假设Q神力量可以突破地心引力,他在一个三维空间里面可以沿着直角坐标系的坐标轴方向前进。告诉你他的每一次转的方向以及前进的距离,请你回答他最后在哪个位置,面朝那一个方向。假设他一开始在(0,0,0),面朝x轴的正方向。

输入格式

  多组输入数据。
  每组第一行是一个整数n。接下来n行每行一个字符表示方向,接着一个整数表示前进距离。其中,f(forward)表示继续前进,方向不变;b(back)表示向后转;l(left)表示向左转;r(right)表示像右转;u(up)表示向上;d(表示向下)。如图示。

输出格式

  对于每一组数据,输出Q神的坐标和他的朝向。其中:左手系的x正方向、y正方向、z正方向分别位0、1、2,对应的负方向分别为3、4、5.

样例输入

6
l 10
r 11
u 12
d 13
f 14
b 15

样例输出

23 -10 12 3

数据规模和约定

  数据组数不超过10组,n《=200 ,每次移动距离不超过100

#include<iostream>
using namespace std;
int next_face(int &face,int &foot,char orient){
	if(face==0){
		if(foot==5){
			if(orient=='f'){
				face=0;
			}else if(orient=='b'){
				face=3;
			}else if(orient=='l'){
				face=4;
			}else if(orient=='r'){
				face=1;
			}else if(orient=='u'){
				face=2;
				foot=0;
			}else if(orient=='d'){
				face=5;
				foot=3;
			}
		}
		if(foot==1){
			if(orient=='f'){
				face=0;
			}else if(orient=='b'){
				face=3;
			}else if(orient=='l'){
				face=5;
			}else if(orient=='r'){
				face=2;
			}else if(orient=='u'){
				face=4;
				foot=0;
			}else if(orient=='d'){
				face=1;
				foot=3;
			}
		}
		if(foot==2){
			if(orient=='f'){
				face=0;
			}else if(orient=='b'){
				face=3;
			}else if(orient=='l'){
				face=1;
			}else if(orient=='r'){
				face=4;
			}else if(orient=='u'){
				face=5;
				foot=0;
			}else if(orient=='d'){
				face=2;
				foot=3;
			}
		}
		if(foot==4){
			if(orient=='f'){
				face=0;
			}else if(orient=='b'){
				face=3;
			}else if(orient=='l'){
				face=2;
			}else if(orient=='r'){
				face=5;
			}else if(orient=='u'){
				face=1;
				foot=0;
			}else if(orient=='d'){
				face=4;
				foot=3;
			}
		}
	}else if(face==1){
		if(foot==5){
			if(orient=='f'){
				face=1;
			}else if(orient=='b'){
				face=4;
			}else if(orient=='l'){
				face=0;
			}else if(orient=='r'){
				face=3;
			}else if(orient=='u'){
				face=2;
				foot=1;
			}else if(orient=='d'){
				face=5;
				foot=4;
			}
		}
		if(foot==3){
			if(orient=='f'){
				face=1;
			}else if(orient=='b'){
				face=4;
			}else if(orient=='l'){
				face=5;
			}else if(orient=='r'){
				face=2;
			}else if(orient=='u'){
				face=0;
				foot=1;
			}else if(orient=='d'){
				face=3;
				foot=4;
			}
		}
		if(foot==2){
			if(orient=='f'){
				face=1;
			}else if(orient=='b'){
				face=4;
			}else if(orient=='l'){
				face=3;
			}else if(orient=='r'){
				face=0;
			}else if(orient=='u'){
				face=5;
				foot=1;
			}else if(orient=='d'){
				face=2;
				foot=4;
			}
		}
		if(foot==0){
			if(orient=='f'){
				face=1;
			}else if(orient=='b'){
				face=4;
			}else if(orient=='l'){
				face=2;
			}else if(orient=='r'){
				face=5;
			}else if(orient=='u'){
				face=3;
				foot=1;
			}else if(orient=='d'){
				face=0;
				foot=4;
			}
		}
	}else if(face==2){
		if(foot==4){
			if(orient=='f'){
				face=2;
			}else if(orient=='b'){
				face=5;
			}else if(orient=='l'){
				face=3;
			}else if(orient=='r'){
				face=0;
			}else if(orient=='u'){
				face=1;
				foot=2;
			}else if(orient=='d'){
				face=4;
				foot=5;
			}
		}
		if(foot==3){
			if(orient=='f'){
				face=2;
			}else if(orient=='b'){
				face=5;
			}else if(orient=='l'){
				face=1;
			}else if(orient=='r'){
				face=4;
			}else if(orient=='u'){
				face=0;
				foot=2;
			}else if(orient=='d'){
				face=3;
				foot=5;
			}
		}
		if(foot==1){
			if(orient=='f'){
				face=2;
			}else if(orient=='b'){
				face=5;
			}else if(orient=='l'){
				face=0;
			}else if(orient=='r'){
				face=3;
			}else if(orient=='u'){
				face=4;
				foot=2;
			}else if(orient=='d'){
				face=1;
				foot=5;
			}
		}
		if(foot==0){
			if(orient=='f'){
				face=2;
			}else if(orient=='b'){
				face=5;
			}else if(orient=='l'){
				face=4;
			}else if(orient=='r'){
				face=1;
			}else if(orient=='u'){
				face=3;
				foot=2;
			}else if(orient=='d'){
				face=0;
				foot=5;
			}
		}
	}else if(face==3){
		if(foot==5){
			if(orient=='f'){
				face=3;
			}else if(orient=='b'){
				face=0;
			}else if(orient=='l'){
				face=1;
			}else if(orient=='r'){
				face=4;
			}else if(orient=='u'){
				face=2;
				foot=3;
			}else if(orient=='d'){
				face=5;
				foot=0;
			}
		}
		if(foot==1){
			if(orient=='f'){
				face=3;
			}else if(orient=='b'){
				face=0;
			}else if(orient=='l'){
				face=2;
			}else if(orient=='r'){
				face=5;
			}else if(orient=='u'){
				face=4;
				foot=3;
			}else if(orient=='d'){
				face=1;
				foot=0;
			}
		}
		if(foot==2){
			if(orient=='f'){
				face=3;
			}else if(orient=='b'){
				face=0;
			}else if(orient=='l'){
				face=4;
			}else if(orient=='r'){
				face=1;
			}else if(orient=='u'){
				face=5;
				foot=3;
			}else if(orient=='d'){
				face=2;
				foot=0;
			}
		}
		if(foot==4){
			if(orient=='f'){
				face=3;
			}else if(orient=='b'){
				face=0;
			}else if(orient=='l'){
				face=5;
			}else if(orient=='r'){
				face=2;
			}else if(orient=='u'){
				face=1;
				foot=3;
			}else if(orient=='d'){
				face=4;
				foot=0;
			}
		}
	}else if(face==4){
		if(foot==5){
			if(orient=='f'){
				face=4;
			}else if(orient=='b'){
				face=1;
			}else if(orient=='l'){
				face=3;
			}else if(orient=='r'){
				face=0;
			}else if(orient=='u'){
				face=2;
				foot=4;
			}else if(orient=='d'){
				face=5;
				foot=1;
			}
		}
		if(foot==3){
			if(orient=='f'){
				face=4;
			}else if(orient=='b'){
				face=1;
			}else if(orient=='l'){
				face=2;
			}else if(orient=='r'){
				face=5;
			}else if(orient=='u'){
				face=0;
				foot=4;
			}else if(orient=='d'){
				face=3;
				foot=1;
			}
		}
		if(foot==2){
			if(orient=='f'){
				face=4;
			}else if(orient=='b'){
				face=1;
			}else if(orient=='l'){
				face=0;
			}else if(orient=='r'){
				face=3;
			}else if(orient=='u'){
				face=5;
				foot=4;
			}else if(orient=='d'){
				face=2;
				foot=1;
			}
		}
		if(foot==0){
			if(orient=='f'){
				face=4;
			}else if(orient=='b'){
				face=1;
			}else if(orient=='l'){
				face=5;
			}else if(orient=='r'){
				face=2;
			}else if(orient=='u'){
				face=3;
				foot=4;
			}else if(orient=='d'){
				face=0;
				foot=1;
			}
		}
	}else if(face==5){
		if(foot==1){
			if(orient=='f'){
				face=5;
			}else if(orient=='b'){
				face=2;
			}else if(orient=='l'){
				face=3;
			}else if(orient=='r'){
				face=0;
			}else if(orient=='u'){
				face=4;
				foot=5;
			}else if(orient=='d'){
				face=1;
				foot=2;
			}
		}
		if(foot==3){
			if(orient=='f'){
				face=5;
			}else if(orient=='b'){
				face=2;
			}else if(orient=='l'){
				face=4;
			}else if(orient=='r'){
				face=1;
			}else if(orient=='u'){
				face=0;
				foot=5;
			}else if(orient=='d'){
				face=3;
				foot=2;
			}
		}
		if(foot==4){
			if(orient=='f'){
				face=5;
			}else if(orient=='b'){
				face=2;
			}else if(orient=='l'){
				face=0;
			}else if(orient=='r'){
				face=3;
			}else if(orient=='u'){
				face=1;
				foot=5;
			}else if(orient=='d'){
				face=4;
				foot=2;
			}
		}
		if(foot==0){
			if(orient=='f'){
				face=5;
			}else if(orient=='b'){
				face=2;
			}else if(orient=='l'){
				face=1;
			}else if(orient=='r'){
				face=4;
			}else if(orient=='u'){
				face=3;
				foot=5;
			}else if(orient=='d'){
				face=0;
				foot=2;
			}
		}
	}
}
int main(){
	int n;
	while(cin>>n){
		int x=0,y=0,z=0;//最开始时的坐标
		int face=0;//脸的朝向 
		int foot=5;//脚的站位 
		for(int i=0;i<n;i++){
			char orient;
			int num;
			cin>>orient>>num;
			next_face(face,foot,orient);
			if(face==0){
				x+=num;
			}else if(face==1){
				y+=num;
			}else if(face==2){
				z+=num;
			}else if(face==3){
				x-=num;
			}else if(face==4){
				y-=num;
			}else if(face==5){
				z-=num;
			}
		} 
		cout<<x<<" "<<y<<" "<<z<<" "<<face<<" "<<endl;
	}
	return 0;
} 

思路:需要知道Q神的脸的朝向、脚的站位(头指向脚的方向)才能确定Q神的位置。已知脸的朝向,可能有4个站位。

例如:初始位置:脸的朝向为0,脚的站位为5