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.
就是四个方向走,问问能不能走到终点,而且可以停在某个位置一秒。
一开始打算是模拟,但是一想,这玩意如何模拟啊,画了个图之后,才发现,如果目标点在起始点的西北部,那么给出的字符串里只要有足够的W和N就好了啊,分四种情况。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;const int maxx=1e5+10;
char s[maxx];
int t,sx,sy,ex,ey;int main()
{while(cin>>t>>sx>>sy>>ex>>ey){cin>>s;int xx=ex-sx;int yy=ey-sy;if(xx==0&&yy==0){cout<<0<<endl;continue;}int flag=0;if(xx<=0&&yy<=0){for(int i=0;i<t;i++){if(s[i]=='S'&&yy) yy++;if(s[i]=='W'&&xx) xx++;if(xx==0&&yy==0){flag=1;cout<<i+1<<endl;break;}}}else if(xx<=0&&yy>=0){for(int i=0;i<t;i++){if(s[i]=='N'&&yy) yy--;if(s[i]=='W'&&xx) xx++;if(xx==0&&yy==0){flag=1;cout<<i+1<<endl;break;}}}else if(xx>=0&&yy>=0){for(int i=0;i<t;i++){if(s[i]=='N'&&yy) yy--;if(s[i]=='E'&&xx) xx--;if(xx==0&&yy==0){flag=1;cout<<i+1<<endl;break;}}}else if(xx>=0&&yy<=0){for(int i=0;i<t;i++){if(s[i]=='S'&&yy) yy++;if(s[i]=='E'&&xx) xx--;if(xx==0&&yy==0){flag=1;cout<<i+1<<endl;break;}}}if(!flag) cout<<"-1"<<endl;}
}

努力加油a啊,(o)/~

Sail(CodeForces - 298B )相关推荐

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

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

  2. 【日常训练】Help Far Away Kingdom(Codeforces 99A)

    题意与分析 题意很简单,但是注意到小数可能有一千位,作为一周java选手的我选择了java解决. 这里的分析会归纳一些必要的Java API:(待补) 代码 /** ACM Code => cf ...

  3. 洛谷千题复习计划(一)(Codeforces + AtCoder)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 每天花一个小时简单复习一下我写过的洛谷的题目! 虽然还没有到千题,但是快了(等我复习完这些以后我 lu ...

  4. [题解]RGB Substring (hard version)-前缀和(codeforces 1196D2)

    题目链接:https://codeforces.com/problemset/problem/1196/D2 题意: q 个询问,每个查询将给你一个由 n 个字符组成的字符串s,每个字符都是 &quo ...

  5. Books Queries(codeforces 1066)

    You have got a shelf and want to put some books on it. You are given q queries of three types: L id ...

  6. World of Darkraft(codeforces 138D)

    题意:有一个 n × m 的棋盘,每个点上标记了 L; R; X 中的一个 每次能选择一个没有被攻击过的点 (i; j),从这个点开始发射线,射线形状为: 1. 若字符是 L,向左下角和右上角发,遇到 ...

  7. Memory and Crow(CodeForces 712A)

    题目描述: There are n integers b1, b2, -, bn written in a row. For all i from 1 to n, values ai are defi ...

  8. Circling Round Treasures(codeforces 375c)

    题意:要求在一张网格图上走出一条闭合路径,不得将炸弹包围进去,使围出的总价值减去路径长度最大. /*类似于poj3182的做法,只不过出现了多个点,那么就用状态压缩的方法记录一个集合即可. */ #i ...

  9. 「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)

    题意与分析(CodeForces 540D) 是一道概率dp题. 不过我没把它当dp做... 我就是凭着概率的直觉写的,还好这题不算难. 这题的重点在于考虑概率:他们喜相逢的概率是多少?考虑超几何分布 ...

最新文章

  1. 数字证书及网络加解密原理
  2. 怪不的软件开发这么挣钱,原来是有这么多职位
  3. selenium 文件下载
  4. 【CSDN】Markdown 如何画分界线
  5. 《剑指offer》——04. 二维数组中的查找——暴力法、线性查找——java实现
  6. Python 面向监狱编程,就靠它了
  7. status debug when entering opportunity overview page
  8. 在python是什么意思_python 的 表示什么
  9. jzoj4742-单峰【数学,数论】
  10. .ajax 自定义headers,Ajax设置自定义请求头的两种方法
  11. android八方向手势,Android开发中顺时针逆时针滑动手势的识别算法
  12. 爬虫python技术分享_Python技术分享:爬虫
  13. 畅购商城-添加订单实现(一)
  14. 话说丢帧率系列---帧间隙
  15. 什么是网关,网关的作用是什么
  16. UVA之11462 - Age Sort
  17. [BMZCTF]-WEB_ezphp
  18. ssm时装秀服装在线购物商城管理系统
  19. NKOI 1550 旅馆
  20. PM2.5检测仪设计

热门文章

  1. java猜拳游戏代码_Java实现简单猜拳游戏
  2. XSS跨站脚本(web应用)——XSS跨站脚本防御(三)
  3. 暴力破解(初级)以及弱口令工具的使用
  4. c语言必背数据结构_数据结构(C语言)考试重点必背
  5. FreeCAD v0.19源码编译与VS2017+Qt5环境搭建
  6. 2 数据源配置_SpringBoot 整合Druid与配置数据源监控
  7. linux 拆分文件 多个,linux – 如何拆分文件并保留每个部分的第...
  8. list python 访问 键值对_基础|Python常用知识点汇总(中)
  9. 从0开始的Python学习009参数
  10. DockerONE 干货 深入理解Docker容器和镜像