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. MYSQL 查询缓存

    查询缓存: 是指对select 语句的结果进行缓存,当下一次运行同样的select语句时,就可以直接返回数据,跳过解析,执行,优化阶段. 1.查询缓存会跟踪查询涉及的表,如果表发生变化,相关的缓存都会 ...

  2. jQuery学习-事件之绑定事件(五)

    大家应该还记得dispatch方法中有这么一段代码: event = jQuery.event.fix( event ); event的修复是在fix这个方法中的,而在fix中是通过 new jQue ...

  3. java猜数字小游戏

    /* * * 猜数字小游戏 * * 先由系统生成一个2-100之间的随机数字, * * 然后捕获用户从控制台中输入的数字是否与系统生成的随机数字相同, * * 如果相同则统计用户所猜的次数,并给出相应 ...

  4. 8 个优秀的 Linux 图形图像及色彩工具

    8 个优秀的 Linux 图形图像及色彩工具 1. 硬件色彩分析器LPROF LPROF 是一个用于创建设备兼容,如相机.扫描仪.显示器的ICC兼容型材的颜色分析器.这些配置提供跨设备的色彩一致性.他 ...

  5. 打开网页自动弹出QQ临时会话 (打开网站弹出QQ聊天) qq.js文件代

    eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35 ...

  6. poj1922

    Ride to School Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18704   Accepted: 7552 D ...

  7. ssh 应用

    SSH反向连接及Autossh ssh 隧道: http://www.cnblogs.com/robinyjj/archive/2008/11/02/1325018.html This guy wri ...

  8. 高仿精仿微信应用ios源码下载

    微信,超过3亿人使用,能够通过手机网络给好友发送语音.文字消息.表情.图片和视频,还可以分享照片到朋友圈.通过摇一摇.查看附近的人,你可以认识新的朋友.使用扫一扫,你可以扫描二维码.条码.图书和街景. ...

  9. 使用yii中CSecurityManager的一点小技巧

    当我们使用CSecurityManager::encrypt对字符串进行加密, 加密后的字符串是一串乱码(看起来确实像乱码, 具体是什么有待考证), 这不利于我们的下一步操作. 我们可以使用base6 ...

  10. Android SQLite Database Tutorial

    表名: 列(字段): 联系人实体类:构造方法,setters .getters方法 File:   Contact.java package com.example.sqlitetest; publi ...