24h購物| | PChome| 登入
2013-08-11 14:59:42| 人氣3,899| 回應0 | 上一篇 | 下一篇

[UVA] 10751 - Chessboard

推薦 0 收藏 0 轉貼0 訂閱站台

Chessboard

Time limit: 100 milliseconds
Memory limit: 64 megabytes

A king wishes to go for a walk of a square chessboard with thefollowing conditions:

  • Each two consequent cells of the path must beadjacent, i.e., share an edge or a corner (thus, a cell mayhave up to eight adjacent cells).
  • Each cell must be visited exactly once; the first andthe last cells of the path must coincide (thus, the firstcell of the path will be actually visited twice).
  • The path must have no self intersections (we may think of apath as a closed polyline with vertices at cells' centers).

Your task is to find the maximal possible length of a king'spath (here we mean the length of the polyline, not thenumber of king's moves).

Input

The first line of the input contains the number of the testcases, which is at most 20.The descriptions of the test cases follow. Eachtest case description consists of an integer N (1 ≤ N ≤ 300), denoting the size of the chessboard. The test casesare separated by blank lines.

Output

For each test case in the input, output a line containingthe length of the king's tour with at least three digitsafter the decimal point. Print a blank line between testcases. The cells have side 1.

Examples

Input
3123

Output

0.0004.0009.414



#include <stdio.h>
#include <math.h>
int main() {
int testcase, n;
scanf("%d", &testcase);
while(testcase--) {
scanf("%d", &n);
if(n == 1)
puts("0.000");
else if(n == 2)
puts("4.000");
else
printf("%.3lf\n", (n-1)*4 + (n*n-4*(n-1))*sqrt(2));
if(testcase)
puts("");
}
return 0;
}


題目要求從一點出發,可斜著走,把所有點都走過一次,並且每個點只能走一次,最後回到原點。
問路徑最長為何?

從下圖中,可以發現很明顯的 greedy,盡可能走斜的。







台長: Morris
人氣(3,899) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA] 10882 - Koerner's Pub
此分類上一篇:[UVA] 697 - Jack and Jill

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文