B. Sail

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).

  • If the wind blows to the east, the boat will move to (x + 1, y).
  • If the wind blows to the south, the boat will move to (x, y - 1).
  • If the wind blows to the west, the boat will move to (x - 1, y).
  • If the wind blows to the north, the boat will move to (x, y + 1).

Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?

Input

The first line contains five integers t, sx, sy, ex, ey (1 ≤ t ≤ 105,  - 109 ≤ sx, sy, ex, ey ≤ 109). The starting location and the ending location will be different.

The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).

Output

If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).

Examples

input

5 0 0 1 1
SESNW

output

4

input

10 5 3 3 6
NENSWESNEE

output

-1

Note

In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.

In the second sample, they cannot sail to the destination.

题目大意:

北极熊要去钓鱼了。他们计划从(sx,sy)航行到(ex,ey)。然而,这艘船只能靠风航行。每过一秒,风都会向东、南、西或北的方向吹。假设船当前位于(x,y)。

如果风吹向东方,船将移动到(x+y,)。
如果风向南吹,船将移动到(x,y-1)。
如果风向向西,船将移动到(x-1,y)。
如果风吹向北方,船将移动到(x,y+1)。
或者,他们可以用锚把船固定住。在这种情况下,船保持在(x,y)。考虑到T秒的风向,他们最早的航行时间是什么时候(例如,ey)?

输入
第一行包含五个整数t,sx,sy,ex,ey(1≤≤105,-109≤sx,sy,ex,ey≤109)。开始位置和结束位置将不同。

第二行包含T字符,第i个字符是第i秒的风向。这将是四种可能性之一:“E”(东)、“S”(南)、“W”(西)和“N”(北)。

输出
如果他们能在T秒内达到(例如,ey),则打印他们能达到的最早时间。否则,打印“-1”(不带引号)。

分析:只要判断每次操作后是否与之前的距离缩短即可,能够缩短就前进一步,否则下锚(即不进行任何操作),每次操作完后检查是否。如果到终点了结束,输出步数即可。当所有的操作都遍历完后,如果还没有到达终点输出-1结束即可。

#include<stdio.h>
#include<string.h>
int abs(int a)
{return a>=0?a:-a;
}
const int M=1e5+5;
int t,sx,sy,ex,ey,i=0,tw=0,ans=1;
char a[M];
int main()
{scanf("%d %d %d %d %d",&t,&sx,&sy,&ex,&ey);//输入起点终点坐标scanf("%s",a);//输入操作步骤int x=sx;int y=sy;int tempx,tempy;while(1){tempx=x;tempy=y;if(i==t)//判断所有操作步骤是否完成{ans=0;//如果完成后还没有到达终点,标记结束break;}switch(a[i])//如果还没有遍历完所有步骤{           //则判断本次步骤的方向,并从该方向前进一步case 'N':{tempy+=1;break;}case 'S':{tempy-=1;break;}case 'E':{tempx+=1;break;}case 'W':{tempx-=1;break;}}if(abs(ex-x)>abs(ex-tempx)||abs(ey-y)>abs(ey-tempy)){   //如果前进后的距离小于先前距离,则变为正式向这个方向前进一步x=tempx;y=tempy;i++;tw++;}else//否则忽略本次操作{i++;tw++;}if(x==ex&&y==ey)//操作完后,判断是否到达终点,如果到达,输出步数结束{break;}}if(ans==0)//判断结果,并输出对应结果printf("-1\n");elseprintf("%d\n",tw);
}

codeforce-298B Sail(模拟)相关推荐

  1. CodeForces - 298B Sail (思维题)

    CodeForces - 298B Sail The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey ...

  2. codeforces 298B Sail

    题解:只要按照对应方向扫一遍,能走就走,不能走就停下,看看能不能走到就OK了-- 代码如下: #include<iostream> #include<cstdio> #incl ...

  3. Codeforces 298B Sail 题解

    题意 一开始你的船在(sx,sy)处,要去(ex.ey),给定t秒内每一秒的风向,每一秒可以选择顺着风的方向走一格或者不动,问能否在t秒内到达目的地,可以的话最早可以在多少秒到 思路 只有当顺着风的方 ...

  4. 【CodeForces - 298B 】Sail (模拟,题意)

    题干: The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boa ...

  5. Sail(CodeForces - 298B )

    The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat ca ...

  6. Codeforces Round #180 (Div. 2) B. Sail 【模拟】

    B. Sail time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  7. 每日一套codeforce集训1119E[贪心],821C[栈模拟],645D[拓扑排序]

    有n种长度的棍子,长度分别为2^0 ,2 ^ 1,-,2 ^ (n-1) ,每种棍子有a[i] 种,问你能组成多少个三角形. 三角形两边之和大于第三边,而2 ^ i + 2 ^ i = 2 ^ (i+ ...

  8. codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)

    ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...

  9. Codeforce 1255 Round #601 (Div. 2) C. League of Leesins (大模拟)

    Bob is an avid fan of the video game "League of Leesins", and today he celebrates as the L ...

最新文章

  1. 怎么进u8系统服务器网址,服务器地址变更后如何进入u8
  2. waf旁路oracle客户端,WAF Bypass数据库特性(Oracle探索篇)
  3. 如何在10亿数中找出前1000大的数
  4. Norse Attack Map网络攻击实时监测地图
  5. pip错误:TypeError: parse() got an unexpected keyword argument 'transport_encoding'
  6. python爬虫教程(一)
  7. 寻路机器人单片机程序示例_单片机精华程序代码示例:DS1302时钟程序
  8. gulp + webpack 构建多页面前端项目 1
  9. All in Linux:一个算法工程师的IDE断奶之路
  10. CSS 单词间距 word-spacing属性
  11. 2017.4.24 聪明的质检员 思考记录
  12. 我是特斯拉车主:以前很风光,现在被鄙视
  13. 稳定服务器头像,WordPress 修复 Gravatar 头像稳定服务器地址
  14. Ubuntu: 使用U盘拷贝文件
  15. Java常见设计模式
  16. Extraneous non-emits event listeners (onJump) were passed to component but could not be automaticall
  17. 温度报警器c语言课程设计,综合电子设计课程设计实验报告-可调温度报警器.doc...
  18. c语言自动取数函数,C语言中取随机数的函数
  19. 【SAP Abap】关于销售凭证VBKD业务数据表的使用与注意事项
  20. 说故我在-跟老友记练口语

热门文章

  1. OCR文本检测模型—SegLink
  2. Java 秒数分钟转小时分钟
  3. 基于asp.net企业门户网站设计
  4. 从Maxwell方程组到波动方程
  5. AWS上配置Cisco ASAv AnyConnect
  6. AnyConnect win10安装
  7. 社保只交15年就行?国家白给的羊毛怎么能不薅?
  8. eclipse自动排版快捷键 按了没有用 的解决办法
  9. linux socket结构,Linux C socket编程中几个常见的结构体定义
  10. PRML学习笔记--似然函数到底是什么