Description
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).

Sample Input
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1

题目意思:

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

解题思路:

只有当顺着风的方向可以使当前位置到目的地距离减小,否则就不动,如果在中间某一时刻到目的地了就输出当时的秒数,如果到最后都没到就是到不了了。

上代码:

#include<stdio.h>
#include<string.h>
int main()
{
int t,sx,sy,ex,ey,i;
char s[];
scanf("%d%d%d%d%d",&t,&sx,&sy,&ex,&ey);
getchar();
gets(s);
for(i=;i<t;i++)
{
if(s[i]=='E'&&sx<ex)
sx++;
else if(s[i]=='W'&&sx>ex)
sx--;
else if(s[i]=='S'&&sy>ey)
sy--;
else if(s[i]=='N'&&sy<ey)
sy++;
if(sx==ex&&sy==ey)
{
printf("%d\n",i+);
break;
}
}
if(!(sx==ex&&sy==ey))
printf("-1\n");
return ;
}

Sail的更多相关文章

  1. sail.js学习 - 安装篇

    导言: 最近在学习sails.js(http://sailsjs.org/),因为用的人不多,资料较少,故写些自己的学习过程.因自己也是初学node.js,有问题大家指出. 介绍: sails.js的 ...

  2. Codeforces Round #180 (Div. 2) B. Sail 贪心

    B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...

  3. 与你相遇好幸运,Sail.js定义其他主键

    uuid : { type: 'string', unique: true, required: true, primaryKey: true },

  4. 与你相遇好幸运,Sail.js其他字段查询

    query: function (req, res) {    var par = req.query;    for(var key in par){      var options = {};  ...

  5. 与你相遇好幸运,Sail.js创建.sailsrc文件

    在项目根目录下创建.sailsrc文件 {  "generators": {    "modules": {}  },  "hooks": ...

  6. 与你相遇好幸运,Sail.js新建模型控制器

    sails generate api user  创建了user的controller和models sails generate api user index 创建了user的controller和 ...

  7. sail.js学习 - 一些问题

    问题: 一.数据填充: 在开发环境中,难免要填充一些基础数据用于测试用.现有两种方法 1.在bootstrap.js或者其他启动文件中创建一些数据 2.https://github.com/frost ...

  8. BZOJ.1805.[IOI2007]sail船帆(贪心 线段树)

    BZOJ 洛谷 首先旗杆的顺序没有影响,答案之和在某一高度帆的总数有关.所以先把旗杆按高度排序. 设高度为\(i\)的帆有\(s_i\)个,那么答案是\(\sum\frac{s_i(s_i-1)}{2 ...

  9. BZOJ1805[Ioi2007]Sail船帆——线段树+贪心

    题目描述 让我们来建造一艘新的海盗船.船上有 N个旗杆,每根旗杆被分成单位长度的小节.旗杆的长度等于它被分成的小节的数目.每根旗杆上会挂一些帆,每张帆正好占据旗杆上的一个小节.在一根旗杆上的帆可以任意 ...

随机推荐

  1. Eclipse易卡死

    在用eclipse编辑项目的时候,经常卡死,经过查询知道原来是我的JDK和eclipse版本对应的不好,我们都知道,eclipse的环境需要配置. 当时情况是这样的 2.容易出现卡死或者如图所示的情况 ...

  2. hadoop学习笔记——用python写wordcount程序

    尝试着用3台虚拟机搭建了伪分布式系统,完整的搭建步骤等熟悉了整个分布式框架之后再写,今天写一下用python写wordcount程序(MapReduce任务)的具体步骤. MapReduce任务以来H ...

  3. 『Python基础-4』字符串

    # 『Python基础-4』字符串 目录 1.什么是字符串 2.修改字符串 2.1 修改字符串大小 2.2 合并(拼接)字符串 2.3 使用乘号'*'来实现字符串的叠加效果. 2.4 在字符串中添加空 ...

  4. Go Web 问题集-持续更新

    前端: 导入静态js,css报错,在确保js和css语法编写正确的前提下 GET   错误:          等问题 1.在服务器中运行:静态服务文件路径设置错误 2.本地运行:相对路径设置错误 3 ...

  5. 天津Uber优步司机奖励政策(1月4日~1月10日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. Restify Api 开发经验

    此文已由作者王振华授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 工作期间,一直在用Restify开发或维护大大小小的API系统,现在分享一下一些个人觉得不错的Tips. 充 ...

  7. ElasticSearch-Java-low-level-rest-client官方文档翻译

    人肉翻译,非谷歌机翻,部分地方添加了个人的理解,并做了分割,如有错误请在评论指出.转载请指明原链接,尊重个人劳动成果.        High-Level-Rest-Client基于Low-Level ...

  8. http tcp udp

    HTTP连接 1.HTTP协议即超文本传送协议(Hypertext Transfer Protocol ),是Web联网的基础,也是手机联网常用的协议之一,HTTP协议是建立在TCP协议之上的一种应用 ...

  9. 「日常训练」Ice Cave(Codeforces Round 301 Div.2 C)

    题意与分析(CodeForces 540C) 这题坑惨了我....我和一道经典的bfs题混淆了,这题比那题简单. 那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落.然后求可行解. 但是 ...

  10. php随机类型验证码

    开发使用验证码的意义就是为了区别操作者是人还是机器,防止自动脚本对服务器造成灾难性的攻击 目前有各种各样的验证码种类,譬如:静态字符验证码.算术验证码.拖拽验证码.识别文字或识别物品验证码(高级),下 ...