dfs (sister of red envelopes)

Links: https://ac.nowcoder.com/acm/contest/3402/I

   Hope ah, ah hope, accompanied by the ringing of the clock, we are about to usher in a beautiful traditional festival - the Spring Festival. To add to the New Year festive atmosphere thick, juniors who have the senior sister apprentice said: "senior sister apprentice I want to receive a red envelope!", And we people of the United States and good heart sounds pretty jx senior sister apprentice is also very atmospheric and said: "Give, give big th, n fill it, "Shidishimei:"?. No more, thank senior sister apprentice, senior sister apprentice nice ", but lovely naughty senior sister apprentice to a total of n red envelope hidden in the laboratory to 228 years, and gave you every coordinate a red envelope. Wit of course you have to take away all the envelopes, but you have to move the least distance to get to all the envelopes !! Orinoco !! You start at (0,0) in.

Enter a description:

The first row contains an integer n (0 <= n <= 13)
Next n lines of two real numbers,
Red denotes the i-th coordinates x, y (0 <= | x |, | y | <= 30)
The distance between two points is a formula
sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

Output Description:

Need to go a minimum distance, the result retained two decimal places.
Example 1

Entry

copy
2
-1 1
2 2

Export

Copy 4.58

Problem-solving ideas: because so little data range can be found dfs storms
AC Code:
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10 + the C- ' 0 ' , C = getchar (); return F * X;} 
typedef Long  Long LL;
 const  int MAXN = 1E2 + 10 ;
 Double X [MAXN];
 Double Y [MAXN];
 Double ANS = 1,000,000.0 ;
 int VIS [MAXN];
 Double DIS [MAXN] [MAXN];
 int the n-;
 void the DFS ( int the X-, int now, Double MIN1) { // the X-depth, now is now the state because you want to continue to go on a state // MIN1 record minimum 
    if(min1>ans){
        return ;
    } 
    if(x==n){
        ans=min1;    
    }
    for(int i=1;i<=n;i++){
        if(vis[i]==0){
            vis[i]=1;//标记数组
            dfs(x+1,i,min1+dis[i][now]);
            vis[i]=0;
        }
    }
}
int main()
{
    
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x[i]>>y[i];
    }
    for(int i=0;i<=n;i++){
        for(int j=0;j<=n;j++){
            dis[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));//先算出打表算出任意两点的距离
        }
    }
    dfs(0,0,0.0);
    printf("%.2lf",ans);
    return 0;
}
View Code

AC Code 2:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
typedef long long ll;
const int maxn = 1e2+10;
double x[maxn],y[maxn];
double lmin=1000000.0;
double dis(int i,int j){
    return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
int vis[maxn];
int p[maxn];
int n;
void dfs(int x,double ans){
    if(ans>lmin)return;
    if(x==n){
        lmin=ans;
    }
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            vis[i]=1;
            p[x]=i;
            dfs(x+1,ans+dis(p[x],p[x-1]));//p[x-1]是上一个状态
            vis[i]=0;
        }
    }
    
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>x[i]>>y[i];
    }
    dfs(0,0.0);
    printf("%.2lf",lmin);
    return 0;
}
View Code

 

 



Guess you like

Origin www.cnblogs.com/lipu123/p/12160223.html