• [Luogu OJ P1433][2013.10.18]DFS基础题-吃奶酪


    近期第一次写DFS,纪念一下

    题目描述

    房间里放着n块奶酪。一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处。
     

    输入格式

    第一行一个数n  (n<=15)
    接下来每行2个,表示第i块奶酪的坐标。
    两点之间的距离公式=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))
     

    输出格式

    一个数,表示要跑的最少距离,保留2位小数。
     

    样例输入 

    4
    1 1
    1 -1
    -1 1
    -1 -1

    样例输出 

    7.41

    明显的搜索、、、n<=15

    数据不强,就加了一步优化  if xx>Iris Then Exit;

    有兴趣的话还可以把各点间距离存起来、、、

    搜索+回溯、

    不详细解释了、

     1 Var
     2   x,y:Array[0..21] of Double;
     3   Used:Array[0..21] of Boolean;
     4   i,n:Integer;
     5   xx,Iris:Extended;
     6 Procedure DFS(i,t:Integer;xx:Double);
     7   Var
     8     j:integer;
     9   Begin
    10     if xx>Iris Then Exit;
    11     if t=n then if xx<Iris Then Iris:=xx;
    12     For j:=1 to n do
    13       if Not Used[j] then
    14         Begin
    15           Used[j]:=True;
    16           xx:=xx+Sqrt(Sqr(x[i]-x[j])+Sqr(y[i]-y[j]));
    17           t:=t+1;
    18           DFS(j,t,xx);
    19           t:=t-1;
    20           xx:=xx-Sqrt(Sqr(x[i]-x[j])+Sqr(y[i]-y[j]));
    21           Used[j]:=False;
    22         End;
    23   End;
    24 Begin
    25   Read(n);
    26   For i:=1 to n do
    27     Read(x[i],y[i]);
    28   Fillchar(Used,Sizeof(Used),False);
    29   Iris:=Maxlongint Shr 1;
    30   DFs(0,0,0);
    31   Writeln(Iris:0:2);
    32 End.

    评测详情

    编译成功

    • 测试点1:通过该数据点。得分10,耗时 46 ms,内存 2166 kb。
    • 测试点2:通过该数据点。得分10,耗时 0 ms,内存 2170 kb。
    • 测试点3:通过该数据点。得分10,耗时 15 ms,内存 2166 kb。
    • 测试点4:通过该数据点。得分10,耗时 0 ms,内存 2170 kb。
    • 测试点5:通过该数据点。得分10,耗时 953 ms,内存 2166 kb。
    • 测试点6:通过该数据点。得分10,耗时 62 ms,内存 2170 kb。
    • 测试点7:通过该数据点。得分10,耗时 0 ms,内存 2170 kb。
    • 测试点8:通过该数据点。得分10,耗时 0 ms,内存 2170 kb。
    • 测试点9:通过该数据点。得分10,耗时 515 ms,内存 2166 kb。
    • 测试点10:通过该数据点。得分10,耗时 0 ms,内存 2170 kb。
  • 相关阅读:
    简单伪类
    购物网页css
    「WC2020T2」猜数
    ARC 103
    Codeforces 1198F
    ZJOI2019二试游记
    ZJOI2019一试游记
    「WC2015」未来程序
    「CodeForces Round #545 Div2」划水记
    「CF1116」Microsoft Q# Coding Contest
  • 原文地址:https://www.cnblogs.com/Catch-22/p/3376448.html
Copyright © 2020-2023  润新知