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++代码:

 #include <fstream>
#include <iostream>
#include <cstdio>
#include <cmath> using namespace std; int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
int n,l,r;
double ans,h=sqrt();
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&l,&r);
if((l&)&&(r&))
ans=h+l*r-;
else
ans=l*r;
printf("Scenario #%d:\n%.2f\n\n",i,ans);
}
return ;
}

python 2.7.3代码:

 import sys

 n=int(raw_input())
n=n+1 for index in range(1,n):
num=sys.stdin.readline()
r=num.split()
r1=int(r[0])
r2=int(r[1])
if r1&1 and r2&1:
ans=r1*r2+0.41
else:
ans=r1*r2
print 'Scenario #%d:\n%.2f\n'%(index,ans)

zoj1037-Gridland的更多相关文章

  1. poj 1450 Gridland

    Gridland Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  2. Gridland(规律)

    Gridland Time Limit: 2 Seconds      Memory Limit: 65536 KB BackgroundFor years, computer scientists ...

  3. HDU1046:Gridland

    Problem Description For years, computer scientists have been trying to find efficient solutions to d ...

  4. TJU Problem 1015 Gridland

    最重要的是找规律. 下面是引用 http://blog.sina.com.cn/s/blog_4dc813b20100snyv.html 的讲解: 做这题时,千万不要被那个图给吓着了,其实这题就是道简 ...

  5. ZOJ 1037 Gridland

    原题链接 题目大意:给出一个格子图,求走完所有节点的最短路径距离. 解法:简单啊,如果都是奇数,可以走一次斜边,其他情况就是长*宽. 参考代码: #include <stdio.h> in ...

  6. zju 1037 Gridland(找规律,水题)

    题目链接 多写几个案例,根据数据的奇偶性,就能找到规律了 #include<stdio.h> int main() { int t,n,m; double ans; scanf(" ...

  7. HDU 1046 - Gridland

    果然是数学题 #include <iostream> #include <cstdio> #include <cmath> using namespace std; ...

  8. HDU ACM 1046 Gridland 找规律

    分析:给出一个矩阵.问最短从一个点经过全部点以此回到起点的长度是多少.绘图非常好理解.先画3*4.3*3.4*4的点阵图案.试着在上面用最短路走一走,能够发现当矩形点阵的长宽都是奇数时,最短路中必然有 ...

  9. 1046 Gridland

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1046 难点在于读懂题意 题意:输入一个n*m的点阵,间距为1,问你遍历完所有点阵并回到起点的最短路径是多少 ...

  10. POJ1450:Gridland 【杂题】

    题目大意:先给出了TSP的背景,然后给出一个n*m的单位格点的图,图中除边缘上的点与八个方向的点有边连接,距离为欧拉距离,求从左上角出发的TSP 思路:从水题列表中看到的题,但看一开始给出的backg ...

随机推荐

  1. mock的使用二(根据数据模板生成模拟数据)

    Mock.mock( rurl?, rtype?, template|function( options ) ) 根据数据模板生成模拟数据. Mock.mock( template ) 根据数据模板生 ...

  2. pyqtree

    pyqtree module API Documentation Classes class Index The top spatial index to be created by the user ...

  3. 2016-the brave never die

    2016年最后一天工作日了,由于这段时间一直忙于春节项目没时间写点关于2016年的总结,回忆一下,2016年其实还有很多事情没做好,究其原因,感觉是因为对于2016年没有做任何的规划和计划,就凭着一股 ...

  4. eclipse和myeclipse的he user operation is wating问题

    近做了一个MyEclipse项目,但是没开始多久就发现了这个问题:只要文件被修改过,不论多小的修改,保存的时候都会跳出一个框框,里面写着the user operation is wating.... ...

  5. php处理字符串格式的计算表达式

    有时候我们对每一种产品都有一个提成公式,而这个计算提成的公式是以字符串格式存在表中的 当我们用这个计算公式时,他并不像我们写的:$a=2+3*5;这样简单的能计算出结果,而它是个字符串 所以,我们就必 ...

  6. python 生成器和生成器表达式

    1.生成器 生成器的本质就是迭代器 生成器的特点和迭代器一样.取值方式和迭代器一样(__next__(),send():给上一个yield传值) 生成器一般由生成器函数或者生成器表达式来创建 其实就是 ...

  7. appium历史版本下载地址

    https://github.com/appium/appium-desktop/releases

  8. GC之八--GC 触发Full GC执行的情况及应对策略

    目录: GC之一--GC 的算法分析.垃圾收集器.内存分配策略介绍 GC之二--GC日志分析(jdk1.8)整理中 GC之三--GC 触发Full GC执行的情况及应对策略 gc之四--Minor G ...

  9. Win10 TensorFlow(gpu)安装详解

    Win10 TensorFlow(gpu)安装详解 写在前面:TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理.Tensor(张量)意味着 ...

  10. lamp与lnmp的选择

    lnmp和lamp业务上的不同 由于二者仅仅是区别在于web的选择,nginx更高效,占用资源更少,详情区别查看LNMP环境应用实践 lnmp和lamp安装上的不同 生产环境中,可能会遇到lamp架构 ...