PTA浙江大学数据结构习题——第六周

图1 列出连通集

用邻接矩阵存图。

#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

const int N = 15;

int n, m;
int list[N][N];
bool st[N];

void dfs(int u)
{
    st[u] = true;
    printf("%d ", u);
    
    for (int i = 0; i < n; i ++)
        if (!st[i] && list[u][i])
            dfs(i);
}

void bfs(int u)
{
    queue<int> q;
    q.push(u);
    st[u] = true;
    
    while (q.size())
    {
        int p = q.front();
        q.pop();
        printf("%d ", p);
        
        for (int i = 0; i < n; i ++)
        {
            if (!st[i] && list[p][i])
            {
                st[i] = true;
                q.push(i);
            }
        }
    }
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i < m; i ++)
    {
        int a, b;
        cin >> a >> b;
        list[a][b] = list[b][a] = 1;
    }
    
    for (int i = 0; i < n; i ++)
    {
        if (!st[i]) 
        {
            printf("{ ");
            dfs(i);
            printf("}\n");
        }
    }
    
    memset(st, false, sizeof st);
    
    for (int i = 0; i < n; i ++)
    {
        if (!st[i])
        {
            printf("{ ");
            bfs(i);
            printf("}\n");
        }
    }
    
    return 0;
}

图2 Saving James Bond - Easy Version

构造一个新结构,存放坐标、是否能直接上岸、是否已经遍历过、是否能第一步跳上的信息。
然后将所有鳄鱼信息存放到数组中,依次从每个能第一步跳上的鳄鱼开始作 BFSDFS

#include <iostream>
#include <cmath>
#include <queue>

using namespace std;

const int N = 110;

int n, d;
bool res;
struct Node
{
    int x, y;   // 鳄鱼坐标
    bool safe;  // 离岸距离小于等于d
    bool st;    // 是否已经跳过该鳄鱼
    bool first; // 第一步能否跳上该鳄鱼
} node[N];

double get_d(int x1, int y1, int x2, int y2)
{
    return sqrt(pow(x1 - x2, 2.0) + pow(y1 - y2, 2.0));
}

void dfs(int u)
{
    if (node[u].safe)
    {
        res = true;
        return;
    }
    
    node[u].st = true;
    
    for (int i = 0; i < n; i ++)
        if (!node[i].st && (get_d(node[i].x, node[i].y, node[u].x, node[u].y) <= d))
            dfs(i);
}

void bfs(int u)
{
    queue<int> q;
    q.push(u);
    node[u].st = true;
    
    while (q.size())
    {
        int p = q.front();
        q.pop();
        if (node[p].safe)
        {
            res = true;
            return;
        }
        
        for (int i = 0; i < n; i ++)
            if (get_d(node[p].x, node[p].y, node[i].x, node[i].y) <= d && !node[i].st)
            {
                node[i].st = true;
                q.push(i);
            }
    }
}

int main()
{
    cin >> n >> d;
    for (int i = 0; i < n; i ++)
    {
        int x, y;
        cin >> x >> y;
        node[i].x = x, node[i].y = y;
        if (abs(x - 50) <= d || abs(x + 50) <= d || abs(y + 50) <= d || abs(y - 50) <= d)
            node[i].safe = true;
        else node[i].safe = false;
        
        // 第一步可以跳上去
        if (get_d(x, y, 0, 0) <= d + (double)15 / 2) node[i].first = true;
        else    node[i].first = false;
        
        node[i].st = false;
    }
    
    for (int i = 0; i < n; i ++)
    {
        if (node[i].first && !node[i].st)
        {
            dfs(i);
//             bfs(i);
        }
    }
    if (res)    puts("Yes");
    else    puts("No");
    
    return 0;
}

图3 六度空间

构建邻接表,然后对每个节点做一次 BFS 操作,返回距离小于等于 6 的节点个数。

#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

const int N = 1010;

typedef struct Node *List;
struct Node
{
    int data;
    List next;
};
List g[N];
bool st[N];
int n, m;

int bfs(int u)
{
    queue<int> q;
    q.push(u);
    st[u] = true;
    
    int res = 1, level = 0, last = u, tail = u;
    
    while (q.size())
    {
        int p = q.front();
        q.pop();
        
        // 找到与p直接相连的节点并入队
        List cur = g[p]->next;
        while (cur)
        {
            if (!st[cur->data])
            {
                q.push(cur->data);
                st[cur->data] = true;
                res ++;
                tail = cur->data;	// 入队完成后,tail表示当前层最后一个入队的元素
            }
            cur = cur->next;
        }
        
        // 如果当前出队元素等于上一层最后一个入队元素,更新last,表示进入新的一层
        if (p == last)
        {
            last = tail;
            level ++;
        }
        
        if (level == 6) break;
    }
    
    return res;
}

int main()
{
    cin >> n >> m;
    
    // 初始化邻接表
    for (int i = 1; i <= n; i ++)
    {
        g[i] = (List)malloc(sizeof (struct Node));
        g[i]->data = i;
        g[i]->next = NULL;
    }
    
    // 初始化边
    for (int i = 0; i < m; i ++)
    {
        int x, y;
        cin >> x >> y;
        
        List cur = (List)malloc(sizeof (struct Node));
        cur->data = y;
        cur->next = g[x]->next;
        g[x]->next = cur;
        
        cur = (List)malloc(sizeof (struct Node));
        cur->data = x;
        cur->next = g[y]->next;
        g[y]->next = cur;
    }
    
    for (int i = 1; i <= n; i ++)
    {
        memset(st, false, sizeof st);
        int cnt = bfs(i);
        printf("%d: %.2f%%\n", i, (double)(100.0 * cnt / n));
    }
    
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值