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个旗杆,每根旗杆被分成单位长度的小节.旗杆的长度等于它被分成的小节的数目.每根旗杆上会挂一些帆,每张帆正好占据旗杆上的一个小节.在一根旗杆上的帆可以任意 ...
随机推荐
- JsonCpp在vs中使用
Jsoncpp是c++生成和解析Json数据的跨平台开源库.下面简介如何在vs中使用. 1.官网下载.https://sourceforge.net/projects/jsoncpp/解压文件得到js ...
- 在java程序中使用JDBC连接mysql数据库
在java程序中我们时常会用到数据库中的数据或操作数据库中的数据,如果java程序没有和我们得数据库连接,就不能实现在java程序中直接操作数据库.使用jdbc就能将java程序和数据库连起来,此时我 ...
- Java中Date类型如何向前向后滚动时间,( 附工具类)
Java中的Date类型向前向后滚动时间(附工具类) 废话不多说,先看工具类: import java.text.SimpleDateFormat; import java.util.Calendar ...
- 解决h5底部输入框在ios被软键盘顶飞 软键盘消失还下不来
好吧,其实不是顶飞,准确点说应该是h5页面fiexed定位在底部的输入框在ios软键盘弹起的时候软键盘跟输入框有时会有一段悬空的距离,无法紧贴.在安卓机子上则没有这样的情况. 解决方法是通过h5的sc ...
- python反射怎么用
反射: 通过字符串的形式对 对象 进行增删改查 setattr 设置某个属性的值 class A(object): def __init__(self): self.name = "sath ...
- vim 配色方案
1. 自己电脑上的vim 注释很难看清,又不想取消高亮.原来显示: 在 if has("syntax") syntax onendif 语句下面追加一句: colorscheme ...
- linux 查看内置命令
使用: man shell builtins 查找结果如下:
- BZOJ1800_fly飞行棋_KEY
题目传送门 看数据范围,N<=20! 你没看错,搜索都能过. O(N^2)的做法,就是先求出有几对点之间的距离为圆周长的一半. 然后求C(N,2)即可. code: /************* ...
- [数据结构]_[C/C++]_[链表的最佳创建方式]
场景 1.链表在C/C++里使用非常频繁, 因为它非常使用, 可作为天然的可变数组. push到末尾时对前面的链表项不影响. 反观C数组和std::vector, 一个是静态大小, 一个是增加多了会对 ...
- ORB-SLAM(十二)优化
ORB-SLAM中优化使用g2o库,先复习一下g2o的用法,上类图 其中SparseOptimizer就是我们需要维护的优化求解器,他是一个优化图,也是一个超图(包含若干顶点和一元二元多元边),怎样定 ...