zoukankan      html  css  js  c++  java
  • [洛谷] 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;
    }
  • 相关阅读:
    精通特征工程
    reduce_mem_usage 降低内存使用 绘制学习率曲线和验证曲线
    正态性检验 Python正态性检验
    pd.melt Pandas 的melt的使用
    pandas dataframe 格式设置 set_option
    常用模块
    第9章 列表生成式、生成器和迭代器
    全栈作业(一)
    第8章 装饰器、模块和包
    第7章 Python 函数
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270442.html
Copyright © 2011-2022 走看看