无线电联系 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_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_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.

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

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

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

输入输出格式

输入格式:

The first line of input contains \(N\) and \(M\) (\(1 \le N, M \le 1000\)). The second line contains integers \(f_x\) and \(f_y\), and the third line contains \(b_x\) and \(b_y\) (\(0 \le f_x, f_y, b_x, b_y \le 1000\)). The next line contains a string of length \(N\) describing FJ's path, and the final line contains a string of length \(M\) describing Bessie's path. It is guranteed that Farmer John and Bessie's coordinates are always in the range (\(0 \le x,y \le 1000\)) throughout their journey. Note that East points in the positive x direction and North points in the positive y direction.

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

第二行输入整数\(f_x\)和\(f_y\),第三行输入\(b_x\)和\(b_y\)\((0 \le f_x,f_y,b_x,b_y \le 1000)\)。下一行包含一个长度为N的字符串描述FJ的路径,最后一行包含一个字符串的长度\(M\)描述Bessie的路径。

数据满足\((0 \le x,y \le 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

思路

众所周知,DP是一个好东西,所以这题用DP。

这是样例的示意图

这道题有十分明显的阶段性,而且数据也不大,所以用DP即可。

我们用一个二维数组f[i][j]表示当Farmer John到第i步,Bessie到第j步时所需花费的最小能量。

转移方程容易推得:

f[i][j] = min( f[i][j - 1], f[i - 1][j], f[i - 1][j - 1] ) + dist;

其中dist表示FJ在第i步,Bessie在j步时他们距离的平方\(( tx1 - tx2 ) \times ( tx1 - tx2 ) + ( ty1 - ty2 ) \times ( ty1 - ty2 )\)

这里要注意初始化:

f[i][0] = f[i - 1][0] + dist;
f[0][i] = f[i - 1][0] + dist;
f[0][0] = 0;//因为从初始位置开始的第一步不需要能量

这里我用一个move函数移动坐标(用了引用变量方便一点)

代码

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define MAXN 1005
#define LL long long int N, M;
LL f[MAXN][MAXN];//开long long防止上溢
int fx, fy, bx, by, la, lb;
char a[MAXN], b[MAXN];//移动方向 void move( int &x, int &y, char d ){ //移动坐标
switch( d ){
case 'N': y++; break;
case 'S': y--; break;
case 'W': x--; break;
case 'E': x++; break;
}
} LL dist( int x1, int y1, int x2, int y2 ){ //花费能量
return ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 );
} int main(){
scanf( "%d%d", &N, &M );
scanf( "%d%d%d%d", &fx, &fy, &bx, &by );
scanf( "%s%s", a + 1, b + 1 );
la = strlen( a + 1 ); lb = strlen( b + 1 );
int tx1(fx), ty1(fy), tx2(bx), ty2(by);//FJ、Bessie的坐标
for ( int i = 1; i <= la; ++i ){ // 初始化f[i][0](i != 0)
move( tx1, ty1, a[i] );
f[i][0] = f[i - 1][0] + dist( tx1, ty1, bx, by );
}
for ( int i = 1; i <= lb; ++i ){ //初始化f[0][i](i != 0)
move( tx2, ty2, b[i] );
f[0][i] = f[0][i - 1] + dist( fx, fy, tx2, ty2 );
}
tx1 = fx; ty1 = fy;
for ( int i = 1; i <= la; ++i ){
tx2 = bx; ty2 = by;
move( tx1, ty1, a[i] );
for ( int j = 1; j <= lb; ++j ){
move( tx2, ty2, b[j] );
f[i][j] = min( f[i - 1][j], f[i][j - 1] ); // 上述转移方程
f[i][j] = min( f[i][j], f[i - 1][j - 1] );
f[i][j] += dist( tx1, ty1, tx2, ty2 );
}
}
printf( "%lld", f[la][lb] );
return 0;
}

** 所以: DP是个好东西 **

「BZOJ4510」「Usaco2016 Jan」Radio Contact 解题报告的更多相关文章

  1. 「SCOI2014」方伯伯的商场之旅 解题报告

    「SCOI2014」方伯伯的商场之旅 我一开始的想法会被两个相同的集合位置去重给搞死,不过应该还是可以写的,讨论起来老麻烦. 可以先钦定在\(1\)号点集合,然后往后调整一部分. 具体一点,通过前缀和 ...

  2. 「洛谷P2906」[USACO08OPEN]牛的街区Cow Neighborhoods 解题报告

    P2906 [USACO08OPEN]牛的街区Cow Neighborhoods 题目描述 Those Who Know About Cows are aware of the way cows gr ...

  3. 「BZOJ1722」「Usaco2006 Mar」Milk Team Select产奶比赛 解题报告

    Milk Team Select 产奶比赛 Description Farmer John's N (\(1 \le N \le 500\)) cows are trying to select th ...

  4. 「Luogu P3931」SAC E#1 - 一道难题 Tree 解题报告

    圆原题面 我环顾四周,发现大佬们的写法都好高端! 比较差劲的我,只能交上一份DFS的题解 思路: DFS(当然了,其他算法也行) 要想切断叶子节点到根节点的连接 就是在叶子节点和根节点之间砍掉一条边 ...

  5. 「Mobile Testing Summit China 2016」 中国移动互联网测试大会-议题征集

    时至北京盛夏,一场由 TesterHome 主办的关于移动互联网测试技术的盛会正在紧锣密鼓的筹备中.只要你关注软件质量,热爱测试,期待学习,都欢迎你加入这次移动测试技术大会中和我们一起分享经验.探讨话 ...

  6. Git 执行 「fork 出来的仓库」和「最新版本的原仓库」内容同步更新

    当我们在 GitHub 上 fork 出一个仓库后,如果原仓库更新了,此时怎样才能保证我们 fork 出来的仓库和原仓库内容一致呢?我们一般关注的是仓库的 master(主干分支)的内容,通过以下步骤 ...

  7. 【翻译】西川善司「实验做出的游戏图形」「GUILTY GEAR Xrd -SIGN-」中实现的「纯卡通动画的实时3D图形」的秘密,前篇(2)

    Lighting和Shading(2)镜面反射的控制和模拟次级表面散射技术 http://www.4gamer.net/games/216/G021678/20140703095/index_2.ht ...

  8. 「Windows MFC 」「Edit Control」 控件

    「Windows MFC 」「Edit Control」 控件

  9. 「七天自制PHP框架」第二天:模型与数据库

    往期回顾:「七天自制PHP框架」第一天:路由与控制器,点击此处 什么是模型? 我们的WEB系统一定会和各种数据打交道,实际开发过程中,往往一个类对应了关系数据库的一张或多张数据表,这里就会出现两个问题 ...

随机推荐

  1. @codechef - SONATR@ Sonya and Tree

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定 p 为 0~N-1 的一个排列,并给定一棵 N 个点的树. ...

  2. css3 word-wrap属性

    允许长单词换行到下一行: word-wrap:break-word

  3. pytorch更新

    Pytorch如何更新版本与卸载,使用pip,conda更新卸载Pytorch 2018年05月22日 07:33:52 醉雨轩Y 阅读数 19047   今天我们主要汇总如何使用使用ubuntu,C ...

  4. larave5.6 将Excel文件数据导入数据库代码实例

    <?php namespace App\Admin\Controllers; use App\AdminUser; use Illuminate\Http\Request; use Excel; ...

  5. supersockets服务器配置热更新

    Keywords: 配置,热更新 此功能能够允许你在不重启服务器的前提下更新服务器实例的配置. (仅限1.6.5及其以上版本)

  6. Java JDBC学习实战(二): 管理结果集

    在我的上一篇博客<Java JDBC学习实战(一): JDBC的基本操作>中,简要介绍了jdbc开发的基本流程,并详细介绍了Statement和PreparedStatement的使用:利 ...

  7. 2018-8-10-win10-uwp-读取保存WriteableBitmap-、BitmapImage

    title author date CreateTime categories win10 uwp 读取保存WriteableBitmap .BitmapImage lindexi 2018-08-1 ...

  8. 【39.68%】【CF 714 C】Filya and Homework

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  9. 关于对height:100%的研究

    参考此链接: https://segmentfault.com/a/1190000012707337

  10. java打包上传服务器的一些命令

    Maven下package打包成jar包和war包,都在target目录下 其中War包扔在tomcat的webapps目录下.随tomcat启动自行启动 运行jar包命令. nohup java - ...