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.

原题链接:http://codeforces.com/problemset/problem/298/B

题意:一艘帆船要从一点到达另外一个点。每一秒钟都会有东南西北四个中的一种风,你可以选择动或不动,问你到达终点的最短时间。

直接模拟:如果某一时刻的风与你目标方向相同,则走,否则不动。

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
char a[maxn];
int main()
{int n,sx,sy,ex,ey;while(cin>>n>>sx>>sy>>ex>>ey){cin>>a;bool flag=false;for(int i=0;i<n;i++){if(a[i]=='N'&&sy<ey)sy++;else if(a[i]=='S'&&sy>ey)sy--;else if(a[i]=='E'&&sx<ex)sx++;else if(a[i]=='W'&&sx>ex)sx--;if(sx==ex&&sy==ey){flag=true;cout<<i+1<<endl;break;}}if(!flag)cout<<"-1"<<endl;}return 0;
}

Codeforces Round #180 (Div. 2) B. Sail 【模拟】相关推荐

  1. Codeforces Round #552 (Div. 3) E stl模拟 F dp G gcd

    contest链接 https://codeforces.com/contest/1154 E 题解思路 直接哈希模拟删除T了,可以用setsetset和lowerlowerlower_boundbo ...

  2. Codeforces Round #180 (Div. 2) A. Snow Footprints 贪心

    A. Snow Footprints 题目连接: http://www.codeforces.com/contest/298/problem/A Description There is a stra ...

  3. Codeforces Round #176 (Div. 2) D. Shifting(模拟,STLdeque应用)

    题目链接 D. Shifting time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. 模拟 Codeforces Round #249 (Div. 2) C. Cardiogram

    题目地址:http://codeforces.com/contest/435/problem/C 1 /* 2 题意:给一组公式,一组数据,计算得到一系列的坐标点,画出折线图:) 3 模拟题:蛮恶心的 ...

  5. 模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie

    题目传送门 1 /* 2 模拟:这就是一道模拟水题,看到标签是贪心,还以为错了呢 3 题目倒是很长:) 4 */ 5 #include <cstdio> 6 #include <al ...

  6. Codeforces Round #656 (Div. 3) F. Removing Leaves 贪心 + 模拟

    传送门 文章目录 题意: 思路: 题意: 思路: 首先有一个贪心策略就是每次都找一个叶子节点最多的点,让后删掉他的kkk个叶子节点,现在我们就来考虑如何模拟这个过程. 我们整一个vector<s ...

  7. Codeforces Round #249 (Div. 2) (模拟)

    Codeforces Round #249 (Div. 2) (模拟) C. Cardiogram time limit per test 1 second memory limit per test ...

  8. Codeforces Round #743 (Div. 2) D. Xor of 3 模拟 + 构造

    传送门 文章目录 题意: 思路: 题意: 给你一个010101序列aaa,定义一次操作是选择一个[1,n−2][1,n-2][1,n−2]范围内的下表,将ai,ai+1,ai+2a_i,a_{i+1} ...

  9. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

最新文章

  1. numpy使用np.set_printoptions函数抑制numpy数组输出结果使用科学计数法进行显示(suppressing scientific notation in numpy array)
  2. OpenCV与c语言图像融合
  3. outlook异常之:邮件输入模式转换
  4. delete,drop,truncate 区别
  5. 用Mysql创建设备管理信息系统数据库(图解)
  6. php fprintf,PHP fprintf()函数用法讲解
  7. 柱状图设置坐标轴名称_职场老鸟珍藏的柱状图技巧
  8. 杂记-字符串的字节长度
  9. Struts2中ActionContext介绍
  10. 关于Arduino 步进电机Stepper库的一些想法
  11. 浅谈JVM垃圾回收机制
  12. mysql数据库集群版_MySQL数据库集群实战
  13. 详解Haar特征与AdaBoost方法原理
  14. 虚拟打印机可以设置默认保存路径吗
  15. 电话机上面的接头RJ11
  16. 电脑只能上QQ,不能上网浏览网站怎么解决
  17. 【代码质量】嵌入式编程节约内存技巧
  18. 14、Kanzi插件——通过Kanzi Engine插件创建自定义属性类型及其元数据+代码解析
  19. Altium Designer 3D元件库,PCB封装库,极为全面一份足以
  20. 百家号同步公众号的自媒体工具有吗?

热门文章

  1. S2B2C模式流程图
  2. ArcGIS server如何将自己的小地图叠加到Google maps或者Virtual Earth上
  3. 京东新通路,零售价值增量发展的新范式
  4. Echarts的配置与使用
  5. Wiz写Blog? 不会再爱了,全面拥抱Markdown+Pandoc
  6. 全球与中国密封轻触开关市场现状及未来发展趋势
  7. 第一章恶意软件静态分析基础
  8. public,nbsp;private,nbsp;prote…
  9. 教师计算机考试高频题型,教师资格证考试信息技术高频考点精选
  10. crmsh配置pacemaker集群时报错 CIB not supported: validator 'pacemaker-2.5'