Gridland (初试ACM)

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


【思路】:其实这个思路挺难想,也是看了别人的才知道的。因为要遍历所有的点,要形成一个环。那么就是看斜边个数,后来发           现(可以画3*4,3*3,4*4)矩阵进行尝试,后来发现当全是基数时候路程为m*n-1+squrt(2),而只要有一个偶数就是            m*n。


【coding总结】


Q1:cin后不应该有“endl” 低级错误;


Q2:不知道是不是ACM都要是整形返回,我尝试了void main就无法通过编译。所以还是int main吧,然后变量返回值一定要是        return 0


Q3: cout.setf(ios::showpoint);   这三个合起来用于表示小数点后面位数,(ios::showpoint) 就是就算是整数也要现实小数点

      cout.precision(2);           小数点后精确度为2

      cout.setf(ios::fixed);       用实数表示浮点数。

      要包含头文件#include


Q4:  最后一个太坑,没看清题scenario和#有个空格,一直提醒答案不对。



【Coding】

#include
#include
#include 
using namespace std;
int main(void){
	int i,n,m,j;
	float k;
	cin >> i;
	for(j=0;j>m>>n;
		if(m%2==0||n%2==0) k=(float)m*n;
		else k=m*n-1+sqrtf(2.0);
		cout<<"Scenario#"<


你可能感兴趣的:(Coding)