无线电联系 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. epoll简介(二)

    一:多路复用的举例 以一个生活中的例子来解释: 假设你在大学中读书,要等待一个朋友(数据)来访(要读),而这个朋友只知道你在A号楼(socket集合),但是不知道你具体住在哪里,于是你们约好了在A号楼 ...

  2. dynamic_cast, static_cast, const_cast, reinterprt_cast浅析

    用法:dynamic_cast < type-id > ( expression ) 说明:Type-id必须是类的指针.类的引用或者void *:如果type-id是指针类型,那么exp ...

  3. springmvc 返回json数据给前台jsp页面展示

    spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json            优点:不需要自己再处理 步骤一:在spring- ...

  4. 【CSS3】3D立方体动画

    关于CSS3的3D立方体动画 知识点: 1.每个元素有独立的坐标系 2.坐标系随当前元素的改变而发生改变 3.立方体由静态transform参数构成 4.通过给容器添加动画使立方体运动 效果图: &l ...

  5. css超出盒子隐藏

    效果如图1-1. 效果图1-1 css代码: white-space: nowrap;overflow: hidden; text-overflow: ellipsis; display: inlin ...

  6. LRJ 3-7

    #define _CRT_SECURE_NO_WARNINGS #include <cstdio> int main() { int T; int m, n; ][]; // 4 < ...

  7. 洛谷P2486 [SDOI2011]染色 题解 树链剖分+线段树

    题目链接:https://www.luogu.org/problem/P2486 首先这是一道树链剖分+线段树的题. 线段树部分和 codedecision P1112 区间连续段 一模一样,所以我们 ...

  8. python基础八之文件操作

    python的文件操作 1,打开文件 编码方式要和文件的编码方式相同! #open('路径','打开方式','指定编码方式') f = open(r'E:\pycharm\学习\day8\test', ...

  9. 【7003】&&【a203】合并多项式

    Time Limit: 3 second Memory Limit: 2 MB 问题描述      求两个一元多项式的和.输入多项式方式为:多项式项数.每项系数和指数,按指数从大到小的顺序输入.输出多 ...

  10. 第3本:Visual Studio程序员箴言

    第3本:Visual Studio程序员箴言 Visual Studio 2010是我经常使用的程序开发工具,也知道VS中有大量的快捷键可以帮助提高效率,可惜就是不愿意记忆,最近在学vim的时候快速把 ...