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. LG4848 崂山白花蛇草水

    题意 神犇 Aleph 在 SDOI Round2 前立了一个 flag:如果进了省队,就现场直播喝崂山白花蛇草水.凭借着神犇 Aleph 的实力,他轻松地进了山东省省队,现在便是他履行诺言的时候了. ...

  2. uwsgi配置理解

    最近使用uwsgi 部署了flask应用,出现了不少问题,仔细查阅了一下资料以及翻看了官方文档,就对自己了解到的做个总结~~ 一.http/http-socket/socketuwsgi开头当然少不了 ...

  3. SharePoint中使用Visio Service展示业务数据

    SharePoint中可以通过Visio Service可以在浏览器中查看Visio图,功能部署到系统中,一切安好. 而现实总是很折磨人,使用该功能后,相关使用者随后提出,Visio图能否与我的业务数 ...

  4. 在同一服务器使用git分支建立线上 和 测试 项目

    分支分配文件夹后 sourcetree 创建分支与合并 https://blog.csdn.net/qq_34975710/article/details/74469068 线上分支master 测试 ...

  5. xshell连接kali

    连接出现错误,连接不上去,看到一篇文章可以使用,https://blog.csdn.net/yemaxq/article/details/78171241

  6. 32位centos下安装jdk1.7报Permission denied处理方式

    本文转载自:http://blog.csdn.net/snowwhitewolf/article/details/50287877 环境:centos5.8 32位jdk-7u71-Linux-i58 ...

  7. Linux:数据库服务(Mysql安装及链接、远程链接、genelog)

    yum  search  +  服务:查询服务是否存在: yum  remove  +  服务:卸载服务: 使用 service 操作服务时,服务的名称后要加上字符 d,如启动:service  my ...

  8. JS的事件流的概念

    事件的概念: HTML中与javascript交互是通过事件驱动来实现的,例如鼠标点击事件.页面的滚动事件onscroll等等,可以向文档或者文档中的元素添加事件侦听器来预订事件.想要知道这些事件是在 ...

  9. bash中常用的快捷键

    常用快捷键 ctrl+c 强制终止当前命令 ctrl+l 清屏(clear) ctrl+a 光标移动到命令行行首 ctrl+e 光标移动到命令行行尾 ctrl+u 从光标所在位置删除到行首 ctrl+ ...

  10. mysql数据库忘记密码时如何登录

    1.打开cmd命令提示符,进入上一步mysql.exe所在的文件夹即: 2.输入命令  mysqld --skip-grant-tables  回车,此时就跳过了mysql的用户验证 3.然后直接输入 ...