Zoj 1037 gridland (simple)

Source: Internet
Author: User
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 ining "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

 

 

Meaning: Give You N * m points, each of which must be traversed. The shortest path is required, and the diagonal corner can be used.

Solution: Because every point has to go, when the number of points is an even number, it is N * M. When the number is an odd number, the last one can go through the diagonal corner, the last length is 1.41. Note the format. The last two line breaks.

 

Paste the Code:

 

#include <stdio.h>int main(){    int n, i, m, T;    float sum;    scanf("%d", &T);    for( i = 1; i<=T; i++)    {        scanf("%d%d", &n, &m);        sum = m*n*1.0;        if(m*n%2 == 0)            printf("Scenario #%d:\n%.2f\n\n", i, sum);        else            printf("Scenario #%d:\n%.2f\n\n", i, sum+0.41);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.