216-Getting in Line

Source: Internet
Author: User

[Cpp]
Description: Water question. I will not explain it ......
 
# Include <iostream>
# Include <cstdio>
# Include <cmath>
# Include <cstring>
Using namespace std;
Double count;
Int n, step [10];
Void dfs (int cur, double sum, int * flag, int (* p) [10])
{
If (cur> n)
{
If (sum <count)
{
Count = sum; www.2cto.com
For (int I = 1; I <= n; I ++) step [I] = flag [I];
}
Return;
}
Else for (int I = 1; I <= n; I ++)
{
Int j;
For (j = 1; j <= n; j ++)
If (flag [j] = I) break;
If (flag [j] = I) continue;
Flag [cur] = I;
If (cur> = 2)
{
Int x = p [0] [flag [cur-1], y = p [1] [flag [cur-1];
Double c = sqrt (p [0] [I]-x) * (p [0] [I]-x) + (p [1] [I]-y) * (p [1] [I]-y ));
Dfs (cur + 1, sum + c, flag, p );
}
Else dfs (cur + 1, sum, flag, p );
Flag [cur] = 0;
}
}
Int main ()
{
# Ifndef ONLINE_JUDGE
Freopen ("a.txt", "r", stdin );
# Endif
Int j (0), num [2] [10], flag [10];
While (scanf ("% d", & n )! = EOF)
{
If (! N) break;
Memset (num, 0, sizeof (num ));
Memset (flag, 0, sizeof (flag ));
Printf ("************************************* * ******************** \ n ");
Printf ("Network # % d \ n", ++ j );
Count = 100000;
For (int I = 1; I <= n; I ++)
Scanf ("% d", & num [0] [I], & num [1] [I]);
Dfs (1, 0, flag, num );
For (int I = 1; I <n; I ++)
{
Int x1 = num [0] [step [I], y1 = num [1] [step [I], x2 = num [0] [step [I + 1], y2 = num [1] [step [I + 1];
Printf ("Cable requirement to connect (% d, % d) to (% d, % d) is %. 2lf feet. \ n ", x1, y1, x2, y2, sqrt (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) + 16 );
}
Printf ("Number of feet of cable required is %. 2lf. \ n", count + (n-1) * 16 );
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.