真是神题
其实递归的写是不难写的
我在昏昏欲睡的时候参考了别人的代码 写了手工栈非递归的 意识模糊
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<string>
#include<iostream>
using namespace std;
string ret;
int tot;
inline void P(int x,int y,int z){
++tot;
printf("%d %s %d\n",x,!y?"left":"right",z);fflush(stdout);
cin>>ret; if (ret=="treasure") exit(0);
}
int m;
stack<int> p,pos,low,fa;
#define oo 1<<30
#define opt (ret=="right")
int main(){
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
cin>>m;
p.push(0); pos.push(-1); low.push(oo); fa.push(-1);
cin>>ret;
while (1){
if (p.top()==m){
int cnt=0;
P(pos.top()%m,0,pos.top()%m),cnt++;
while (ret!="right") P(0,0,0),cnt++;
do P(0,1,0),cnt++; while (ret!="left");
while (--cnt) P(0,opt,0);
int tmp=low.top();
p.pop(); pos.pop(); low.pop();
if (tmp<low.top())
pos.top()=fa.top(),low.top()=tmp;
fa.pop();
}else{
p.top()++; P(1,1,1);
string _ret=ret;
if (_ret=="center"){
fa.push(p.top()); p.push(0); pos.push(-1); low.push(oo);
}else if (_ret=="right"){
int cnt=0,tmp;
P(0,0,0);
while (ret!="left") P(0,1,0),cnt++;
tmp=cnt; while (tmp--) P(0,1,0);
if (cnt){
int dep=(int)p.size()-cnt;
if (dep<low.top())
pos.top()=p.top(),low.top()=dep;
}else
P(0,1,0);
}else{
int cnt=0,dep=(int)p.size()+1;
do P(0,opt,0),cnt++; while (ret!="right");
do P(0,opt,0),cnt++,dep--; while (ret!="left");
while (--cnt) P(0,opt,0);
if (dep<low.top())
pos.top()=p.top(),low.top()=dep;
}
}
}
return 0;
}