HDU3362-Fix

Fix

                                                                         Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                                        Total Submission(s): 1038    Accepted Submission(s): 350


Problem Description
There are a few points on a plane, and some are fixed on the plane, some are not. We want to connect these points by some sticks so that all the points are fixed on the plane. Of course, we want to know the minimum length of the sum of the sticks.

As in the images, the black points are fixed on the plane and red ones are not, which need to be fixed by sticks.
All the points in the left image have been fixed. But the middle one is not, which we need add one stick to fix those four points (the right image shows that stick). Triangle is steady, isn’t it?
 

Input
The input consists of multiply test cases. The first line of each test case contains one integer, n (1 <= n <= 18), which is the number of points. The next n lines, each line consists of three integers, x, y, c (0 <= x, y < 100). (x, y) indicate the coordinate of one point; c = 1 indicates this point is fixed; c = 0 indicates this point is not fixed. You can assume that no two points have the same coordinate.
The last test case is followed by a line containing one zero, which means the end of the input.
 

Output
Output the minimum length with six factional digits for each test case in a single line. If you cannot fix all the points, then output “No Solution”.
 

Sample Input
  
4 0 0 1 1 0 1 0 1 0 1 1 0 3 0 0 1 1 1 0 2 2 0 0
 

Sample Output
  
4.414214 No Solution
 

Source
 

题意:题目给出n(n <= 18)个点的二维坐标,并说明某些点是被固定了的,其余则没固定,要求添加一些边,使得还没被固定的点变成固定的,若不能使所有点固定则输出No Solution

解题思路:由于n很小,而且固定点的顺序没有限制,可以用状态压缩DP。当一个没固定的点和两个固定了的点连接后,该点就被间接固定了(三角形的稳定性质)。无论是直接固定还是间接固定的点,都可以供以后的点用于固定。若有有两点以上是固定则一定可以将所有点固定。因此,对于当前需要固定的点,在已经是固定状态的点中选出两个距离当前点最小的,这就保证了局部最优,从起始状态开始转移,最后判断能否到达最终目标状态就可以了。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>

using namespace std;

int a[50],b[50],c[50];
double dp[1000000];

int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        int k=0,cnt=0;
        memset(dp,0,sizeof dp);
        for(int i=0;i<n;i++)
        {
            scanf("%d %d %d",&a[i],&b[i],&c[i]);
            k=k|(c[i]<<i);
            if(c[i]) cnt++;
        }
        if(n==1&&c[0]) {printf("0.000000\n");continue;}
        if(n==1&&!c[0]) {printf("No Solution\n");continue;}
        if(cnt<2) {printf("No Solution\n");continue;}
        int m=1<<n;
        for(int i=k+1;i<m;i++)
        {
            int flag=1;
            for(int j=0;j<n;j++)
                if(c[j]&&!((1<<j)&i)) flag=0;
            if(!flag) continue;
            double mi=999999999;
            for(int j=0;j<n;j++)
            {
                if(((1<<j)&i)&&!c[j])
                {
                    int kk=i-(1<<j);
                    for(int p=0;p<n;p++)
                    {
                        if((1<<p)&kk)
                        {
                            for(int q=p+1;q<n;q++)
                            {
                                if((1<<q)&kk)
                                    mi=min(mi,dp[kk]+1.0*sqrt((a[j]-a[p])*(a[j]-a[p])+(b[j]-b[p])*(b[j]-b[p]))+1.0*sqrt((a[j]-a[q])*(a[j]-a[q])+(b[j]-b[q])*(b[j]-b[q])));
                            }
                        }
                    }
                }
            }
            dp[i]=mi;
        }
        printf("%lf\n",dp[m-1]);
    }
    return 0;
}

static void parse_gprmc(nmea_msg *gps, const char *buf) { const char *p = strstr(buf, "GPRMC"); if (!p) return; uint8_t pos; uint8_t dx; // UTC时间 pos = nmea_comma_pos((const uint8_t *)p, 1); if (pos != 0xFF) { uint32_t time = nmea_str2num((const uint8_t *)(p + pos), &dx); gps->utc.hour = time / 10000; gps->utc.min = (time % 10000) / 100; gps->utc.sec = time % 100; } // 状态检查 (A=有效, V=无效) pos = nmea_comma_pos((const uint8_t *)p, 2); if (pos != 0xFF && *(p + pos) == 'V') { gps->fixmode = 0; // 无效定位 return; } // 纬度 pos = nmea_comma_pos((const uint8_t *)p, 3); if (pos != 0xFF) { float lat_val = nmea_str2num((const uint8_t *)(p + pos), &dx) / 100.0f; int deg = (int)(lat_val) / 100; float min = lat_val - deg * 100; gps->latitude = deg + min / 60.0f; } // 纬度半球 pos = nmea_comma_pos((const uint8_t *)p, 4); if (pos != 0xFF) gps->nshemi = *(p + pos); // 经度 pos = nmea_comma_pos((const uint8_t *)p, 5); if (pos != 0xFF) { float lon_val = nmea_str2num((const uint8_t *)(p + pos), &dx) / 100.0f; int deg = (int)(lon_val) / 100; float min = lon_val - deg * 100; gps->longitude = deg + min / 60.0f; } // 经度半球 pos = nmea_comma_pos((const uint8_t *)p, 6); if (pos != 0xFF) gps->ewhemi = *(p + pos); } // 完整GPGGA解析 static void parse_gpgga(nmea_msg *gps, const char *buf) { const char *p = strstr(buf, "GPGGA"); if (!p) return; uint8_t pos; uint8_t dx; // UTC时间 pos = nmea_comma_pos((const uint8_t *)p, 1); if (pos != 0xFF) { uint32_t time = nmea_str2num((const uint8_t *)(p + pos), &dx); gps->utc.hour = time / 10000; gps->utc.min = (time % 10000) / 100; gps->utc.sec = time % 100; } // 纬度 pos = nmea_comma_pos((const uint8_t *)p, 2); if (pos != 0xFF) { float lat_val = nmea_str2num((const uint8_t *)(p + pos), &dx) / 100.0f; int deg = (int)(lat_val) / 100; float min = lat_val - deg * 100; gps->latitude = deg + min / 60.0f; } // 纬度半球 pos = nmea_comma_pos((const uint8_t *)p, 3); if (pos != 0xFF) gps->nshemi = *(p + pos); // 经度 pos = nmea_comma_pos((const uint8_t *)p, 4); if (pos != 0xFF) { float lon_val = nmea_str2num((const uint8_t *)(p + pos), &dx) / 100.0f; int deg = (int)(lon_val) / 100; float min = lon_val - deg * 100; gps->longitude = deg + min / 60.0f; } // 经度半球 pos = nmea_comma_pos((const uint8_t *)p, 5); if (pos != 0xFF) gps->ewhemi = *(p + pos); // 定位质量 pos = nmea_comma_pos((const uint8_t *)p, 6); if (pos != 0xFF) gps->fixmode = nmea_str2num((const uint8_t *)(p + pos), NULL); // 卫星数 pos = nmea_comma_pos((const uint8_t *)p, 7); if (pos != 0xFF) gps->posslnum = nmea_str2num((const uint8_t *)(p + pos), NULL); // 海拔高度 pos = nmea_comma_pos((const uint8_t *)p, 9); if (pos != 0xFF) gps->altitude = nmea_str2num((const uint8_t *)(p + pos), &dx) / 10.0f; } // 主解析函数 void gps_parse_nmea(nmea_msg *gps, const char *buf) { // 校验和验证 const char *end = strchr(buf, '*'); if (end) { uint8_t calc_check = nmea_str2num(buf); uint8_t recv_check = strtoul(end + 1, NULL, 16); if (calc_check != recv_check) { return; // 校验失败 } } if (strstr(buf, "GPRMC")) parse_gprmc(gps, buf); else if (strstr(buf, "GPGGA")) parse_gpgga(gps, buf); } // 显示数据 void gps_show_data(const nmea_msg *gps) { if (gps->fixmode == 0) { printf("No valid GPS fix\n"); return; } printf("Longitude: %.6f° %c\n", gps->longitude, gps->ewhemi); printf("Latitude: %.6f° %c\n", gps->latitude, gps->nshemi); printf("Altitude: %.1f meters\n", gps->altitude); printf("UTC Time: %02d:%02d:%02d\n", gps->utc.hour, gps->utc.min, gps->utc.sec); printf("Satellites: %d\n", gps->posslnum); printf("Fix mode: %s\n", gps->fixmode == 1 ? "GPS" : gps->fixmode == 2 ? "DGPS" : "Unknown"); } 修过后该处显示以下错误如何修改[OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c: In function 'gps_parse_nmea': [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:174:43: warning: pointer targets in passing argument 1 of 'nmea_str2num' differ in signedness [-Wpointer-sign] [OHOS ERROR] uint8_t calc_check = nmea_str2num(buf); [OHOS ERROR] ^~~ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:27:5: note: expected 'const uint8_t * {aka const unsigned char *}' but argument is of type 'const char *' [OHOS ERROR] int nmea_str2num(const uint8_t *buf, uint8_t *dx) { [OHOS ERROR] ^~~~~~~~~~~~ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:174:30: error: too few arguments to function 'nmea_str2num' [OHOS ERROR] uint8_t calc_check = nmea_str2num(buf); [OHOS ERROR] ^~~~~~~~~~~~ [OHOS ERROR] ../../../vendor/pzkj/pz_hi3861/common/bsp/src/bsp_gps.c:27:5: note: declared here [OHOS ERROR] int nmea_str2num(const uint8_t *buf, uint8_t *dx) { [OHOS ERROR] ^~~~~~~~~~~~ [OHOS ERROR] you can check build log in D:\hi3861\hi3861_hdu_iot_application\src\out\hispark_pegasus\wifiiot_hispark_pegasus\build.log [OHOS ERROR] command: "D:\hi3861\hi3861_hdu_iot_application\src\prebuilts\build-tools\win64-x64\bin\ninja.exe -w dupbuild=warn -C D:\hi3861\hi3861_hdu_iot_application\src\out\hispark_pegasus\wifiiot_hispark_pegasus" failed [OHOS ERROR] return code: 1 [OHOS ERROR] execution path: D:\hi3861\hi3861_hdu_iot_application\src scons: *** [src\out\hispark_pegasus\wifiiot_hispark_pegasus\target.elf] Error -1
最新发布
07-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值