洛谷 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炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
随机推荐
- MyBatis-Spring-SqlSessionFactoryBean(转)
SqlSessionFactoryBean 在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建.而在 MyBatis-Spring 中 ...
- mybatis逆向工程不生成Example
mybatis逆向生成映射文件时会生成一大堆example文件,没感觉有啥用,可以手动删除这些多余的东西,使项目变得好看许多 也可以通过配置达到目的: 原配置: <table tableName ...
- $_SERVER 详解
$_SERVER['HTTP_ACCEPT_LANGUAGE']//浏览器语言 $_SERVER['REMOTE_ADDR'] //当前用户 IP . $_SERVER['REMOTE_HOST'] ...
- BA-冷源系统--一次流量控制方案(转载)
空调水变一次流量控制方案 浙大网新快威科技 黄逸林 一.引言 建筑物中央空调系统的冷冻水一次泵,传统上都采用固定转速水泵.空调水的变一次流量控制系统(VPF:Variable-Primary-Flow ...
- 使用Android Studo开发NDK之Gradle的配置(能debug C代码)
配置: 用的版本号是AS1.5(也能够尝试更高版本号). Gradle地址是distributionUrl=https\://services.gradle.org/distributions/gra ...
- 广东工业大学2016校赛决赛-网络赛 1174 Problem F 我是好人4 容斥
Problem F: 我是好人4 Description 众所周知,我是好人!所以不会出太难的题,题意很简单 给你n个数,问你1000000000(含1e9)以内有多少个正整数不是这n个数任意一个的倍 ...
- 41.使用SAX读取XML
main.cpp #include <QtGui> #include <iostream> #include "saxhandler.h" int main ...
- Spring mvc 实现jsonp和json数据类型
在使用springmvc开发rest接口的时候很方便,可以直接使用@ResponseBody注解,直接加在springmvc的控制器类的方法上,springmvc会直接为我们将返回的对 ...
- adplus 抓取dump
工具所在路径 C:\Program Files\Windows Kits\10\Debuggers\x64 cmd窗口切换目录倒adplus所在路径下,输入抓取命令.adplus -hang -p ...
- 微信小程序和App的UI设计有什么异同吗?
大家总是把小程序和App放在一起比,因此我也花时间看了一下小程序的开发指南,尤其是UI部分的设计和原则,今天就拿它和苹果的HIG(Human Interface Guidelines)做个比较,其实两 ...