hdu 1046 Gridland

Gridland

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5137    Accepted Submission(s):
2342

Problem Description

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.

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.

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

Source

Northwestern
Europe 2001

Recommend

We have carefully selected several similar problems for
you:  1038 1033 1036 1124 1129

一道找规律的题,找到规律之后很简单。

题意:给定n*m的格点,问从一个角开始,遍历所有的点一次然后回到起点的最短距离。

附上代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 int main()
 5 {
 6     int m,k,n,j=1;
 7     double s;
 8     scanf("%d",&k);
 9     while(k--)
10     {
11         scanf("%d%d",&m,&n);
12         if(m%2==0||n%2==0)
13             s=m*n;     //若n是偶数,我们可以将m的最后一列留出来和n的最下面一行,为了回来,而剩余的段走S形完全遍历;m是偶数时同理,共n*m。
14         else
15             s=m*n+0.414;     //若都是奇数时,必须走一次斜线,才能变成上面的情况。共n*m-1+sqrt(2)。
16         printf("Scenario #%d:\n",j);
17         printf("%.2lf\n\n",s);
18         j++;
19     }
20     return 0;
21 }
时间: 2024-08-30 10:18:54

hdu 1046 Gridland的相关文章

HDU 1046.Gridland【非搜索,找规律】【8月25】

Gridland Problem Description 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, ev

ZOJ 1037 &amp;&amp; HDU 1046 Gridland (找规律)

链接:click here 题意: 给你 一张图,问你从起点出发,最后回到起点的最短路程 思路: 当n,m有一者能够为偶数时,结果是n*m否者必有一条路需要斜着走,结果为n*m-1+1.41 代码: #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h

HDU 1046 &amp; POJ 1450 Gridland【有趣的数学题】

Gridland Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5697    Accepted Submission(s): 2607 Problem Description For years, computer scientists have been trying to find efficient solutions to

HDU ACM 1046 Gridland 找规律

分析:给出一个矩阵,问最短从一个点经过所有点以此回到起点的长度是多少.画图很好理解,先画3*4.3*3.4*4的点阵图案,试着在上面用最短路走一走,可以发现当矩形点阵的长宽都是奇数时,最短路中必定有一条斜线:而只要长或宽有一个是偶数就可以通过直线来完成最短路经,因此只需判断一下两边的奇偶情况就能求最短路径了. #include<iostream> #include<cmath> using namespace std; int main() { int T,t=0,m,n; cin

(HDU)1046 -- 完数

题目链接:https://vjudge.net/problem/HDU-1406 注意是所有的因子之和,重复出现的因子不要累加(如果模拟了除法的话),另外给出的两个整数要比较大小(坑). 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 #include <algorithm> 6 #include <string>

杭电 HDU ACM 1046 Tempter of the Bone

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 83458    Accepted Submission(s): 22740 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

HDU分类

模拟题, 枚举 1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201 12

HDU——PKU题目分类

HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201