PAT : 数据结构与算法题目集(中文)7-7 六度空间

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/belous_zxy/article/details/87902282

目录

DFS

BFS(邻接表+vector)

BFS(邻接矩阵)


DFS:

#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#define MaxIndex 10001
using namespace std;
int getdigit(void)
{
    int digit{0};
    char ch{0};
    while (1)
    {
        ch = getchar();
        if (!isdigit(ch))
            return digit;
        digit = digit * 10 + ch - '0';
    }
}
int N{0};
bool book[MaxIndex], mat[MaxIndex][MaxIndex];
int isp{0};
int sheet[MaxIndex];
void mDFS(int P, int cnt)
{
    isp++;
    book[P] = true;
    sheet[P] = cnt;
    if (cnt == 6)
        return;
    for (int i = 1; i <= N; ++i)
    {
        if (mat[P][i])
        {
            if (!book[i])
                mDFS(i, cnt + 1);
            if (sheet[i] > cnt + 1)
            {
                isp--;
                mDFS(i, cnt + 1);
            }
        }
    }
}
void DFS(int P)
{
    isp = 0;
    mDFS(P, 0);
    printf("%d: %.2lf%%\n", P, (static_cast<double>(isp) / N) * 100);
}
int main(int argc, char *argv[])
{
    int M;
    N = getdigit();
    M = getdigit();
    while (M--)
    {
        int a = getdigit(), b = getdigit();
        mat[a][b] = mat[b][a] = true;
    }
    for (int i = 1; i <= N; ++i)
    {
        memset(book, false, sizeof(bool) * (N + 1));
        DFS(i);
    }
    return EXIT_SUCCESS;
}

DFS显然并不擅长这个问题,距起始点距离不得超过6,BFS更为适合~

BFS(邻接表+vector):

#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#define MaxIndex 10001
using namespace std;
int getdigit(void)
{
    int digit{0};
    char ch{0};
    while (1)
    {
        ch = getchar();
        if (!isdigit(ch))
            return digit;
        digit = digit * 10 + ch - '0';
    }
}
int N{0};
bool book[MaxIndex];
vector<int> List[MaxIndex];
void BFS(int P)
{
    struct T
    {
        int value, asp;
    };
    queue<T> Q;
    T temp = {P, 0};
    Q.push(temp);
    book[P] = true;
    int pass{1};
    while (!Q.empty())
    {
        T top = Q.front();
        if(top.asp==6)
            break;
        for (const auto &i : List[top.value])
        {
            if (!book[i])
            {
                pass++;
                Q.push({i,top.asp+1});
                book[i] = true;
            }
        }
        Q.pop();
    }
    printf("%d: %.2lf%%\n", P, (static_cast<double>(pass) / N) * 100);
}
int main(int argc, char *argv[])
{
    int M;
    N = getdigit();
    M = getdigit();
    while (M--)
    {
        int a = getdigit(), b = getdigit();
        List[a].push_back(b);
        List[b].push_back(a);
    }
    for (int i = 1; i <= N; ++i)
    {
        memset(book, false, sizeof(bool) * (N + 1));
        BFS(i);
    }
    return EXIT_SUCCESS;
}

BFS(邻接矩阵):

#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
#define MaxIndex 10001
using namespace std;
int getdigit(void)
{
    int digit{0};
    char ch{0};
    while (1)
    {
        ch = getchar();
        if (!isdigit(ch))
            return digit;
        digit = digit * 10 + ch - '0';
    }
}
int N{0};
bool book[MaxIndex], mat[MaxIndex][MaxIndex];
void BFS(int P)
{
    struct ints
    {
        int sp, cnt;
    };
    queue<ints> Q;
    Q.push({P, 0});
    book[P] = true;
    int pass{1};
    while (!Q.empty())
    {
        ints top = Q.front();
        if (top.cnt == 6)
            break;
        for (int i = 1; i <= N; ++i)
        {
            if (mat[top.sp][i] && !book[i])
            {
                pass++;
                Q.push({i, top.cnt + 1});
                book[i] = true;
            }
        }
        Q.pop();
    }
    printf("%d: %.2lf%%\n", P, (static_cast<double>(pass) / N) * 100);
}
int main(int argc, char *argv[])
{
    int M;
    N = getdigit();
    M = getdigit();
    while (M--)
    {
        int a = getdigit(), b = getdigit();
        mat[a][b] = mat[b][a] = true;
    }
    for (int i = 1; i <= N; ++i)
    {
        memset(book, false, sizeof(bool) * (N + 1));
        BFS(i);
    }
    return EXIT_SUCCESS;
}

节点数很大、通路较少的情况下(稀疏),邻接表存储图的方式会好很多~

END

猜你喜欢

转载自blog.csdn.net/belous_zxy/article/details/87902282