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个旗杆,每根旗杆被分成单位长度的小节.旗杆的长度等于它被分成的小节的数目.每根旗杆上会挂一些帆,每张帆正好占据旗杆上的一个小节.在一根旗杆上的帆可以任意 ...
随机推荐
- 初识Java——第一章 初识Java
1. 计算机程序: 为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的集合. 2. JAVA相关的技术: 1).安装和运行在本机上的桌面程序 2).通过浏览器访问的面向 ...
- Java分布式锁之数据库方式实现
之前的文章<Java分布式锁实现>中列举了分布式锁的3种实现方式,分别是基于数据库实现,基于缓存实现和基于zookeeper实现.三种实现方式各有可取之处,本篇文章就详细讲解一下Java分 ...
- 初识Pentaho(一)
学习一门语言或工具,首先得知道该工具的用途是什么.Pentaho 的官方定义是一个集数据集成和数据分析于一体的平台.这样的解释还是有点模糊.还是看其有哪些特点吧: ☐可以进行数据集成.谈到数据集成这 ...
- php源码建博客2--实现单入口MVC结构
主要: MVC目录结构 数据库工具类制作 创建公共模型类和公共控制器类 --------------文件结构:-------------------------------------- blog├─ ...
- 帝国CMS如何禁止内容关键字替换ALT和title中的关键词为链接
很多帝国cms用户喜欢使用关键字替换来实现文章自动内链的方法. 为什么要用关键词替换功能呢?这关系到站内优化,下面直接进入正题. 解决办法:打开e/class/functions.php 查找 '/' ...
- MySQL数据库的原理
点进去就是你历经千辛万苦找到的数据库的原理: https://www.cnblogs.com/smallyard/p/5626061.html
- 最新版的stm32f1xx.h文件中取消了u8, u16, u32的类型定义
使用芯片stm32f103zet6和stm32l151c8t6,在移植程序时发现,编译器提示u8未定义: 在Keil MDK 开发环境里,st定义无符号32位整形数据有很多种表示方法: 1 unsig ...
- python网络编程之协程
本节的主题是基于单线程来实现并发,即只用一个主线程(很明显可利用的cpu只有一个)情况下实现并发,为此我们需要先回顾下并发的本质:切换+保存状态 cpu正在运行一个任务,会在两种情况下切走去执行其他的 ...
- C语言实验报告(四)完全数
完全数,又称完美数或者完备数.是一些特殊的自然数.它所有的真因子的和,恰好等于它本身.编程找出1000以内的所有完全数,并输出该数成为完全数的因子. (例如6=1+2+3.按照6,its factor ...
- ACM数据结构-并查集
ACM数据结构-并查集 并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合 ...