这是一道简单题。
观察一下数据,发现 ,字母后面的数不超过 ,所以模拟能过。
现在有个问题:如果我们的起始坐标为 ,用数组存,有概率越界,那怎么办呢?
1.你可以把初始坐标设为 。
2.你可以把数组改为 map 。
当然,我选择第二种 (其实是懒) 。
#include<bits/stdc++.h>
using namespace std;
int m,v,x=1,y=1,ans;
char op;
map<int,map<int,int> > ma;
int main(){
cin>>m;
ma[1][1]=1;
while(m--){
cin>>op>>v;
if(op=='S'){
while(v--){
x++;
if(ma[x][y]) ans++;
ma[x][y]=1;
}
}else if(op=='N'){
while(v--){
x--;
if(ma[x][y]) ans++;
ma[x][y]=1;
}
}else if(op=='E'){
while(v--){
y++;
if(ma[x][y]) ans++;
ma[x][y]=1;
}
}else{
while(v--){
y--;
if(ma[x][y]) ans++;
ma[x][y]=1;
}
}
}
cout<<ans;
}