zoj1037-Gridland

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=37

Gridland

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 

 

 

 

题解:题意就是图中每个点都走且仅走一次而且最好回到起点。这么规整的图形,明显是找规律。规律是:

    • 偶数行或偶数列必定存在全部为1的路径把所有点连起来,即m*n;
    • 否则m行n列中存在 m*n-1个1 和 一个 sqrt(2) 的路径连起所有点。

对于后一种情况,给一个图借以揣摩:

C++代码:

 1 #include <fstream>
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cmath>
 5 
 6 using namespace std;
 7 
 8 int main(){
 9     //freopen("D:\input.in","r",stdin);
10     //freopen("D:\output.out","w",stdout);
11     int n,l,r;
12     double ans,h=sqrt(2);
13     scanf("%d",&n);
14     for(int i=1;i<=n;i++){
15         scanf("%d%d",&l,&r);
16         if((l&1)&&(r&1))
17             ans=h+l*r-1;
18         else
19             ans=l*r;
20         printf("Scenario #%d:
%.2f

",i,ans);
21     }
22     return 0;
23 }

python 2.7.3代码:

 1 import sys
 2 
 3 n=int(raw_input())
 4 n=n+1
 5 
 6 for index in range(1,n):
 7     num=sys.stdin.readline()
 8     r=num.split()
 9     r1=int(r[0])
10     r2=int(r[1])
11     if r1&1 and r2&1:
12         ans=r1*r2+0.41
13     else:
14         ans=r1*r2
15     print 'Scenario #%d:
%.2f
'%(index,ans)
原文地址:https://www.cnblogs.com/jiu0821/p/4692533.html