无线电联系 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. 从HelloWorld看Knative Serving代码实现

    摘要: Knative Serving以Kubernetes和Istio为基础,支持无服务器应用程序和函数的部署并提供服务.我们从部署一个HelloWorld示例入手来分析Knative Servin ...

  2. HZOJ 回家

    这篇博客大部分在写我的错解……明明很简单的一道题,知道正解后10分钟AC,然而几个错解打的想死…… 错解1 WA40: 鬼知道这40分哪来的……这也是考试最后很无奈地交上去的代码,最后剩20分钟时发现 ...

  3. 系统学习前端之FormData详解

    FormData 1. 概述 FormData类型其实是在XMLHttpRequest 2级定义的,它是为序列化表以及创建与表单格式相同的数据(当然是用于XHR传输)提供便利. 2. 构造函数 创建一 ...

  4. hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  5. 【DCN】端口与地址绑定技术

    端口与地址绑定技术   与AM技术不同之处在于,AM端口下绑定的MAC或IP能够通信,不限制绑定的MAC在其它接口下通信.   开启MAC-CPU学习模式 mac-address-learning c ...

  6. npx cowsay 让动物说话~

      发现个好玩的东东, 忍不住想分享出来, 好可爱, 哈哈哈~~   node环境执行命令:    npm i cowsay -D npx cowsay hello! npx cowsay -f sh ...

  7. ubuntu snmp 安装与配置

    0.说明 关于一个完整的教程,还是那句话,国内的要么不完整,要么就太旧了,而且思路也不清晰,所以这里写一篇完整的给大家分享一下. 虽然对于Linux主机的监控可以通过执行特定的命令来完成,但是相比之后 ...

  8. 2018-8-10-VisualStudio-自定义外部命令

    title author date CreateTime categories VisualStudio 自定义外部命令 lindexi 2018-08-10 19:16:53 +0800 2018- ...

  9. ZR1050

    ZR1050 http://www.zhengruioi.com/problem/1030 题目大意: 给定一棵带点权的树,求所有联通块的点权和的平方的和 \(n \le 10^5\) 题解 首先,关 ...

  10. Linux 内核kobject 缺省属性

    当被创建时, 每个 kobject 被给定一套缺省属性. 这些属性通过 kobj_type 结构来指定. 这个结构, 记住, 看来如此: struct kobj_type { void (*relea ...