题目描述

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];
}
 

最新文章

  1. 利用Microsoft.Practices.Unity的拦截技术,实现.NET中的AOP
  2. 【Mysql】phpMyAdmin安装与配置
  3. 第十章 MySQL 常用函数
  4. C#中一个关于不同窗体间的颜色参数的传递
  5. ArcGIS 10 许可配置
  6. 创建OpenStack外部网络并分配浮动IP
  7. 配置ssh免密码登录——集群学习日记
  8. lower_bound()返回值
  9. Android的AIDL机制
  10. 安装NVIDIA
  11. python全栈开发day53-mysql
  12. [zz]有哪些优秀的科学网站和科研软件推荐给研究生?
  13. inception安装使用
  14. Java:终于找到了在alloy中的JFileChooser中的弹出式菜单不显示文字的解决办法
  15. LeetCode--013--罗马数字转整数(java)
  16. 牛客第二场Dmoney
  17. [py]django模板继承
  18. BZOJ5294 BJOI2018二进制(线段树)
  19. pymysql的使用心得(1)------小细节,注意!
  20. 【LeetCode】Path Sum II 二叉树递归

热门文章

  1. nodejs-配置vs code的插件
  2. Spring Boot 第一个demo
  3. 数据库连接池和connection的理解
  4. [using_microsoft_infopath_2010]Chapter8 使用InfoPath表单web部件
  5. 什么是EL表达式
  6. 一个Python项目的创建架构
  7. LVS(Linux Viretual Server) 负载均衡器 + 后端服务器
  8. js 数据类型判断
  9. ASP版_阿里大于短信API Demo
  10. OCR文字识别软件FineReader系列产品双十一特惠!