씨발 무슨 흘깃보는 클론코딩하는데 몇시간 쓰는거 정상이냐?
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 1001
#define TRUE 1
#define FALSE 0
#define MAX_QUEUE_SIZE 1001
typedef int element;
int visited[MAX_VERTICES];
int visitedtwo[MAX_VERTICES];
typedef struct GraphType {
int n;
int adj_mat[MAX_VERTICES][MAX_VERTICES];
}GraphType;
typedef struct {
element queue[MAX_QUEUE_SIZE];
int front, rear;
}QueueType;
void queue_init(QueueType *q) {
q->rear=q->front = 0;
}
int is_empty(QueueType* q) {
return (q->front == q->rear);
}
int is_full(QueueType* q) {
return ((q->rear + 1) % MAX_QUEUE_SIZE == q->front);
}
void enqueue(QueueType* q, int num) {
if (is_full(q)) {
fprintf(stderr, "큐 에러");
return;
}
q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
q->queue[q->rear] = num;
}
element dequeue(QueueType* q) {
if (is_empty(q)) {
fprintf(stderr, "공백 오류");
}
q->front = (q->front + 1) % MAX_QUEUE_SIZE;
return q->queue[q->front];
}
void init(GraphType* g) {
g->n=1;
int r,c;
for (r=0; r<MAX_VERTICES; r++) {
for (c=0; c<MAX_VERTICES; c++) {
g->adj_mat[r][c]=0;
}
}
}
void insert_vertex(GraphType* g) {
if(g->n >= MAX_VERTICES) {
fprintf(stderr, "VERTEX 수 초과");
return;
}
g->n++;
}
void insert_edge(GraphType* g, int start, int end) {
if (start > g->n || end > g->n) {
fprintf(stderr, "EDGE 번호 오류");
return;
}
g->adj_mat[start]+=1;
g->adj_mat+[start]=1;
}
void dfs_mat(GraphType *g, int initnum) {
visited[initnum]=TRUE;
printf("%d ", initnum);
for(int i=1; i<(g->n); i++) {
if(g->adj_mat[initnum][i] && !visited[i]) {
dfs_mat(g, i);
}
}
}
void bfs_mat(GraphType *g, int initnum) {
QueueType q;
queue_init(&q);
element popped;
visitedtwo[initnum] = TRUE;
printf("%d ", initnum);
enqueue(&q, initnum);
while(!is_empty(&q)) {
popped = dequeue(&q);
for(int i=1; i<(g->n); i++) {
if(g->adj_mat[popped][i] && !visitedtwo[i]) {
visitedtwo[i]=TRUE;
printf("%d ", i);
enqueue(&q, i);
}
}
}
}
int main(void) {
int vs, es, initnum, start, end;
GraphType* g;
g=(GraphType *)malloc(sizeof(GraphType));
init(g);
scanf("%d %d %d", &vs, &es, &initnum);
for(int i=0; i<vs; i++) insert_vertex(g);
for(int j=0; j<es; j++) {
scanf("%d %d", &start, &end);
insert_edge(g, start, end);
}
dfs_mat(g, initnum);
printf("\n");
bfs_mat(g, initnum);
free(g);
return 0;
}
3분 이내
그건 영타연습아니냐..? 씨발