题目描述

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_yf​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_yb​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.

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_xf​x​​ and f_yf​y​​, and the third line contains b_xb​x​​

and b_yb​y​​ (0 \leq f_x, f_y, b_x, b_y \leq 10000≤f​x​​,f​y​​,b​x​​,b​y​​≤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.

输入输出样例

输入样例#1:

2 7
3 0
5 0
NN
NWWWWWN
输出样例#1:

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的更多相关文章

  1. P3133 [USACO16JAN]无线电联系Radio Contact

    题目描述 Farmer John has lost his favorite cow bell, and Bessie the cow has agreed to help him find it! ...

  2. 洛谷 P3133 [USACO16JAN]Radio Contact G

    题目传送门 解题思路: f[i][j]表示FJ走了i步,Bessie走了j步的最小消耗值.方程比较好推. 横纵坐标要搞清楚,因为这东西WA了半小时. AC代码: #include<iostrea ...

  3. 洛谷 P3131 [USACO16JAN]子共七Subsequences Summing to Sevens

    P3131 [USACO16JAN]子共七Subsequences Summing to Sevens 题目描述 Farmer John's NN cows are standing in a row ...

  4. 2018.08.17 洛谷P3135 [USACO16JAN]堡哞(前缀和处理)

    传送门 有趣的前缀和. 数据范围中的n≤200" role="presentation" style="position: relative;"> ...

  5. 「BZOJ4510」「Usaco2016 Jan」Radio Contact 解题报告

    无线电联系 Radio Contact 题目描述 Farmer John has lost his favorite cow bell, and Bessie the cow has agreed t ...

  6. DP【洛谷P3135】[USACO16JAN]堡哞Fort Moo

    [洛谷P3135][USACO16JAN]堡哞Fort Moo Bessie和她的朋友Elsie正在建筑一个堡垒,与任何一个好的堡垒一样,这个需要一个强固的框架.Bessie想造一个轮廓是1m宽的空心 ...

  7. 【题解】洛谷P4391 [BOI2009] Radio Transmission(KMP)

    洛谷P4391:https://www.luogu.org/problemnew/show/P4391 思路 对于给定的字符串 运用KMP思想 设P[x]为前x个字符前缀和后缀相同的最长长度 则对于题 ...

  8. 洛谷P1991 无线通讯网

    P1991 无线通讯网 170通过 539提交 题目提供者洛谷OnlineJudge 标签图论 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 怎么又炸了 为啥一直40!求解! UKE:inv ...

  9. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

随机推荐

  1. MyBatis中动态SQL元素的使用

    掌握MyBatis中动态SQL元素的使用 if choose(when,otherwise) trim where set foreach <SQL>和<include> 在应 ...

  2. BA-siemens-insight时间表设置

    时间表问题汇总: 如果遇到这种问题,显示"unable to locate databse object",就使用database transfer上传一边所有的模块信息,然后在操 ...

  3. 工具-常用VS插件

    工欲善其事,必先利其器,没有好的工具,怎么能高效的开发出高质量的代码呢?本文为各ASP.NET 开发者介绍一些高效实用的工具,涉及SQL 管理,VS插件,内存管理,诊断工具等,涉及开发过程的各个环节, ...

  4. byte类型取值范围以及溢出具体解释

    例1: public class test { public static void main(String[] args) { byte a = 127 ; a = (byte)(a+3) ; Sy ...

  5. HDU 2874 LCA离线算法 tarjan算法

    给出N个点,M条边.Q次询问 Q次询问每两点之间的最短距离 典型LCA 问题   Marjan算法解 #include "stdio.h" #include "strin ...

  6. Linux体验之旅(一)——制作U启,安装rhel-server-6.3

    U启制作: 双击UltraISO: 点击文件→打开: 选择rhel-server6.3 点击启动→选择写入硬盘映像 最后选择格式化优盘→写入→完毕 注意:启动盘制作完毕后一定记得将rhel-serve ...

  7. [jzoj 3175] 数树数 解题报告 (树链剖分)

    interlinkage: https://jzoj.net/senior/#main/show/3175 description: 给定一棵N 个节点的树,标号从1~N.每个点有一个权值.要求维护两 ...

  8. 枚举所有排列-STL中的next_permutation

    枚举排列的常见方法有两种 一种是递归枚举 另一种是STL中的next_permutation //枚举所有排列的另一种方法就是从字典序最小排列开始,不停的调用"求下一个排列"的过程 ...

  9. 【转】如何使用windows的计划任务?计划任务?

    我们经常有一些程序想要过了几小时来运行:比如定时关机 或者说希望能够每天的几点执行一个什么程序: 这些所有的操作都需要用到windows的任务计划:或者叫计划任务:反正都一样 下面小编将指导大家创建一 ...

  10. layer-list

    <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android=" ...