Gridland
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2246 Accepted Submission(s): 1076
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.

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.
2 2 2 2 3
Scenario #1: 4.00 Scenario #2: 6.00
分析:简单题,只要找到规律即可:若m*n是偶数时,一个个走;若m*n是奇数时,开始时一个个走,到最后一个结点时,与
开始那个结点需要一个根号2的距离即可。
注意:空行的输出,每输出一个答案需要再输出一个空行(题目要求)
代码:
#include<stdio.h>
#include<math.h>
int main()
{
int m,n;
int i=1;
int N;
scanf("%d",&N);
while(N--){
scanf("%d%d",&m,&n);
if(m*n%2==0)
printf("Scenario #%d:\n%.2f\n\n",i,(float)m*n);
else
printf("Scenario #%d:\n%.2f\n\n",i,(float)(m*n-1+sqrt(2)));
i++;
}
return 0;
}