codeforces 298B Sail

版权声明:本文为博主原创文章,转载请联系博主(通常都会同意就是了) https://blog.csdn.net/cccccwb/article/details/86572946

题解:只要按照对应方向扫一遍,能走就走,不能走就停下,看看能不能走到就OK了……

代码如下:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string> 
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

int n;
long long sx,sy,tx,ty;
char s[100010];

int main()
{
	int ans=-1;
	scanf("%d%lld%lld%lld%lld",&n,&sx,&sy,&tx,&ty);gets(s);
	gets(s);if(s[0]<'A'||s[0]>'Z') gets(s);
	if(sx==tx&&sy==ty){puts("0");return 0;}
	for(int i=0;i<n;i++)
	{
	   if(s[i]=='E'&&tx>sx) sx++;
	   if(s[i]=='W'&&tx<sx) sx--;
	   if(s[i]=='N'&&ty>sy) sy++;
	   if(s[i]=='S'&&ty<sy) sy--;
	   if(sx==tx&&sy==ty){ans=i+1;break;}
	}
	printf("%d\n",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/cccccwb/article/details/86572946
今日推荐