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. Android.mk用法详解

    一.Android.mk介绍 Android.mk是Android提供的一种makefile文件,用来指定诸如编译生成so库名.引用的头文件目录.需要编译的.c/.cpp文件和.a静态库文件等.要掌握 ...

  2. Java列表分页查询结果导出到CSV文件,导入CSV文件并解析

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  3. Android 编译系统的组成

    Android 和 Linux 的编译系统都是通过 Makefile 工具来组织编译源代码的. Makefile 工具用来解释和执行 Makefile 文件,在 Makefile 文件里定义好工程源代 ...

  4. 写动态库时遇到了symbol lookup error问题

    之前写TLPI上的代码一直是手动进行错误处理,感觉代码冗余量很大,最后还是决定使用书上的tlph_hdr.h,顺便回顾下动态库的创建/使用. 参考很久之前的一篇博客 linux上静态库和动态库的编译和 ...

  5. FPGA时序优化简单窍门

    尽量用硬核,比如硬件乘法器,这个应该都知道. 结构上的pipeline,简言之就是“拆",最极端的情形是拆到源和目的Reg间只有基本的组合逻辑门,比如说~a & b之类...:当然F ...

  6. NKOJ1236 a^b

    题目描述 对于任意两个正整数a,b(0<=a,b<10000)计算a^b各位数字的和的各位数字的和的各位数字的和的各位数字的和. Input 输入有多组数据,每组只有一行,包含两个正整数a ...

  7. [Java][Web]Request 实现转发和 MVC 设计模式

    String data = "aaaaa"; request.setAttribute("data",data); // 将数据存在 request reque ...

  8. Log4j配置详解之log4j.xml

    Log4j配置详解之log4j.xml Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息. ...

  9. common-lang3-version.jar 提供java.lang的扩展功能

    Apache Commons Lang 3.6 API

  10. ALSA声卡10_从零编写之数据传输_学习笔记

    1.引言 (1)应用程序使用声卡的时候,数据流程是:应用程序把数据发送给驱动,驱动把数据发送给硬件声卡,声卡把数据转换成声音数据播放出去. (2)可以使用两种方式发送数据 第一种:app发数据,等驱动 ...