Find a path
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2064 Accepted Submission(s): 889
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
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
12 21 23 4
Sample Output
Case #1: 14
Source
Recommend
#include<algorithm>
#include<iostream>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define maxn 40
#define LL long long
#define INF 10000000
const int UB=60*32*2;
using namespace std;
int n,m,G[maxn][maxn];
int dp[maxn][maxn][UB];
/*题目大意:给一个n*m的矩形数字阵,
求从1,1位置到n,m位置其关于路径和的方差(具体公式题目中有)
首先,是对公式的化简。
最后得出的式子是关于路径每个数的平方和,和路径权重和的一个函数。
对此,原本想暴力dp处理的,,即硬是找关系。
参照了大神的思路,,得知控制变量的思维模式,,
即如果和确定了,,如何使每个数平方和最小的一种思想,
所以三维dp基本敲定,给dp[i][j][k]赋予意义,
即:从(1,1)走到(i,j)时路径和为k时其权重平方和的最小值,
试想如果我们能得到这些数据,那么最后只要枚举比较一下最后的dp[n][m][k]即可(套公式比较)
那么构造出dp数组的实现不难想,初始化就是dp[1][1][G[1][1]]=平方
我的做法比较保险,先把边界预处理了,也比较好想。
这题我犯的错在于:上界的选择,
在答案的筛选中初始化最大值有点小。
那么对于这种初始值有没有什么万无一失的办法呢,
观察数据范围,预计其极限值即可,教训教训啊
*/
int Mul(int x) { return x*x; }
int main()
{
int t;scanf("%d",&t);
for(int cnt=1;cnt<=t;cnt++)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&G[i][j]);
memset(dp,0xff,sizeof(dp));
dp[1][1][ G[1][1] ]=Mul(G[1][1]);
for(int i=2;i<=n;i++)
for(int k=G[i][1];k<=UB;k++)
{
int tp=dp[i-1][1][k-G[i][1]];
if(tp!=-1) dp[i][1][k]=dp[i-1][1][k-G[i][1]]+Mul(G[i][1]) ;
}
for(int j=2;j<=m;j++)
for(int k=G[1][j];k<=UB;k++)
{
int tp=dp[1][j-1][k-G[1][j]];
if(tp!=-1) dp[1][j][k]=tp+Mul(G[1][j]);
}
for(int i=2;i<=n;i++)
for(int j=2;j<=m;j++)
{
for(int k=G[i][j];k<=61*(n+m);k++)
{
dp[i][j][k]=UB*UB;
int tp1=dp[i-1][j][k-G[i][j]] , tp2=dp[i][j-1][k-G[i][j]] ;
if(tp1!=-1) dp[i][j][k]=min(tp1+Mul(G[i][j]) , dp[i][j][k] );
if(tp2!=-1) dp[i][j][k]=min(tp2 + Mul(G[i][j]), dp[i][j][k] );
}
}
int ans=UB*UB;
for(int k=1;k<=(n+m)*65;k++)
{
if(dp[n][m][k]==-1||dp[n][m][k]==UB*UB) continue;
ans=min( ans, (n+m-1)*dp[n][m][k]-k*k );
}
printf("Case #%d: %d\n",cnt,ans);
}
return 0;
}