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)

最新文章

  1. Windows OS上安装运行Apache Kafka教程
  2. gradle相关配置内容解析
  3. 代理委托和block
  4. day10---异步I/O,gevent协程
  5. 【LintCode】计算两个数的交集(二)
  6. 安装完grunt和grunt-cli仍然无法识别grunt
  7. 从数据包谈如何封杀P2SP类软件
  8. 双心ping GUI工具1.0
  9. 网站TCP链接暴增
  10. 操作系统-mac安装linux(Ubuntu)
  11. OpenSSL使用指南
  12. 发送email
  13. CSS 实现自动换行、强制换行、强制不换行的属性
  14. Oracle DB Day02(SQL)
  15. VS Code保存使用项目Eslint规则格式化代码
  16. String和常量池
  17. redux 知识点
  18. Activiti源码:StandaloneInMemProcessEngineConfiguration与SpringProcessEngineConfiguration
  19. JavaScript编写风格指南 (二)
  20. U盘启动装完系统后 一拔下优盘 就不能进入系统

热门文章

  1. Mathtype启动失败与Microsoft公式编辑器Equation的问题处理案例
  2. Dev-C++ 小问题锦集
  3. 互联网的keyvalue处理
  4. c#实现QQ群成员列表导出及邮件群发之邮件群发
  5. NGUI的UIPanel、UIButton、AtlasMaker、Widget、Anchor、Tween、RectTransform
  6. PyQt 5事件和信号
  7. DataFactory+MySQL数据构造
  8. 基于RabbitMQ的跨平台RPC框架
  9. 深入浅出 Java Concurrency (11): 锁机制 part 6 CyclicBarrier
  10. MongoDB + Spark: 完整的大数据解决方案