[NOIP2016]玩具谜题

题目传送门

sol

给你n个数,m个指令,我们可以分成四种情况来讨论:
①往左边数且当前位置朝内
②往左边数且当前位置朝外
③往右边数且当前位置朝内
④往右边数且当前位置朝外
这样来解决问题,将问题简单化,自然就迎刃而解了

code

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
struct node{
	int chao;//朝向
	char name[11];//名字
}e[maxn];
int n,m;
bool flag;
inline int solve(int fangx,int step,int now){//fangx为方向,step为步数,now为移动前的位置	
	flag=false;
	if(fangx==0){//左数 
		if(e[now].chao==0){//朝内左数
			now-=step;if(now<1)now+=n;
			flag=true;
		}
		if(e[now].chao==1&&!flag) now+=step;if(now>n)now-=n;//朝外左数 
	}
	if(fangx==1){//右数 
		if(e[now].chao==0){//朝内右数 
			now+=step;if(now>n)now-=n;
			flag=true;
		}
		if(e[now].chao==1&&!flag) now-=step;if(now<1)now+=n;//朝外右数
	}
	return now;
}
int x;
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%d%s",&e[i].chao,e[i].name+1);
	int x=1;
	for(int i=1,a,s;i<=m;i++){
		scanf("%d%d",&a,&s);
		x=solve(a,s,x);//solve函数计算每一次询问后的位置
	}
	printf("%s",e[x].name+1);//完美输出
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/qzwer/p/13168178.html