HDU-5492


Find a path

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  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+M1, and Aavg is the average value of all Ai. The beauty of the path is (N+M1) multiplies the variance of the values:(N+M1)N+M1i=1(AiAavg)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 (T50).
Each test case starts with a line containing two integers N and M (1N,M30). 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
 

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM