06-图4. Saving James Bond - Hard Version (30)-DFS回溯

题目地址:http://www.patest.cn/contests/mooc-ds/06-%E5%9B%BE4

/* 06-图4. Saving James Bond - Hard Version (30) http://www.patest.cn/contests/mooc-ds/06-%E5%9B%BE4 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <unordered_map>
using namespace std;

#define N 102
#define INF 9999999

int n,m;

struct myp{
    int x, y;
};

void pf(myp p)
{
    printf("%d %d", p.x, p.y);
}

// 两点之间的距离 小于等于 m 就行了
bool isCanJmp(myp p1, myp p2)
{
    int tmp = (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
    if (tmp <= m*m)
        return true;
    return false;
}

// 能否调到岸边
bool isOk(myp p)
{
    if (50 - abs(p.x) <= m)
        return true;
    if (50 - abs(p.y) <= m)
        return true;
    return false;
}

myp a[N];
vector<myp> que; // 存储结果用
int ans ;
bool vis[N];

vector<myp> frs;

int find(myp p)
{
    int i;
    for (i = 0; i <= n; i++)
    {
        if (a[i].x == p.x && a[i].y == p.y)
            return i;
    }
    return -1;
}

void dfs(myp ps,int num)
{
    int i, j;
    if (isOk(ps))
    {
        if (num < ans)
        {
            ans = num;
            frs.clear();
            for (i = 0; i < num; i++)
            {
                frs.push_back(que[i]);
            }
        }
        else if (num == ans)
        {
            int d1 = que[0].x*que[0].x + que[0].y*que[0].y;
            int d2 = frs[0].x*frs[0].x + frs[0].y*frs[0].y;
            if (d1 < d2)
            {
                frs.clear();
                for (i = 0; i < num; i++)
                {
                    frs.push_back(que[i]);
                }
            }
        }
        else{

        }
        /*printf("%d\n", num); for (i = 0; i < num; i++) { pf(que[i]); printf("\n"); }*/
    }
    else
    {
        if (ps.x == 0 && ps.y == 0)
        {
            for (i = 0; i <= n; i++)
            {
                if (!vis[i])
                {
                    double tmpd = sqrt(a[i].x * a[i].x + a[i].y * a[i].y) - 7.5;
                    if (tmpd <= m)
                    {
                        vis[i] = true;
                        que[num] = a[i];
                        dfs(a[i], num + 1);
                        vis[i] = false; // 回溯
                        //vis[find(que.front())] = false;
                    }
                }
            }
        }
        else{
            for (i = 0; i <= n; i++)
            {
                if (!vis[i] && isCanJmp(ps, a[i]))
                {
                    vis[i] = true;
                    que[num] = a[i];
                    dfs(a[i], num + 1);
                    vis[i] = false; // 回溯
                }
            }
        }
    }
}

int main()
{
    //freopen("in", "r", stdin);
    int i, tmp;
    while (scanf("%d%d", &n,&m) != EOF)
    {
        a[0].x = 0;
        a[0].y = 0;
        for (i = 1; i <= n; i++)
        {
            scanf("%d%d", &a[i].x, &a[i].y);
        }
        for (i = 0; i <= n; i++)
        {
            vis[i] = false;
        }

        que.resize(1001);

        vis[0] = true;
        ans = INF;
        dfs(a[0],0);
        if (ans == INF){
            printf("0\n");
        }
        else
        {
            printf("%d\n", ans + 1);
            for (i = 0; i < ans; i++)
            {
                pf(frs[i]);
                printf("\n");
            }
        }
    }
    return 0;
}

你可能感兴趣的:(06-图4. Saving James Bond - Hard Version (30)-DFS回溯)