zoukankan      html  css  js  c++  java
  • P3133 [USACO16JAN]无线电联系Radio Contact

    题目描述

    Farmer John has lost his favorite cow bell, and Bessie the cow has agreed to help him find it! They both fan out and search the farm along different paths, but stay in contact via radio so they can keep in touch with each-other. Unfortunately, the batteries in their radios are running low, so they want to plan their movements so as to conserve power, by trying to stay always within a short distance apart.

    Farmer John starts at location (f_x, f_yfx,fy ) and plans to follow a path consisting of NN steps, each of which is either 'N' (north), 'E' (east), 'S' (south), or 'W' west. Bessie starts at location (b_x, b_ybx,by ) and follows a similar path consisting of MM steps. Both paths may share points in common. At each time step, Farmer John can either stay put at his current location, or take one step forward along his path, in whichever direction happens to be next (assuming he has not yet reached the final location in his path). Bessie can make a similar choice. At each time step (excluding the first step where they start at their initial locations), their radios consume energy equal to the square of the distance between them.

    Please help FJ and Bessie plan a joint movement strategy that will minimize the total amount of energy consumed up to and including the final step where both of them first reach the final locations on their respective paths.

    FJ失去了他最喜欢的牛铃,而Bessie已经同意帮助他找到它!他们用不同的路径搜索农场,通过无线电保持联系。不幸的是,无线电中的电池电量不足,所以他们设法尽可能保持两者位置的距离最小,以节省电量。

    FJ从位置(fx,fy)开始,并计划遵循由N步骤组成的路径,每个步骤都是“N”(北),“E”(东),“S”(南),或“W”(西)。Bessie从位置(bx,by)开始,并遵循由M步骤组成的类似路径。两个路径可以经过相同的点。在每个时间段,FJ可以保持在他现在的位置,或沿着他的道路前进一步,无论哪个方向恰好在下一个(假设他还没有到达他的路径的最后位置)。Bessie可以做出类似的选择。在每个时间步(不包括从初始位置开始的第一步),他们的无线电消耗的能量等于它们之间距离的平方。

    请帮助FJ和Bessie计划行动策略,最大限度地减少消耗的能量总量。总量包括最终步骤,这时两者首先到达各自路径上的最终位置。

    输入输出格式

    输入格式:

    The first line of input contains NN and MM (1 leq N, M leq 10001N,M1000 ). The

    second line contains integers f_xfx and f_yfy , and the third line contains b_xbx

    and b_yby (0 leq f_x, f_y, b_x, b_y leq 10000fx,fy,bx,by1000 ). The next line contains a

    string of length NN describing FJ's path, and the final line contains a string

    of length MM describing Bessie's path.

    It is guranteed that Farmer John and Bessie's coordinates are always in the

    range (0 leq x,y leq 10000x,y1000 ) throughout their journey. Note that East points in the positive x direction and North points in the positive y direction.

    第一行输入N和M(1≤N,M≤1000)。

    第二行输入整数fx和fy,第三行输入bx和by(0≤fx,fy,bx,≤1000)。下一行包含一个长度为N的字符串描述FJ的路径,最后一行包含一个字符串的长度M描述Bessie的路径。

    数据满足(0≤x,y≤1000)。注意,东方向为正X方向,北方向为正Y方向。

    输出格式:

    Output a single integer specifying the minimum energy FJ and Bessie can use

    during their travels.

    输出一个整数,表示最小能量。

    输入输出样例

    输入样例#1: 复制
    2 7
    3 0
    5 0
    NN
    NWWWWWN
    输出样例#1: 复制
    28

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    #define inf 2147483647
    const ll INF = 0x3f3f3f3f3f3f3f3fll;
    #define ri register int
    template <class T> inline T min(T a, T b, T c)
    {
        return min(min(a, b), c);
    }
    template <class T> inline T max(T a, T b, T c)
    {
        return max(max(a, b), c);
    }
    template <class T> inline T min(T a, T b, T c, T d)
    {
        return min(min(a, b), min(c, d));
    }
    template <class T> inline T max(T a, T b, T c, T d)
    {
        return max(max(a, b), max(c, d));
    }
    #define pi acos(-1)
    #define me(x, y) memset(x, y, sizeof(x));
    #define For(i, a, b) for (int i = a; i <= b; i++)
    #define FFor(i, a, b) for (int i = a; i >= b; i--)
    #define mp make_pair
    #define pb push_back
    const int maxn = 100005;
    // name*******************************
    int n,m;
    struct pos
    {
        int x,y;
    } a[1005],b[1005];
    int f[1005][1005];
    int ax,ay,bx,by;
    char s1[1005],s2[1005];
    // function******************************
    int dis(int i,int j)
    {
        return (a[i].x-b[j].x)*(a[i].x-b[j].x)+(a[i].y-b[j].y)*(a[i].y-b[j].y);
    }
    //***************************************
    int main()
    {
        cin>>n>>m;
        cin>>ax>>ay>>bx>>by;
        a[0].x=ax;
        a[0].y=ay;
        b[0].x=bx;
        b[0].y=by;
        scanf("%s",s1+1);
        scanf("%s",s2+1);
        char c;
        For(i,1,n)
        {
            c=s1[i];
            if(c=='N')
            {
                a[i].x=a[i-1].x;
                a[i].y=a[i-1].y+1;
            }
            if(c=='S')
            {
                a[i].x=a[i-1].x;
                a[i].y=a[i-1].y-1;
            }
            if(c=='E')
            {
                a[i].x=a[i-1].x+1;
                a[i].y=a[i-1].y;
            }
            if(c=='W')
            {
                a[i].x=a[i-1].x-1;
                a[i].y=a[i-1].y;
            }
        }
        For(i,1,m)
        {
            c=s2[i];
            if(c=='N')
            {
                b[i].x=b[i-1].x;
                b[i].y=b[i-1].y+1;
            }
            if(c=='S')
            {
                b[i].x=b[i-1].x;
                b[i].y=b[i-1].y-1;
            }
            if(c=='E')
            {
                b[i].x=b[i-1].x+1;
                b[i].y=b[i-1].y;
            }
            if(c=='W')
            {
                b[i].x=b[i-1].x-1;
                b[i].y=b[i-1].y;
            }
        }
        me(f,127);
        f[0][0]=0;
        for(int i=0; i<=n; i++)
            for(int j=0; j<=m; j++)
            {
                if(i>=1)
                    f[i][j]=min(f[i][j],f[i-1][j]+dis(i,j));
                if(j>=1)
                    f[i][j]=min(f[i][j],f[i][j-1]+dis(i,j));
                if(i>=1&&j>=1)
                    f[i][j]=min(f[i][j],f[i-1][j-1]+dis(i,j));
            }
    
        cout<<f[n][m];
    
        return 0;
    }
  • 相关阅读:
    mysqlnd cannot connect to MySQL 4.1+ using old authentication
    nginx中查看关于php的配置和php-fpm的重启等操作
    nginx中查看关于php的配置和php-fpm的重启等操作
    linux面试题1
    linux笔试
    面试题
    shell脚本实例
    shell脚本
    mysql主从复制
    Discuz!NT静态文件缓存(SQUID)
  • 原文地址:https://www.cnblogs.com/planche/p/8659448.html
Copyright © 2011-2022 走看看