CodeForces - 298B Sail
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.

  • 题目大意:
    每一个时间有一个风向,E S N W中的一个 分别是向东 ,向南,向北,向西。在每一个时间点你可以选择停在原点不动,也可以顺着风向前进一个单位。问能否在规定时间内从起点到终点,能得话输出最小的时间,不能的话,输出-1。
  • 解题思路:
    设 起点 (sx,sy),终点(ex,ey)
    x=ex-sx>0 说明需要往东走,y=ey-sy>0 说明需要向北走。然后只要从头扫一遍风向,看什么时候E的个数>=x 并且N得个数>=y 。然后依次考虑其他的三个方向即可。
  • AC代码
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int t,sx,sy,ex,ey;
const int maxn=1e5+10;
int S,E,W,N;
int s,e,w,n;
char a[maxn];
int main()
{cin>>t>>sx>>sy>>ex>>ey;E=ex-sx;W=sx-ex;N=ey-sy;S=sy-ey;for(int i=0;i<t;i++){cin>>a[i];if(a[i]=='E')e++;if(a[i]=='W')w++;if(a[i]=='S')s++;if(a[i]=='N')n++;}if(S>s||W>w||N>n||E>e)cout<<"-1"<<endl;else{int time=0;int t1=0,t2=0;if(E>=W&&N>=S){for(int i=0;i<t;i++){if(a[i]=='E')t1++;if(a[i]=='N')t2++;time++;if(t1>=E&&t2>=N)break;}}else if(W>=E&&N>=S){for(int i=0;i<t;i++){if(a[i]=='W')t1++;if(a[i]=='N')t2++;time++;if(t1>=W&&t2>=N)break;}}else if(W>=E&&S>=N){for(int i=0;i<t;i++){if(a[i]=='W')t1++;if(a[i]=='S')t2++;time++;if(t1>=W&&t2>=S)break;}}else if(E>=W&&S>=N){for(int i=0;i<t;i++){if(a[i]=='E')t1++;if(a[i]=='S')t2++;time++;if(t1>=E&&t2>=S)break;}}cout<<time<<endl;}return 0;
}

CodeForces - 298B Sail (思维题)相关推荐

  1. codeforces 有意思的思维题 1 ~ 15

    codeforces 思维题 1.给定数组,求满足i < j and ai * aj = i + j的数对数量 2.第 i 步向前跳 i 步或后退 1 步 3.给两个点,求正方形的另两个点 4. ...

  2. CodeForces - 1102A(思维题)

    https://vjudge.net/problem/2135388/origin Describe You are given an integer sequence 1,2,-,n. You ha ...

  3. Marvolo Gaunt's Ring ---CodeForces - 855B(思维题)

    题目链接:http://codeforces.com/problemset/problem/855/B Marvolo Gaunt's Ring Professor Dumbledore is hel ...

  4. codeforces 298B Sail

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

  5. Codeforces 298B Sail 题解

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

  6. Codeforces 1077B Disturbed People(思维题)

    Codeforces 1077B Disturbed People(思维题) There is a house with nn flats situated on the main street of ...

  7. CF--思维练习-- CodeForces - 215C - Crosses(思维题)

    ACM思维题训练集合 There is a board with a grid consisting of n rows and m columns, the rows are numbered fr ...

  8. ☆【CodeForces - 764C】Timofey and a tree (思维题,树的性质)

    题干: Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After tha ...

  9. Educational Codeforces Round 67 (Rated for Div. 2)(D思维题 线段树/E树形dp(换根dp) 二次扫描与换根法)

    心得 D写了个假算法被hack了wtcl- E据涛神说是二次扫描与换根法,看了看好像和树形dp差不多 F概率dp G费用流 回头再补 思路来源 马老师 归神 贤神等代码 http://www.mami ...

最新文章

  1. squid 使用 笔记
  2. Code128条形码
  3. java做的模板商城_不吹不黑4个超火Java开源项目,接私活、练手、必备项目
  4. VTK:可视化算法之MarchingCases
  5. LintCode Find the Weak Connected Component in the Directed Graph
  6. iOS多线程: pthread、NSThread
  7. 项目升级到Delphi 2010总结
  8. 论文模型构建的步骤_最全面的财会核心期刊论文写作技巧,核心期刊审稿人带你详解财会论文写作路数...
  9. visio画直线箭头
  10. 一个屌丝程序猿的人生(二)
  11. 裸机运行c语言,裸机_GPIO实验_C语言
  12. Windows每次开机键盘无法使用,需要重新拔插键盘USB接口的解决方案
  13. 奈奎斯特定理和香农定理解释
  14. Java中Arrays.sort()的三种常用用法(自定义排序规则)
  15. 【节能、绿色、高效】数据中心发展正迈入“高算力”时代
  16. JavaBean 技术与 JSP 开发模型练习题
  17. 计算机视觉——基于sift算法的地理信息图像匹配
  18. 老电脑深度linux,国产操作系统DEEPIN超越微软WINDOWS成为老电脑福音,进步神速
  19. 火绒规则 禁止所有软件的安装_【技术宅】火绒安全软件防流氓规则编写及使用小白教程(附成品)...
  20. c语言抽象数据类型的定义,C++问题,定义“有理数”的抽象数据类型

热门文章

  1. ETHPLANET 黑客松报名最后10天!来领福利喽~
  2. Early Convolutions Help Transformers See Better
  3. 电脑重装系统可能会遇到的几个问题
  4. 2021湖南高考成绩分段查询,2021湖南高考成绩查询系统入口
  5. Python数据处理之一:数据读取
  6. shell(9): shell脚本安装chajian
  7. 如何快速有效提升网站的百度排名
  8. 老话讲“深秋不补,过冬吃苦”,五种食物要常吃,安稳度秋
  9. PEG/蛋白Prote/抗体antibody/PAA/SiO2功能化 修饰NaY(Gd/Lu/Nd):Yb,Tm@NaYF4:Yb,Nd上转换纳米颗粒
  10. 拉勾教育-求职技巧总结