牛客IOI周赛20-普及组移动撤销

移动撤销

https://ac.nowcoder.com/acm/contest/8997/B

//用栈模拟操作
#include<bits/stdc++.h>
using namespace std;
string str = "";//对z作出处理后的最后移动方式
stack<char>st;
int n;
int main()
{
cin >> n;
char ch;
while (cin >> ch)
{
if (ch == 'Z')//撤销操作
{
if (st.empty())//如果栈没有元素
continue;
else
st.pop();//如果栈有wasd操作,弹出栈首,相当于撤销了
}
else
{
st.push(ch);//没有撤销入栈做最后模拟操作
}
}
while (!st.empty())
{
str += st.top();
st.pop();
}
int x = 0, y = 0;//起始位置
for (int i = str.length(); i >= 0; i--)//栈是先进后出,所以我们从最后一个开始模拟
{
if (str[i] == 'W') ++y;
if (str[i] == 'A') --x;
if (str[i] == 'S') --y;
if (str[i] == 'D') ++x;
}
cout << x << " " << y<<endl;//输出坐标
return 0;
}</char>

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务