HDU1046 POJ1450 UVALive2334 ZOJ1037 Gridland【数学计算+水题】

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.  
HDU1046 POJ1450 UVALive2334 ZOJ1037 Gridland【数学计算+水题】_第1张图片  
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

Northwestern Europe 2001


Regionals 2001 >> Europe - Northwestern


问题链接:HDU1046 POJ1450 UVALive2334 Gridland。

问题简述:(略)

问题分析:

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

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

HDU1046 POJ1450 UVALive2334 ZOJ1037 Gridland【数学计算+水题】_第2张图片


HDU1046 POJ1450 UVALive2334 ZOJ1037 Gridland【数学计算+水题】_第3张图片

  根据上图可知,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 

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:\n", i);

        if(m & 1 == 1 && n & 1 == 1) {  /* 均为奇数:m % 2 == 1 && n % 2 == 1 */
            printf("%d.41\n",m*n);      /* 0.41 = sqrt(2) */
        } else {
            printf("%d.00\n",m*n);
        }
        printf("\n");
    }

    return 0;
}


你可能感兴趣的:(#,ICPC-备用二,#,ICPC-UVALive,#,ICPC-POJ,#,ICPC-HDU,#,ICPC-ZOJ,#,ICPC-HDU水题)