Codeforces Round #180 (Div. 2) B. Sail 贪心
B. Sail
题目连接:
http://www.codeforces.com/contest/298/problem/B
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
5 0 0 1 1
SESNW
Sample Output
4
Hint
题意
给你起点sx,sy,终点ex,ey
然后给你n个指令,指令你可以选择遵守也可以选择不遵守
问你最短多久可以从起点到达终点
题解:
贪心,如果我选择之后,能够离终点更近一点,那么我就会选择,否则我就不会选择。
代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
long long sx,sy,ex,ey;
cin>>t>>sx>>sy>>ex>>ey;
if(sx==ex&&sy==ey)return puts("0");
string s;cin>>s;
for(int i=0;i<t;i++)
{
if(s[i]=='E'&&sx<ex)sx+=1;
if(s[i]=='W'&&sx>ex)sx-=1;
if(s[i]=='N'&&sy<ey)sy+=1;
if(s[i]=='S'&&sy>ey)sy-=1;
if(sx==ex&&sy==ey)
{
printf("%d",i+1);
return 0;
}
}
return puts("-1");
}
Codeforces Round #180 (Div. 2) B. Sail 贪心的更多相关文章
- Codeforces Round #180 (Div. 2) D. Fish Weight 贪心
D. Fish Weight 题目连接: http://www.codeforces.com/contest/298/problem/D Description It is known that th ...
- Codeforces Round #180 (Div. 2) A. Snow Footprints 贪心
A. Snow Footprints 题目连接: http://www.codeforces.com/contest/298/problem/A Description There is a stra ...
- Codeforces Round #202 (Div. 1) A. Mafia 贪心
A. Mafia Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...
- Codeforces Round #382 (Div. 2)B. Urbanization 贪心
B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...
- Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp
题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...
- Codeforces Round #192 (Div. 1) A. Purification 贪心
A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...
- Codeforces Round #274 (Div. 1) A. Exams 贪心
A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...
- Codeforces Round #374 (Div. 2) B. Passwords 贪心
B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...
- Codeforces Round #303 (Div. 2) C. Woodcutters 贪心
C. Woodcutters Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...
随机推荐
- Effective java笔记8--序列化
对象的序列化(object serialization)API,它提供了一个框架,用来将对象编码成一个字节流,以及从字节流编码中重新构建对象. 一.谨慎地实现Serializable 要想使一 ...
- EditText 光标不显示问题
android:textCursorDrawable="@drawable/bg_txt_cursor" <?xml version="1.0" enco ...
- 【python】python+behave自动化
留坑,后面再写,先写下request对http请求的校验.
- 中文+django1.9+python3.5一些注意点
1.模板html文件里一定要加 <!DOCTYPE html><meta http-equiv="Content-type" content="text ...
- 判断CString字符串中各位是数字,大小写字母,符号,汉字.xml
pre{ line-height:1; color:#1e1e1e; background-color:#e9e9ff; font-size:16px;}.sysFunc{color:#627cf6; ...
- java 获取当前时间及年月日时分秒
java代码如下: package test; import java.text.SimpleDateFormat; import java.util.Calendar; import java.ut ...
- First step of using junit---------Software Testing Lab 1---2016.03.18
1. Install junit a) Download “junit.jar” b) In eclipse, Windows->Preferences->Java-& ...
- 判断浏览器是IE的几种方式
<script> if(!+[1,])alert("这是ie浏览器"); else alert("这不是ie浏览器"); </script&g ...
- 得到python某个模块的路径
#-*-coding:utf-8-*- # 导入imp模块 import imp # 打印出MySQLdb模块 print imp.find_module("MySQLdb")
- Python 读取文件下所有内容、获取文件名、截取字符、写回文件
# coding=gbk import os import os.path #读取目录下的所有文件,包括嵌套的文件夹 def GetFileList(dir, fileList): newDir ...