传递闭包计算warshall算法C语言实现

传递闭包计算的warshall算法c语言实现

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define N 3
#define TRUE 0


int get_matrix(int a[N][N])
{
    int i = 0,j = 0;
    for (i = 0;i < N;i++) {
        for (j = 0;j < N;j++) {
            scanf("%d",&a[i][j]);
            if (a[i][j] != 0 && a[i][j] != 1) {
                printf("0 or 1 in matrix\n");
                exit(2);
            }
        }
    }
    return TRUE;
}

int output_matrix(int a[N][N])
{
    int i = 0,j = 0;
    for (i = 0;i < N;i++) {
        for (j = 0;j < N;j++) {
            printf("%d ",a[i][j]);
        }
        putchar('\n');
    }
    return TRUE;
}

int warshall(int a[N][N])
{
    int col = 0;
    int line = 0;
    int temp = 0;

    for (col = 0;col < N;col++) {
        for (line = 0;line < N;line++) {
            if (a[line][col] != 0) {
                for (temp = 0;temp < N;temp++) {
                    a[line][temp] = a[line][temp] | a[col][temp];
                }
            }
        }
    }
    return TRUE;
}

int main(void)
{
    int a[N][N] = {0};

    printf("please input a matrix with %d * %d:\n",N,N);
    if (get_matrix(a)) {
        printf("get matrix error!\n");
        exit(1);
    }

    warshall(a);

    output_matrix(a);

    return 0;
}

你可能感兴趣的:(算法,C语言,warshall)