【计蒜课】【数据结构】【邻接矩阵使用的复习】

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

#define MAX_N 500

typedef struct Graph {
int mat[MAX_N][MAX_N];
int n;
}Graph;

void init(Graph *g, int len) {
g->n=len;
memset(g->mat,0,sizeof(g->mat));
}

void insert(Graph *g, int a, int x, int y) {
if(x<0 || y<0 || x>=g->n || y>=g->n){
return;
}
if(a==0){
g->mat[x][y]=1;
}
if(a==1){
g->mat[x][y]=1;
g->mat[y][x]=1;
}
}

void output(Graph *g) {
for(int i=0;i<g->n;i++){
for(int j=0;j<g->n;j++){
if(!j){
printf("%d",g->mat[i][j]);
}
if(j){
printf(" %d",g->mat[i][j]);}
}
printf("\n");
}
}

int main() {
int n,m,a,x,y;
scanf("%d %d",&n,&m);
Graph *graph=(Graph*)malloc(sizeof(Graph));
init(graph,n);
for(int i=0;i<m;i++){
scanf("%d %d %d",&a,&x,&y);
insert(graph,a,x,y);
}
output(graph);
free(graph);
return 0;
}

posted @ 2019-12-09 10:46  马猴烧酒孟优秀  阅读(181)  评论(0编辑  收藏  举报