码迷,mamicode.com
首页 > 其他好文 > 详细

重新写的Gridland

时间:2015-02-17 15:23:24      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:computer   分析   find   

这道题是我很久以前的时候写的,今天我同学问我这道题怎么做,本想偷懒来着,直接看看,但是,没有任何的线索告诉我从那个角度出发,哎,这也是我不足的地方,下面先给题目,分析在后面。
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.0

技术分享
这道题目的意思是给定一个m行n列的网格,网格的交点各有一个城镇。
每个城镇可以通过八个方向(上、下、左、右、左上、左下、右上、右下)到达另一个城镇。每个城镇之间(上、下、左、右)的距离是单位1。其他方向需要根据勾股定理进行求解。现从网格中的某个城镇开始,访问每个城镇一次(只能访问一次)后返回到起点。求访问完毕后走的路程的最小距离。

先看两张图片
技术分享
技术分享
能发现点什么吗,对,就是从行列的奇偶性出发,如果行或者列是偶数的话,最短距离就是a*b(a、b表示行、列数);如果,两数都是奇数,则最短距离是a*b+sqrt(2)-1,是不是挺简单的。
下面是我的代码

#include<stdio.h>
#include<math.h>
int main()
{
    int n, a, b, i;
    double min;
    while(scanf("%d",&n)==1)
    {

        for(i=0;i<n;i++)
        {
            min=0;
            scanf("%d %d",&a,&b);
            if(a%2==0||b%2==0)
            {
                min+=a*b;
            }
            else
            {
                min+=a*b+sqrt(2)-1;
            }
            printf("Scenario #%d:\n",i+1);
            printf("%.2f\n",min);
            printf("\n");
        }
    }
    return 0;
}

重新写的Gridland

标签:computer   分析   find   

原文地址:http://blog.csdn.net/unusualnow/article/details/43866277

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!