• HDU1046 POJ1450 UVALive2334 ZOJ1037 Gridland【数学计算】


    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 9521   Accepted: 4662

    Description

    Background 

    For years, computer scientists have been trying to find efficient solutions to different computing problems. For some of them efficient algorithms are already available, these are the "easy" problems like sorting, evaluating a polynomial or finding the shortest path in a graph. For the "hard" ones only exponential-time algorithms are known. The traveling-salesman problem belongs to this latter group. Given a set of N towns and roads between these towns, the problem is to compute the shortest path allowing a salesman to visit each of the towns once and only once and return to the starting point. 

    Problem 

    The president of Gridland has hired you to design a program that calculates the length of the shortest traveling-salesman tour for the towns in the country. In Gridland, there is one town at each of the points of a rectangular grid. Roads run from every town in the directions North, Northwest, West, Southwest, South, Southeast, East, and Northeast, provided that there is a neighbouring town in that direction. The distance between neighbouring towns in directions North-South or East-West is 1 unit. The length of the roads is measured by the Euclidean distance. For example, Figure 7 shows 2 * 3-Gridland, i.e., a rectangular grid of dimensions 2 by 3. In 2 * 3-Gridland, the shortest tour has length 6. 
     
    Figure 7: A traveling-salesman tour in 2 ? 3-Gridland.


    Input

    The first line contains the number of scenarios. 

    For each scenario, the grid dimensions m and n will be given as two integer numbers in a single line, separated by a single blank, satisfying 1 < m < 50 and 1 < n < 50.

    Output

    The output for each scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. In the next line, print the length of the shortest traveling-salesman tour rounded to two decimal digits. The output for every scenario ends with a blank line.

    Sample Input

    2
    2 2
    2 3

    Sample Output

    Scenario #1:
    4.00
    
    Scenario #2:
    6.00

    Source



    Regionals 2001 >> Europe - Northwestern


    问题链接:HDU1046 POJ1450 UVALive2334 Gridland。基础训练级的题,用C语言编写。

    这个问题主要是一个数学题,输入整数M和N,构成M*N的点阵,相邻点之间距离为1,要遍历M*N个节点,计算其路径长度。

    需要分为两种情况考虑,一是M和N均为奇数情形;二是M和N有一个玩为偶数的情形。参见下图:




    根据上图可知,M和N均为奇数情形,需要走一个斜线,路径长度为M*N+sqrt(1^2+1^2)-1;M和N有一个为偶数的情形,路径长度为M*N。其中,sqrt(1^2+1^2)=sqrt(2)=1.41。


    AC通过的C语言程序如下:

    /* HDU1046 POJ1450 UVALive2334 ZOJ1037 Gridland */
    
    #include <stdio.h>
    
    int main(void)
    {
        int t, m, n, i;
    
        scanf("%d", &t);
        for(i=1; i<=t; i++) {
            scanf("%d%d", &m, &n);
    
            printf("Scenario #%d:
    ", i);
    
            if(m & 1 == 1 && n & 1 == 1) {  /* 均为奇数:m % 2 == 1 && n % 2 == 1 */
                printf("%d.41
    ",m*n);      /* 0.41 = sqrt(2) */
            } else {
                printf("%d.00
    ",m*n);
            }
            printf("
    ");
        }
    
        return 0;
    }


  • 相关阅读:
    【数据结构】10.java源码关于LinkedHashMap
    【数据结构】9.java源码关于HashTable
    【数据结构】8.java源码关于HashMap
    【数据结构】7.java源码关于LinkedList
    【LEETCODE】69、动态规划,easy,medium级别,题目:198、139、221
    【LEETCODE】68、动态规划,medium级别,题目:95、120、91
    【LEETCODE】67、分治递归,medium&hard级别,题目:215、312
    【数据结构】6.java源码ArrayList
    【LEETCODE】66、字符串分类,hard级别,题目:32,72,76
    mysql查询所有表数据大小
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7564576.html
Copyright © 2020-2023  润新知