zoukankan      html  css  js  c++  java
  • 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
    ", Case++, ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    VS2015 出现 .NETSystem.Runtime.Remoting.RemotingException: TCP 错误
    C#学习笔记------参数
    C#简单工厂和抽象类的实例
    css基础1
    html中的div span和frameset框架标签
    关于C#委托的一些学习笔记
    html基础加强2
    HTML基础加强
    利用GDI+在Winfrom绘制验证码
    winfrom如何在listview中添加控件
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4843173.html
Copyright © 2011-2022 走看看