洛谷 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_yfx,fy) 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_ybx,by) 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 10001≤N,M≤1000). The
second line contains integers f_xfx and f_yfy, and the third line contains b_xbx
and b_yby (0 \leq f_x, f_y, b_x, b_y \leq 10000≤fx,fy,bx,by≤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 10000≤x,y≤1000) 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.
输入输出样例
2 7
3 0
5 0
NN
NWWWWWN
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[],s2[];
int f[][],dis[][];
int h1[],h2[],z1[],z2[];
void pre(){
h1[]=fx;z1[]=fy;h2[]=bx;z2[]=by;
for(int i=;i<=n;i++){
if(s1[i-]=='W') h1[i]=h1[i-]-,z1[i]=z1[i-];
if(s1[i-]=='N') h1[i]=h1[i-],z1[i]=z1[i-]+;
if(s1[i-]=='S') h1[i]=h1[i-],z1[i]=z1[i-]-;
if(s1[i-]=='E') h1[i]=h1[i-]+,z1[i]=z1[i-];
}
for(int i=;i<=m;i++){
if(s2[i-]=='W') h2[i]=h2[i-]-,z2[i]=z2[i-];
if(s2[i-]=='N') h2[i]=h2[i-],z2[i]=z2[i-]+;
if(s2[i-]=='S') h2[i]=h2[i-],z2[i]=z2[i-]-;
if(s2[i-]=='E') h2[i]=h2[i-]+,z2[i]=z2[i-];
}
}
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=;i<=n;i++)
for(int j=;j<=m;j++)
dis[i][j]=(h1[i]-h2[j])*(h1[i]-h2[j])+(z1[i]-z2[j])*(z1[i]-z2[j]);
f[][]=;
for(int i=;i<=m;i++) f[][i]=f[][i-]+dis[][i];
for(int i=;i<=n;i++) f[i][]=f[i-][]+dis[i][];
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
f[i][j]=min(f[i-][j],min(f[i-][j-],f[i][j-]))+dis[i][j];
cout<<f[n][m];
}
洛谷 P3133 [USACO16JAN]无线电联系Radio Contact的更多相关文章
- P3133 [USACO16JAN]无线电联系Radio Contact
题目描述 Farmer John has lost his favorite cow bell, and Bessie the cow has agreed to help him find it! ...
- 洛谷 P3133 [USACO16JAN]Radio Contact G
题目传送门 解题思路: f[i][j]表示FJ走了i步,Bessie走了j步的最小消耗值.方程比较好推. 横纵坐标要搞清楚,因为这东西WA了半小时. AC代码: #include<iostrea ...
- 洛谷 P3131 [USACO16JAN]子共七Subsequences Summing to Sevens
P3131 [USACO16JAN]子共七Subsequences Summing to Sevens 题目描述 Farmer John's NN cows are standing in a row ...
- 2018.08.17 洛谷P3135 [USACO16JAN]堡哞(前缀和处理)
传送门 有趣的前缀和. 数据范围中的n≤200" role="presentation" style="position: relative;"> ...
- 「BZOJ4510」「Usaco2016 Jan」Radio Contact 解题报告
无线电联系 Radio Contact 题目描述 Farmer John has lost his favorite cow bell, and Bessie the cow has agreed t ...
- DP【洛谷P3135】[USACO16JAN]堡哞Fort Moo
[洛谷P3135][USACO16JAN]堡哞Fort Moo Bessie和她的朋友Elsie正在建筑一个堡垒,与任何一个好的堡垒一样,这个需要一个强固的框架.Bessie想造一个轮廓是1m宽的空心 ...
- 【题解】洛谷P4391 [BOI2009] Radio Transmission(KMP)
洛谷P4391:https://www.luogu.org/problemnew/show/P4391 思路 对于给定的字符串 运用KMP思想 设P[x]为前x个字符前缀和后缀相同的最长长度 则对于题 ...
- 洛谷P1991 无线通讯网
P1991 无线通讯网 170通过 539提交 题目提供者洛谷OnlineJudge 标签图论 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 怎么又炸了 为啥一直40!求解! UKE:inv ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
随机推荐
- 移动端js手指滑动事件初体验
今天在公司遇到做一个移动端手指滑动的效果,刚開始用了swiper.js插件发现效果不好(文字存在模糊效果).后来查了一些资料,自己手写了一个使用原生js写的滑动效果. 以下直接上代码: <!do ...
- 0x08 总结与练习
1:前面已经搞好了. 2:poj2965 这种开关问题一个点要么点一次要么不点,枚举所有点的方案实行即可 #include<cstdio> #include<iostream> ...
- iOS判断一些权限是否被禁止
iOS中经常会遇到访问相册.相机.麦克疯.蓝牙.以及推送等权限,所以每次我们要使用这些权限是都要记得查看用户是否允许了,如果用户禁止了你的访问权限,你仍然去调取相册或者相机等,那么就会先出现下面的这个 ...
- 2. Add Two Numbers[M]两数相加
题目 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- ROS中安装任意版本的OPENCV
转自:http://tieba.baidu.com/p/5023000237 安装 Opencv 3.2 on Ubuntu 16.04 并创建node测试 step 1: 安装一些package s ...
- Spark Streaming概念学习系列之SparkStreaming的高层抽象DStream
不多说,直接上干货! SparkStreaming的高层抽象DStream 为了便于理解,Spark Streaming提出了DStream抽象,代表连续不断的数据流. DStream 是一个持续的R ...
- php.ini配置文件参数优化
用于生产环境中的PHP需要对其进行优化,让PHP自身发挥更好的性能,除了写好PHP代码,还要配置好php-fpm以及php.ini调优.本文从内存.OPcache.上传.会话以及安全等方面讲解php. ...
- Java NIO(三)通道
概念 通道(Channel)由java.nio.channels包定义的.channel表示IO源与目标打开的连接,类似流,但不能直接访问数据,只能与Buffer进行交互 通道类似流,但又有不同: 既 ...
- 如何使用pgpool failover_stream.sh自己控制选择指定的master节点
集群架构: h236:master h237:standby sync h238:standby sync h239:stadnby async h240:standby async h241:sta ...
- PHP小常识分享
PHP 标记 当解析一个文件时,PHP 会寻找起始和结束标记,也就是 <?php 和 ?>,这告诉 PHP 开始和停止解析二者之间的代码.此种解析方式使得 PHP 可以被嵌入到各种不同的文 ...