Sail
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的更多相关文章
- sail.js学习 - 安装篇
导言: 最近在学习sails.js(http://sailsjs.org/),因为用的人不多,资料较少,故写些自己的学习过程.因自己也是初学node.js,有问题大家指出. 介绍: sails.js的 ...
- Codeforces Round #180 (Div. 2) B. Sail 贪心
B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...
- 与你相遇好幸运,Sail.js定义其他主键
uuid : { type: 'string', unique: true, required: true, primaryKey: true },
- 与你相遇好幸运,Sail.js其他字段查询
query: function (req, res) { var par = req.query; for(var key in par){ var options = {}; ...
- 与你相遇好幸运,Sail.js创建.sailsrc文件
在项目根目录下创建.sailsrc文件 { "generators": { "modules": {} }, "hooks": ...
- 与你相遇好幸运,Sail.js新建模型控制器
sails generate api user 创建了user的controller和models sails generate api user index 创建了user的controller和 ...
- sail.js学习 - 一些问题
问题: 一.数据填充: 在开发环境中,难免要填充一些基础数据用于测试用.现有两种方法 1.在bootstrap.js或者其他启动文件中创建一些数据 2.https://github.com/frost ...
- BZOJ.1805.[IOI2007]sail船帆(贪心 线段树)
BZOJ 洛谷 首先旗杆的顺序没有影响,答案之和在某一高度帆的总数有关.所以先把旗杆按高度排序. 设高度为\(i\)的帆有\(s_i\)个,那么答案是\(\sum\frac{s_i(s_i-1)}{2 ...
- BZOJ1805[Ioi2007]Sail船帆——线段树+贪心
题目描述 让我们来建造一艘新的海盗船.船上有 N个旗杆,每根旗杆被分成单位长度的小节.旗杆的长度等于它被分成的小节的数目.每根旗杆上会挂一些帆,每张帆正好占据旗杆上的一个小节.在一根旗杆上的帆可以任意 ...
随机推荐
- 个人对js面向对象和封装插件的用法
做了一段时间的前端了,给自己写代码也总结出来了一点小小的思路,就来分享一下自己的意见和建议了. 面向对象和插件封装其实说到底都是面向对象的思想,但是插件一般是你要用的时候就new 调用出来的.就说一下 ...
- 学生管理系统增删查基本操作(dom4j/sax技术)
基本代码: student.xml <?xml version="1.0" encoding="UTF-8"?><student> &l ...
- Python入门 —— 06语音识别
Python 语音 实现语音操控的原理 语音操控分为语音识别和语音朗读两部分 我们使用speech模块实现语音模块(python 2.7) SAPI是微软Speech API , 是微软公司推出的语音 ...
- 「PHP」观察者模式模式
引言 所属:行为型模式,常用设计模式之一 学习资料: <大话设计模式>程杰 模式概述 观察者模式定义了一种一对多的依赖关系,让多个观察者对象监听某一个主题对象.这个主题 ...
- PHP删除临时文件
/** * 下载后直接删除临时文件 */ public function deldir($dir) { $dh=opendir($dir); whil ...
- Python学习笔记五:字符串常用操作,字典,三级菜单实例
字符串常用操作 7月19日,7月20日 ,7月22日,7月29日,8月29日,2月29日 首字母大写:a_str.capitalize() 统计字符串个数:a_str.count(“x”) 输出字符, ...
- (杭电2053)A + B Again(转换说明符)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): ...
- 基于FDC2214的手势识别
基于FDC2214的手势识别 1.本次题目来源于2018年全国电子设计大赛D题,要求实现对石头.剪刀.布以及数字12345的识别:同时在上述基础上实现对手势的学习. 2.硬件实现: 硬件主要采用STM ...
- 16 pep8 编码规范
pep8 编码规范 Python Enhancement Proposals :python改进方案 https://www.python.org/dev/peps/ 1. 每级缩进用4个空格. 括号 ...
- ASP.NET MVC 微信JS-SDK认证
layout: post title: ASP.NET MVC 微信JS-SDK认证 category: .net date: 2016-11-01 00:00:00 tags: .net javas ...