x,y数组存储之前在哪个位置
now存储目前位于哪个版本
对于wasd直接now++后更新
对于z直接now--回退到上一个版本(注意判断now>0)
#include<bits/stdc++.h>
using namespace std;
int n,now;
int x[100005],y[100005];
char t;
int main()
{
cin>>n;
while(n--){
cin>>t;
if(t=='W')
{
++now;
x[now]=x[now-1];
y[now]=y[now-1]+1;
}
if(t=='A')
{
++now;
x[now]=x[now-1]-1;
y[now]=y[now-1];
}
if(t=='S')
{
++now;
x[now]=x[now-1];
y[now]=y[now-1]-1;
}
if(t=='D')
{
++now;
x[now]=x[now-1]+1;
y[now]=y[now-1];
}
if(t=='Z')
{
if(now)
--now;
}
}
cout<<x[now]<<" "<<y[now]<<"\n";
return 0;
}
京公网安备 11010502036488号