重新写的Gridland

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

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;
}
时间: 2024-08-25 11:10:31

重新写的Gridland的相关文章

TJU Problem 1015 Gridland

最重要的是找规律. 下面是引用 http://blog.sina.com.cn/s/blog_4dc813b20100snyv.html 的讲解: 1 做这题时,千万不要被那个图给吓着了,其实这题就是道简单的数学题. 2 3 首先看当m或n中有一个为2的情况,显然,只需要算周长就OK了.即(m+n-2)*2,考虑到至少其中一个为2,所以答案为2 *m或2*n,亦即m*n.注意这里保证了其中一个数位偶数. 4 当m,n≥3时,考虑至少其中一个为偶数的情况,显然,这种情况很简单,可以得出,结果为m*

Python:hashlib加密模块,flask模块写登录接口

hashlib模块 主要用于加密相关的操作,(比如说加密字符串)在python3的版本里,代替了md5和sha模块,主要提供 sha1, sha224, sha256, sha384, sha512 ,md5 这些加密方式 import  hashlib m = hashlib.md5()   #用md5加密的方式(md5加密后无法解密),创建一个md5的对象 m.update(b"Hello")  #b代表二进制字节bytes,把字符串hello转成字节,然后加密:用b给一个变量转换

三行写出莫比乌斯函数(HDU1695)

莫比乌斯函数是可以在三行内写出来的 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn=1000000; 5 int mu[maxn+10],T; 6 void Mobius(){ 7 for(int d=1,k;d<=maxn;++d) 8 for(mu[1]=1,k=d<<1;k<=maxn;mu[k]=mu[k]-mu[d],k+=d);

在windows 下使用eclipse进行编译和烧写

eclipse IDE是一款开源的前端编程软件,它提供了编写,编译和调试ESP-IDF项目的图形集成开发环境. 首先在https://www.obeo.fr/en/eclipse-download?INSTALLER-WIN64中选择需要的对应位数的eclipse. 然后在http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html下载eclipse运行所需的java环境. 在安装是选择 点

昨天没写今天补上

恩因为今天要考试所以昨天晚上在复习,没来得及写,现在补上. 昨天依旧是循环,被虐了整整一天! 到现在仍然不知道for里的循环体怎么写.... 感到人生无望...(不想说什么了粘题吧) p1032;#include〈iostream〉 using namespace std; int main () { long long a,b=0,c,d,sum=0; cin>>a; while (a>0) { d=a/10; sum++; for(int i=1;i<=sum;i++) { c

.net 自己写的操作Excel 导入导出 类(以供大家参考和自己查阅)

由于现在网页很多都关系到Excel 的操作问题,其中数据的导入导出更是频繁,作为一个菜鸟,收集网上零散的知识,自己整合,写了一个Excel导入到GridView ,以及将GridView的数据导出到EXCEL的类方法,以供参考和方便自己以后查阅. 1 #region 引用部分 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Web; 6 using System.Dat

架构师写给工程师的一封信(很有价值)【转】

下面的邮件是某Architect发给他的Engineering团队的(来源),我觉得挺不错的,翻译过来,我相信我们所有的程序员都能从中学到很多东西.下面是这封邮件-- 每次当我开始做新的东西是我就会很兴奋.就算在软件圈里做了20年以后,每当开始新的旅程里,我都觉得我心中有一些东西不吐不快.这是我们大家一起的旅程.我强烈地相信我们详细规划的过程是很有乐趣的,富有挑战的和丰富多彩的.我想让这个旅程让你们难忘,并且能增添你们所有人的阅历. 这看起来有些唯心主义,不过,我想制订我的工作日程,我们的技术策

一个缓存容灾写的样例

背景 有时我们能够使用缓存进行容灾的处理.场景例如以下:我们当前有一个专门提供各种数据的应用DataCore,该应用开放多个RFC方法供其它应用使用.      我们平时在读写数据时,会在Cache备份一份(为平时DataCore提高响应速度.减少DB.CPU压力所用),当DB挂掉的时候.Cache还能够用来容灾.使用缓存容灾的优点是:性能足够好,坏处是缓存可比数据库成本高多了. 让我们想象得更猛烈些,当DataCore整个挂掉的时候,A.B.C.D方怎么才干安然的执行下去? 我们能够在A.B.

Mysql占用大量写I/O

早上收到zabbix告警,发现某台存放监控数据的数据库主机CPU的IOwait较高,一直持续较长时间. 登录服务器查看磁盘IO发现队列高达90%多,而且经常反复如此 通过iotop查看发现占用io较大的进程是mysql 登录mysql查看show processlist,发现基本上每次io队列较高时都是在insert时,以为是插入语句有问题,于是打开mysql慢查询日志,观察一段时间磁盘io仍然较高,但是发现并没有任何慢查询语句: 查找关于mysql IO问题优化资料,<[转载]sync_bin