1. 程式人生 > >298B Sail (思維題)

298B Sail (思維題)

CodeForces - 298B Sail
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).

Examples
Input
5 0 0 1 1
SESNW
Output
4
Input
10 5 3 3 6
NENSWESNEE
Output
-1
Note
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.

In the second sample, they cannot sail to the destination.

  • 題目大意:
    每一個時間有一個風向,E S N W中的一個 分別是向東 ,向南,向北,向西。在每一個時間點你可以選擇停在原點不動,也可以順著風向前進一個單位。問能否在規定時間內從起點到終點,能得話輸出最小的時間,不能的話,輸出-1。
  • 解題思路:
    設 起點 (sx,sy),終點(ex,ey)
    x=ex-sx>0 說明需要往東走,y=ey-sy>0 說明需要向北走。然後只要從頭掃一遍風向,看什麼時候E的個數>=x 並且N得個數>=y 。然後依次考慮其他的三個方向即可。
  • AC程式碼
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int t,sx,sy,ex,ey;
const int maxn=1e5+10;
int S,E,W,N;
int s,e,w,n;
char a[maxn];
int main()
{
	cin>>t>>sx>>sy>>ex>>ey;
	E=ex-sx;
	W=sx-ex;
	N=ey-sy;
	S=sy-ey;
	for(int i=0;i<t;i++)
	{
		cin>>a[i];
		if(a[i]=='E')
			e++;
		if(a[i]=='W')
			w++;
		if(a[i]=='S')
			s++;
		if(a[i]=='N')
			n++;
	}
	if(S>s||W>w||N>n||E>e)
		cout<<"-1"<<endl;
	else
	{
		int time=0;
		int t1=0,t2=0;
		if(E>=W&&N>=S)
		{
			for(int i=0;i<t;i++)
			{
				if(a[i]=='E')
					t1++;
				if(a[i]=='N')
					t2++;
				time++;
				if(t1>=E&&t2>=N)
					break;
			}
		}
		else if(W>=E&&N>=S)
		{
			for(int i=0;i<t;i++)
			{
				if(a[i]=='W')
					t1++;
				if(a[i]=='N')
					t2++;
				time++;
				if(t1>=W&&t2>=N)
					break;
			}
		}
		else if(W>=E&&S>=N)
		{
			for(int i=0;i<t;i++)
			{
				if(a[i]=='W')
					t1++;
				if(a[i]=='S')
					t2++;
				time++;
				if(t1>=W&&t2>=S)
					break;
			}
		}
		else if(E>=W&&S>=N)
		{
			for(int i=0;i<t;i++)
			{
				if(a[i]=='E')
					t1++;
				if(a[i]=='S')
					t2++;
				time++;
				if(t1>=E&&t2>=S)
					break;
			}
		}
		cout<<time<<endl;
	}
	return 0;
}