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个旗杆,每根旗杆被分成单位长度的小节.旗杆的长度等于它被分成的小节的数目.每根旗杆上会挂一些帆,每张帆正好占据旗杆上的一个小节.在一根旗杆上的帆可以任意 ...
随机推荐
- Zabbix——设置模板
前提条件: Zabbix版本为4.0 可以正常使用Server 1. 添加H3C网络交换机的CPU和内存模板,并配置自动发现. 注意,需要添加完成后,重新进行搜索后再此进入才有下列选项. 添加CPU的 ...
- MySQL Group Replication 搭建[Multi-Primary Mode]
1. 环境准备 CentOS7.3 percona-server-5.7.18-14 两台服务器ip地址和主机名 10.0.68.206 yhjr-osd-mysql01-uat 10.0.68.20 ...
- Filebeat使用模块收集日志
1.先决条件 在运行Filebeat模块之前: 安装并配置Elastic stack 完成Filebeat的安装 检查Elasticsearch和Kibana是否正在运行,以及Elasticsearc ...
- shell基础知识---与监听服务器长连接端口状态
从未写过脚本我的最近接了俩脚本的需求,就在这分享一下我的我学到基础知识主要就四部分内容 一.变量 变量的定义 string='字符串' string="字符串" num=808st ...
- 使用jQuery实现向上循环滚动效果(超简单)
今天突发奇想 想到的一个新思路 通过使用animate改变外边距达到滚动效果 再用复制节点插入到最后一行达到循环目的 HTML代码如下 <body> <ul style=" ...
- vuejs 预渲染插件 prerender-spa-plugin 生成多页面 -- SEO
前端vue等框架打包的项目一般为SPA应用,而单页面是不利于SEO的,现在的解决方案有两种: 1.SSR服务器渲染 了解服务器渲染请进,这里不做记录. 2.预渲染模式 这比服务端渲染要简单很多 ...
- 浅谈Django的中间件与Python的装饰器
浅谈Django的中间件 与Python的装饰器 一.原理 1.装饰器是Python的一种语法应用,利用闭包的原理去更改一个函数的功能,即让一个函数执行之前先到另外一个函数中执行其他需求语句,在执行该 ...
- .Net Core如何在程序的任意位置使用和注入服务
最近有人问我:我该如何在Startup类之外的地方注入我的服务呢,都写在startup里看着好乱:我该如何在程序的其他地方获取我注入的服务呢: 故我写了这篇博客,文中有不对的地方欢迎指正. 一.如何在 ...
- 【Hive三】Hive理论
1. Hive基础 1. Hive基础 Hive基本概念 引入原因: Hive是什么 Hive数据管理 四种数据模型 Hive内部表和外部表 Hive数据类型 Hive的优化 Map的优化: Redu ...
- reids同步机制和远程连接
RDB同步机制: 开启和关闭:默认情况下是开启了.如果想关闭,那么注释掉redis.conf文件中的所有save选项就可以了. 同步机制: save 900 1:如果在900s以内发生了1次数据更新操 ...