洛谷 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炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
随机推荐
- cogs 969. [NOIP2006] 数列
969. [NOIP2006] 数列 ★☆ 输入文件:sequenc.in 输出文件:sequenc.out 简单对比时间限制:1 s 内存限制:162 MB 题目描述 给定一个正整数 ...
- android setCookie 免登录
CookieSyncManager.createInstance(getActivity()); CookieManager cookieManager = CookieManager.getInst ...
- apple Swift语言新手教程
Apple Swift编程语言新手教程 文件夹 1 简单介绍 2 Swift入门 3 简单值 4 控制流 5 函数与闭包 6 对象与类 7 枚举与结构 1 ...
- webRequest
chrome.webRequest 描述: 使用 chrome.webRequest API 监控与分析流量,还可以实时地拦截.阻止或修改请求. 可用版本: 从 Chrome 17 开始支持. 权 ...
- ExtJs--16--Ext.override()方法专门用来重写对象的方法
Ext.onReady(function(){ /** * Ext.override()方法专门用来重写对象的方法 */ //定义个类 Ext.define("U",{ //该类的 ...
- nyoj--74--小学生算术(水)
小学生算术 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 很多小学生在学习加法时,发现"进位"特别容易出错.你的任务是计算两个三位数在相加时需要多少 ...
- Oracle 11G R2 RAC中的scan ip 的用途和基本原理
Oracle 11G R2 RAC增加了scan ip功能,在11.2之前,client链接数据库的时候要用vip,假如你的cluster有4个节点,那么客户端的tnsnames.ora中就对应有四个 ...
- input file上传文件
如何使用input[type='file']来上传文件呢? html: //angular<input type="file" (change)="fileChan ...
- PHP substr() 函数截取中文字符串乱码
用PHP substr() 函数截取中文字符串乱码,换PHPmb_substr() 函数即可
- 9.自己实现linux中的tree
运行效果: 代码: #include <stdio.h> #include <unistd.h> #include <string.h> #include < ...