Problem Description
Frog fell into a maze. This maze is a rectangle containing N rows and M columns. Each grid in this maze contains a number, which is called the magic value. Frog now stays at grid (1, 1), and he wants to go to grid (N, M). For each step, he can go to either the grid right to his current location or the grid below his location. Formally, he can move from grid (x, y) to (x + 1, y) or (x, y +1), if the grid he wants to go exists.
Frog is a perfectionist, so he'd like to find the most
beautiful path. He defines the beauty of a path in the following way.
Let’s denote the magic values along a path from (1, 1) to (n, m) as A1,A2,…AN+M−1, and Aavg is the average value of all Ai. The beauty of the path is (N+M–1) multiplies the variance of the values:(N+M−1)∑N+M−1i=1(Ai−Aavg)2
In
Frog's opinion, the smaller, the better. A path with smaller beauty
value is more beautiful. He asks you to help him find the most beautiful
path.
 
Input
The first line of input contains a number T indicating the number of test cases (T≤50).
Each test case starts with a line containing two integers N and M (1≤N,M≤30). Each of the next N lines contains M non-negative integers, indicating the magic values. The magic values are no greater than 30.
 
Output
For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from 1. Y is the minimum beauty value.
 
Sample Input
1
2 2
1 2
3 4
 
Sample Output
Case #1: 14
 
 
题目大意:在一个数字方格中,寻找一条从左上角到右下角的路径,使得路径上的数字方差最小。
题目分析:这是2015年合肥网络赛的一道比较简单的题目,赛时没有做出来,惭愧!这道题的一种解法是这样的,枚举路径上的数字和,使其变成数塔问题,递推求解。
 
代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std; double dp[35][35];
int mp[35][35],n,m; double DP(int eva)
{
double k=n+m-1.0;
dp[n-1][m-1]=(mp[n-1][m-1]-eva/k)*(mp[n-1][m-1]-eva/k);
for(int i=n-2;i>=0;--i)
dp[i][m-1]=(mp[i][m-1]-eva/k)*(mp[i][m-1]-eva/k)+dp[i+1][m-1];
for(int i=m-2;i>=0;--i)
dp[n-1][i]=(mp[n-1][i]-eva/k)*(mp[n-1][i]-eva/k)+dp[n-1][i+1];
for(int i=n-2;i>=0;--i)
for(int j=m-2;j>=0;--j)
dp[i][j]=(mp[i][j]-eva/k)*(mp[i][j]-eva/k)+min(dp[i+1][j],dp[i][j+1]);
return (n+m-1)*dp[0][0];
} int main()
{
int T,cas=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
scanf("%d",&mp[i][j]); double ans=1e10;
for(int i=0;i<=1770;++i)
ans=min(ans,DP(i));
printf("Case #%d: %.0lf\n",++cas,ans);
}
return 0;
}

  

最新文章

  1. 【初码干货】使用阿里云对Web开发中的资源文件进行CDN加速的深入研究和实践
  2. Hprose question
  3. FZU-2075 Substring(后缀数组)
  4. nginx之location
  5. ROS 使用自带和usb摄像头获取图像
  6. repeat语句
  7. EasyUI 添加tab页(iframe方式)(转)
  8. [Editor]Unity Editor类常用方法
  9. auto_ptr的使用原则
  10. 嵌入式 详解udev
  11. UIButton的imageEdgeInsets 和 titleEdgeInsets
  12. Binary search for the first element greater than target
  13. Atom远程连接服务器报错服务器版本和客户端版本不一致
  14. Xcode4.4中,代码无法高亮、无法自动补全
  15. hive函数总结-日期函数
  16. WeQuant交易策略—网格交易
  17. Git 入门篇
  18. dede二级导航
  19. C#下载Url文件到本地
  20. C#-方法(八)

热门文章

  1. Tomcat安装(linux系统)
  2. MAVEN项目(仓库中没有jar包)
  3. [World Wind学习]21.影像切割
  4. Flask系列(八)flask-session组件
  5. java基础语法this关键字
  6. Ubuntu16.04 下创建新用户并赋予sudo权限
  7. appscan 9.0.3.10 版本下载
  8. FTRL与Online Optimization
  9. MySQL connector c++使用笔记
  10. Linux /python --- zipinfo命令