[洛谷] P2689 东南西北

求个曼哈顿距离然后贪心

//#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;

const int MAXN = 1e6 + 10;

int arr[MAXN][2], bx, by, ex, ey, step = 0, n;

bool judge(int tx, int ty, int x, int y)
{
    double tdis = sqrt((tx - ex) * (tx - ex) +(ty - ey) * (ty - ey));
    double rdis = sqrt((x - ex) * (x - ex) +(y - ey) * (y - ey));
    if(tdis < rdis)
        return true;
    return false;
}

int main()
{
    //ios::sync_with_stdio(false);

    //cin.tie(0);     cout.tie(0);

    cin>>bx>>by>>ex>>ey>>n;

    char c;

    for(int i = 0; i < n; i++)
    {
        cin>>c;
        switch(c)
        {
            case 'E':
                arr[i][0] = 1, arr[i][1] = 0;
            break;
            case 'S':
                arr[i][0] = 0, arr[i][1] = -1;
            break;
            case 'W':
                arr[i][0] = -1, arr[i][1] = 0;
            break;
            case 'N':
                arr[i][0] = 0, arr[i][1] = 1;
            break;
        }
    }

    int tx, ty;

    for(int i = 0; i < n; i++)
    {
        tx = bx + arr[i][0], ty = by + arr[i][1];
        if(judge(tx, ty, bx, by))
            bx = tx, by = ty, step++;
        if(bx == ex && by == ey)
        {
            cout<<step<<endl;
            goto l1;
        }
    }

    cout<<"-1"<<endl;

    l1:

    return 0;
}
原文地址:https://www.cnblogs.com/zeolim/p/12270442.html