洛谷 P3133 [USACO16JAN]无线电联系Radio Contact

Posted 一蓑烟雨任生平

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了洛谷 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_yf?x??,f?y??) 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_yb?x??,b?y??) 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.

John和Bessie分别从(fx,fy)和(bx,by)出发,他们通过无线电通讯,单位时间消耗能量大小等于两人距离的平方(但他们同时在出发点的开始时刻的能量不算),为了节约能量,他们尽量靠在一起。单位时间内John和Bessie都可以选择走或不走。问最小使用能量大小。

输入输出格式

输入格式:

 

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

second line contains integers f_xf?x?? and f_yf?y??, and the third line contains b_xb?x??

and b_yb?y?? (0 \leq f_x, f_y, b_x, b_y \leq 10000f?x??,f?y??,b?x??,b?y??1000). 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.

 

输出格式:

 

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
思路:
f[i][j]表示,第1个人走了i步,第2个人走了j步,此时所消耗的最小的能量之和。
状态转移方程就可以很轻易的表示出来:f[i][j]=min(f[i-1][j],min(f[i][j-1],f[i-1][j-1]))。
错因:漏下了这两句话,边界条件。
for(int i=1;i<=m;i++)    f[0][i]=f[0][i-1]+dis[0][i];
for(int i=1;i<=n;i++) f[i][0]=f[i-1][0]+dis[i][0];

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,fx,fy,bx,by;
char s1[1001],s2[1001];
int f[1001][1001],dis[1001][1001];
int h1[1001],h2[1001],z1[1001],z2[1001];
void pre(){
    h1[0]=fx;z1[0]=fy;h2[0]=bx;z2[0]=by;
    for(int i=1;i<=n;i++){
        if(s1[i-1]==W)    h1[i]=h1[i-1]-1,z1[i]=z1[i-1];
        if(s1[i-1]==N)    h1[i]=h1[i-1],z1[i]=z1[i-1]+1;
        if(s1[i-1]==S)    h1[i]=h1[i-1],z1[i]=z1[i-1]-1;
        if(s1[i-1]==E)    h1[i]=h1[i-1]+1,z1[i]=z1[i-1];
    }
    for(int i=1;i<=m;i++){
        if(s2[i-1]==W)    h2[i]=h2[i-1]-1,z2[i]=z2[i-1];
        if(s2[i-1]==N)    h2[i]=h2[i-1],z2[i]=z2[i-1]+1;
        if(s2[i-1]==S)    h2[i]=h2[i-1],z2[i]=z2[i-1]-1;
        if(s2[i-1]==E)    h2[i]=h2[i-1]+1,z2[i]=z2[i-1];
    }    
}
int main(){
    scanf("%d%d",&n,&m);
    scanf("%d%d",&fx,&fy);
    scanf("%d%d",&bx,&by);
    scanf("%s",s1);
    scanf("%s",s2);
    pre();
    memset(f,0x7f,sizeof(f));
    for(int i=0;i<=n;i++)
        for(int j=0;j<=m;j++)
            dis[i][j]=(h1[i]-h2[j])*(h1[i]-h2[j])+(z1[i]-z2[j])*(z1[i]-z2[j]);
    f[0][0]=0;
    for(int i=1;i<=m;i++)    f[0][i]=f[0][i-1]+dis[0][i];
    for(int i=1;i<=n;i++)    f[i][0]=f[i-1][0]+dis[i][0];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            f[i][j]=min(f[i-1][j],min(f[i-1][j-1],f[i][j-1]))+dis[i][j];
    cout<<f[n][m];
}

 

 

以上是关于洛谷 P3133 [USACO16JAN]无线电联系Radio Contact的主要内容,如果未能解决你的问题,请参考以下文章

洛谷P3116 [USACO15JAN]约会时间Meeting Time

洛谷 P2979 [USACO10JAN]奶酪塔Cheese Towers

洛谷——P3119 [USACO15JAN]草鉴定Grass Cownoisseur

洛谷 P1948 [USACO08JAN]电话线Telephone Lines

洛谷P3070 [USACO13JAN]岛游记Island Travels

洛谷 P3040 [USACO12JAN]贝尔分享Bale Share