数据结构第七次作业第二题(拓扑排序)

在这里插入图片描述

#include <stdio.h>

char exchange(int index)
{
    if (index <= 8)
        return index + 'A';
    else
        return index - 9 + 'S';
}

int inverse(char index)
{
    if (index >= 'A' && index <= 'I')
        return index - 'A';
    else
        return index - 'S' + 9;
}

int main()
{
    char v1, v2;
    scanf("%c,%c", &v1, &v2);
    int n1 = inverse(v1);
    int n2 = inverse(v2);
    int graph[11][11];
    int front = 0, rear = 0;
    for (int i = 0; i < 11; i++)
        for (int j = 0; j < 11; j++)
            graph[i][j] = 0;
    graph[0][1] = 1;
    graph[0][4] = 1;
    graph[1][2] = 1;
    graph[2][10] = 1;
    graph[3][0] = 1;
    graph[3][4] = 1;
    graph[4][8] = 1;
    graph[4][2] = 1;
    graph[4][5] = 1;
    graph[5][10] = 1;
    graph[5][2] = 1;
    graph[6][7] = 1;
    graph[6][4] = 1;
    graph[6][3] = 1;
    graph[7][8] = 1;
    graph[7][4] = 1;
    graph[8][10] = 1;
    graph[8][5] = 1;
    int in[11] = {1, 1, 3, 1, 4, 2, 0, 1, 2, -1, 3};
    graph[n1][n2] = 0;
    in[n2]--;
    printf("S,");
    int cnt = 0;
    int queue[100];
    for (int i = 0; i < 11; i++)
    {
        if (in[i] == 0)
            queue[++rear] = i;
    }
    while (front < rear)
    {
        int index = queue[++front];
        for (int i = 0; i < 11; i++)
        {
            if (graph[index][i] != 0)
            {
                graph[index][i] = 0;
                if (--in[i] == 0)
                    queue[++rear] = i;
            }
        }
        printf("%c,", exchange(index));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值