zoj 1037 Gridland (简单)

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

题意:就是给你n*m个点,每个都要遍历一遍,要你怎么走才是最短的,可以走斜对角。

解题思路:因为每一个点都要走,当点的个数为偶数个时,就是n*m,当为奇数个是,最后一个可以走斜对角,最后一个的长度为1.41。注意格式,最后2个换行。

贴出代码:

#include <stdio.h>

int main()
{
    int n, i, m, T;
    float sum;
    scanf("%d", &T);
    for( i = 1; i<=T; i++)
    {
        scanf("%d%d", &n, &m);
        sum = m*n*1.0;
        if(m*n%2 == 0)
            printf("Scenario #%d:\n%.2f\n\n", i, sum);
        else
            printf("Scenario #%d:\n%.2f\n\n", i, sum+0.41);
    }
    return 0;
}

zoj 1037 Gridland (简单)

时间: 2024-08-06 07:50:16

zoj 1037 Gridland (简单)的相关文章

1037: 最简单的计算机

1037: 最简单的计算机 时间限制: 1 Sec  内存限制: 128 MB提交: 187  解决: 134[提交][状态][讨论版] 题目描述 一个名叫是 PigHeadThree 的研究组织设计了一台实验用的计算机,命名为 PpMm.PpMm只能执行简单的六种命令 A,B,C,D,E,F:只有二个内存 M1,M2:三个寄存器 R1,R2,R3.六种命令的含义如下:命令 A:将内存 M1 的数据装到寄存器 R1 中:命令 B:将内存 M2 的数据装到寄存器 R2 中:命令 C:将寄存器 R3

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

E - QS Network - zoj 1586(简单)

题意:在一个叫做QS的星系,他们使用一些特殊的通讯方式,两个人之间通讯需要使用一个网络适配器,但是一个网络适配器只能跟一个人联系,所有它连接几个人就需要及格适配器,而且每个人都有一些不同的偏好,喜欢的适配器的牌子也是不同的,现在让你求出来让QS人之间相互通讯最少需要多少代价? 输入第一行是测试数据组数,下面是一个N,代表N个人,下一样有N个数表示每个人喜欢的适配器的价格,接着一个矩阵,表示两点间通讯(电缆??)的价格. 分析:只要在每条边的权值上加上两个适配器的价格就可以了. *********

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

转载: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

概率DP入门题

一 概率问题的论文 1.算法合集之<信息学竞赛中概率问题求解初探> 2.有关概率和期望问题的研究 3.算法合集之<浅析竞赛中一类数学期望问题的解决方法> 二 入门题目 1.POJ 3744 Scout YYF I (简单题) 题意:一条路上有n个地雷 ,a[i]代表第i个地雷放的位置,求安全走过这段路的概率 分析:若第k个位置有地雷则安全走过这个位置的方案为在第k-1个位置跳两步概率为(1-p) 从反面考虑 已经安全走过了第i-1个雷 则在第i个雷的死掉的概率为 1-p(从走到a[