#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;
}
数据结构第七次作业第二题(拓扑排序)
最新推荐文章于 2024-02-22 20:57:12 发布