Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 277    Accepted Submission(s): 132


Problem Description
Frog fell into a maze. This maze is a rectangle containing 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 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”. Y is the minimum beauty value.
 

 

Sample Input
1
2 2
1 2
3 4
 

 

Sample Output
Case #1: 14
 

 

Source
/**
    题意:如题
    做法:dp   比赛的时候想到三维,可是我想的是dp[i][j][k] i表示走的第几步 j,k表示坐标
          然后回溯回去,看了题解  由于数据范围不大 座椅可以直接暴力枚举平均值  
          然后dp[i][j] 表示走i,j 的期望
         在计算的时候算的是((n+m-1) * mmap[i][j] - aver) 这样在结尾的时候除以(n+m-1)
         可是防止精度的问题
         
**/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define maxn 35
#define INF 0x7fffffff
int mmap[maxn][maxn];
int dp[maxn][maxn];
int n, m;
long long solve(int x)
{
    memset(dp, 0, sizeof(dp));
    int tt = n + m - 1;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            if(i == 1 && j == 1) {
                dp[i][j] = (tt * mmap[1][1] - x) * (tt * mmap[1][1] - x);
            }
            else if(i == 1) {
                dp[i][j] = dp[i][j - 1] + (tt * mmap[i][j] - x) * (tt * mmap[i][j] - x);
            }
            else if(j == 1) {
                dp[i][j] = dp[i - 1][j] + (tt * mmap[i][j] - x) * (tt * mmap[i][j] - x);
            }
            else {
                dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + (tt * mmap[i][j] - x) * (tt * mmap[i][j] - x);
            }
        }
    }
    return dp[n][m] / tt;
}
int main()
{
    int T;
    scanf("%d", &T);
    int Case = 1;
    while(T--)
    {
        scanf("%d %d", &n, &m);
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                scanf("%d", &mmap[i][j]);
            }
        }
        long long ans = INF;
        for(int i = 1; i <= 2000; i++)
        {
            ans = min(ans, solve(i));
        }
        printf("Case #%d: %lld\n", Case++, ans);
    }
    return 0;
}
View Code

 

相关文章: