题目传送门
题意: 给一个字符串,按照字符串的N,S,W,E移动,每次移动的距离为1,对于一条边,如果你走过这条边,则代价+1,如果没走过代价+5,问最后总代价。(ps:比如从(0,0)走到(0,1),我们看的是中间这段距离走过没有而不是看(0,1)这个点走过没有)。
思路 map<pii,int>ma[4]记录一个点的周围四条边,每次走先查询是否走过,再把相关的边记录下就行。
#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define vi vector<int>
#define mii map<int,int>
#define pii pair<int,int>
#define ull unsigned long long
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}
return x*f;}
using namespace std;
const int N=1e5+5;
const int inf=0x7fffffff;
const int mod=1e9+7;
const double eps=1e-6;
signed main()
{
int t;
cin>>t;
while(t--)
{
map<pii,int>ma[4];
string s;
cin>>s;
int x=0,y=0,res=0;
for(auto i:s)
{
if(i=='N')
{
if(ma[0][mp(x,y)]==1||ma[1][mp(x-1,y)]==1)
res++;
else
res+=5;
ma[0][mp(x,y)]=1;
ma[1][mp(x-1,y)]=1;
x-=1;
}
if(i=='S')
{
if(ma[1][mp(x,y)]==1||ma[0][mp(x+1,y)]==1)
res++;
else
res+=5;
ma[1][mp(x,y)]=1;
ma[0][mp(x+1,y)]=1;
x+=1;
}
if(i=='E')
{
if(ma[2][mp(x,y)]==1||ma[3][mp(x,y+1)]==1)
res++;
else
res+=5;
ma[2][mp(x,y)]=1;
ma[3][mp(x,y+1)]=1;
y+=1;
}
if(i=='W')
{
if(ma[3][mp(x,y)]==1||ma[2][mp(x,y-1)]==1)
res++;
else
res+=5;
ma[3][mp(x,y)]=1;
ma[2][mp(x,y-1)]=1;
y-=1;
}
}
cout<<res<<endl;
}
}