「BZOJ4510」「Usaco2016 Jan」Radio Contact 解题报告
无线电联系 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 解题报告的更多相关文章
- 「SCOI2014」方伯伯的商场之旅 解题报告
「SCOI2014」方伯伯的商场之旅 我一开始的想法会被两个相同的集合位置去重给搞死,不过应该还是可以写的,讨论起来老麻烦. 可以先钦定在\(1\)号点集合,然后往后调整一部分. 具体一点,通过前缀和 ...
- 「洛谷P2906」[USACO08OPEN]牛的街区Cow Neighborhoods 解题报告
P2906 [USACO08OPEN]牛的街区Cow Neighborhoods 题目描述 Those Who Know About Cows are aware of the way cows gr ...
- 「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 ...
- 「Luogu P3931」SAC E#1 - 一道难题 Tree 解题报告
圆原题面 我环顾四周,发现大佬们的写法都好高端! 比较差劲的我,只能交上一份DFS的题解 思路: DFS(当然了,其他算法也行) 要想切断叶子节点到根节点的连接 就是在叶子节点和根节点之间砍掉一条边 ...
- 「Mobile Testing Summit China 2016」 中国移动互联网测试大会-议题征集
时至北京盛夏,一场由 TesterHome 主办的关于移动互联网测试技术的盛会正在紧锣密鼓的筹备中.只要你关注软件质量,热爱测试,期待学习,都欢迎你加入这次移动测试技术大会中和我们一起分享经验.探讨话 ...
- Git 执行 「fork 出来的仓库」和「最新版本的原仓库」内容同步更新
当我们在 GitHub 上 fork 出一个仓库后,如果原仓库更新了,此时怎样才能保证我们 fork 出来的仓库和原仓库内容一致呢?我们一般关注的是仓库的 master(主干分支)的内容,通过以下步骤 ...
- 【翻译】西川善司「实验做出的游戏图形」「GUILTY GEAR Xrd -SIGN-」中实现的「纯卡通动画的实时3D图形」的秘密,前篇(2)
Lighting和Shading(2)镜面反射的控制和模拟次级表面散射技术 http://www.4gamer.net/games/216/G021678/20140703095/index_2.ht ...
- 「Windows MFC 」「Edit Control」 控件
「Windows MFC 」「Edit Control」 控件
- 「七天自制PHP框架」第二天:模型与数据库
往期回顾:「七天自制PHP框架」第一天:路由与控制器,点击此处 什么是模型? 我们的WEB系统一定会和各种数据打交道,实际开发过程中,往往一个类对应了关系数据库的一张或多张数据表,这里就会出现两个问题 ...
随机推荐
- 「BZOJ3694」「FJ2014集训」最短路
「BZOJ3694」「FJ2014集训」最短路 首先树剖没得说了,这里说一下并查集的做法, 对于一条非树边,它会影响的点就只有u(i),v(i)到lca,对于lca-v的路径上所有点x,都可通过1-t ...
- 实验4 JavaBean创建和使用(计算器)
实验4 JavaBean创建和使用 一.实验目的 1.掌握JavaBean创建和使用方法: 2.领会JavaBean简化页面设计的优势等. 二.实验内容 1.设计诸如以下页面的简单计算器 要求:完成 ...
- oracle函数 ABS(x)
[功能]返回x的绝对值 [参数]x,数字型表达式 [返回]数字 [示例] select abs(100),abs(-100) from dual; sign(x) [功能]返回x的正负值 [参数]x, ...
- 为 Ubuntu 18.04 添加开机自动加载 ntfs分区 功能
注意:Ubuntu终端命令是区分大小写的 1,准备的: ntfs-3g -- 提供ntfs读写支持(一般说来是自带的,若没有,可是使用 sudo apt-get isntall ntfs-3g ...
- H3C PPP MP配置示例二
- Python--day47--mysql索引种类
- dotnet 设计规范 · 结构体定义
X 不要给结构体默认构造函数 默认的C#编译器也不让开发者定义有默认构造的结构体 X 不要定义易变的属性 易变的属性指的是在调用属性返回值的时候返回的是新的实例,易变的属性会有很多的问题. ✓ 需要确 ...
- Linux下FTP的安装和登陆
对于一个经常接触电脑的人来说,FTP无形中出现在我们生活的各个角落.日常生活中的文件上传和下载很多时候就是依靠FTP去实现的. 专业的说,FTP 是File Transfer Protocol(文件传 ...
- Spring Boot笔记之邮件(spring-boot-starter-mail)
Spring Boot环境中发送邮件 pom.xml引入`spring-boot-starter-mail` application.yml配置 163邮箱 QQ邮箱 Gmail邮箱 发送邮件 ser ...
- Git的使用--如何将本地项目上传到Github(两种简单、方便的方法..)
https://blog.csdn.net/u014135752/article/details/79951802 总结:其实只需要进行下面几步就能把本地项目上传到Github 1.在本地创建一个版本 ...