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

题意:给你一个网格,让从一个点出发,访问所有的城市回到起点,规律是如果两个数都是奇数要加0.41;即:a*b-1+sqrt(2.0);否则肯定可以回到起点,直接是a*b;

c代码:

#include<iostream>
#include<algorithm>
using namespace std;
#include<cstdio>
#include<cmath>
int main(){
int T,kase=0;
cin>>T;
while(T--){
int a,b;
double ans;
cin>>a>>b;
if(a&1&&b&1)ans=a*b+0.41;
else ans=(double)a*b;
printf("Scenario #%d:\n%.2lf\n",++kase,ans);
puts("");
}
return 0;
}

  python:

import sys

an=sys.stdin.readline()
t=an.split();
n=int(t[0])
k=0
while n:
n-=1
k+=1
an=sys.stdin.readline()
t=an.split();
a=int(t[0])
b=int(t[1])
if (a%2) and (b%2):
sum=a*b+0.41
else:
sum=a*b
print('Scenario #%d:'%k)
print("%.2f\n"%sum)
#真心无奈了,各种出问题,最终发现python的对齐方式这么重要。。。

  

__author__ = 'cuijunyong'

import math
T = int(raw_input(), 10);
i = 1
while i <= T:
a = raw_input().split();
print "Scenario #%d:" %i;
if((int(a[0]) & 1 == 1) and (int(a[1]) & 1 == 1)):
print "%.2f" %(round((float(a[0])*float(a[1]) + math.sqrt(2) - 1), 2));
else:
print "%.2f" %(round((float(a[0])*float(a[1])), 2));
i += 1;
print ;

  

Gridland(规律)的更多相关文章

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

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

  2. HDU ACM 1046 Gridland 找规律

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

  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. hdu1452 Happy 2004(规律+因子和+积性函数)

    Happy 2004 题意:s为2004^x的因子和,求s%29.     (题于文末) 知识点: 素因子分解:n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en 因子 ...

  6. Codeforces Round #384 (Div. 2) B. Chloe and the sequence(规律题)

    传送门 Description Chloe, the same as Vladik, is a competitive programmer. She didn't have any problems ...

  7. ACM/ICPC 之 DP解有规律的最短路问题(POJ3377)

    //POJ3377 //DP解法-解有规律的最短路问题 //Time:1157Ms Memory:12440K #include<iostream> #include<cstring ...

  8. HDU 5795 A Simple Nim 打表求SG函数的规律

    A Simple Nim Problem Description   Two players take turns picking candies from n heaps,the player wh ...

  9. 在sqlserver中做fibonacci(斐波那契)规律运算

    --利用sqlserver来运算斐波那契规律 --利用事物与存储过程 declare @number intdeclare @A intdeclare @B intdeclare @C int set ...

随机推荐

  1. cocos2d-x中常见的场景切换

    本文转载自:http://www.cnblogs.com/linux-ios/archive/2013/04/09/3010779.html bool HelloWorld::init() { /// ...

  2. web前端开发常用工具

    http://www.gbin1.com/technology/javautilities/20120806-resource-for-front-end-developer/ 冒泡样式 http:/ ...

  3. 论山寨手机与Android联姻 【9】SmartPhone的硬件结构

    如何区别智能手机(SmartPhone)与功能手机(FeaturePhone)? 有一种观点认为,智能手机本质上是功能手机与便携式电脑(Laptop PC)的结合.功能手机的功能受限于制造厂商的预制, ...

  4. 动态规划——数字三角形(递归or递推or记忆化搜索)

    动态规划的核心就是状态和状态转移方程. 对于该题,需要用抽象的方法思考,把当前的位置(i,j)看成一个状态,然后定义状态的指标函数d(i,j)为从格子出发时能得到的最大和(包括格子本身的值). 在这个 ...

  5. Recover a file even if it was not committed but it has to have been added when you use git reset head by mistake.

    git init echo hello >> test.txt git add test.txt Now the blob is created but it is referenced ...

  6. securecrt在linux与windows之间传输文件(转)

    摘自:http://blog.csdn.net/rangf/article/details/6096365 SecureCRT这款SSH客户端软件同时具备了终端仿真器和文件传输功能.比ftp命令方便多 ...

  7. Tkinter类之窗口部件类

    Tkinter类之窗口部件类 Tkinter支持15个核心的窗口部件,这个15个核心窗口部件类列表如下:窗口部件及说明:Button:一个简单的按钮,用来执行一个命令或别的操作.Canvas:组织图形 ...

  8. ASP.NET MVC 中将FormCollection与实体间转换方法【转】

    将Action动作中传递的FormCollection转变成对应的实体,可以使用Controller的TryUpdateModel()方法. 示例如下: [HttpPost] public Actio ...

  9. BestCoder Round #14

    Harry And Physical Teacher Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  10. 图论+dp poj 1112 Team Them Up!

    题目链接: http://poj.org/problem?id=1112 题目大意: 有编号为1~n的n个人,给出每个人认识的人的编号,注意A认识B,B不一定认识A,让你将所有的人分成两组,要求每组的 ...