회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
1
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
2178 미로탐색 메모리초
2178번 - 미로 탐색
jun2077681
7년 전
0
저 코드대로 했는데 메모리 초과 뜨네요... 어떻게 해결해야하나요??ㅠㅠ
#include <stdio.h> struct queueNode { int data1; int data2; int data3; struct queueNode *nextPtr; }; typedef struct queueNode QueueNode; typedef QueueNode *QueueNodePtr; void enqueue(QueueNodePtr *headPtr, QueueNodePtr *tailPtr, int x, int y, int l); void dequeue(QueueNodePtr *headPtr, QueueNodePtr *tailPtr); int isEmpty(QueueNodePtr headPtr); int m, n, count = 0; int map[101][101]; QueueNodePtr headPtr = NULL; QueueNodePtr tailPtr = NULL; void BFS(int x, int y) { int pos = 0; int tempx, tempy, templ; enqueue(&headPtr, &tailPtr, x, y, 1); while (pos < count && ((tailPtr->data1 != n - 1) || (tailPtr->data2 != m - 1))) { map[headPtr->data1][headPtr->data2] = 0; tempx = headPtr->data1; tempy = headPtr->data2; templ = headPtr->data3; if (tempy > 0 && map[tempx][tempy - 1] == 1) enqueue(&headPtr, &tailPtr, tempx, tempy - 1, templ + 1); if (tempy < m - 1 && map[tempx][tempy + 1] == 1) enqueue(&headPtr, &tailPtr, tempx, tempy + 1, templ + 1); if (tempx > 0 && map[tempx - 1][tempy] == 1) enqueue(&headPtr, &tailPtr, tempx - 1, tempy, templ + 1); if (tempx < n - 1 && map[tempx + 1][tempy] == 1) enqueue(&headPtr, &tailPtr, tempx + 1, tempy, templ + 1); pos++; dequeue(&headPtr, &tailPtr); } if (pos < count) printf("%d", tailPtr->data3); } int main(void) { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf("%1d", &map[i][j]); BFS(0, 0); return 0; } void enqueue(QueueNodePtr *headPtr, QueueNodePtr *tailPtr, int x, int y, int l) { QueueNodePtr newPtr; count++; newPtr = malloc(sizeof(QueueNode)); if (newPtr != NULL) { newPtr->data1 = x; newPtr->data2 = y; newPtr->data3 = l; newPtr->nextPtr = NULL; if (isEmpty(*headPtr)) { *headPtr = newPtr; } else { (*tailPtr)->nextPtr = newPtr; } *tailPtr = newPtr; } else { return -1; } } void dequeue(QueueNodePtr *headPtr, QueueNodePtr *tailPtr) { int value1, value2, value3; QueueNodePtr tempPtr; value1 = (*headPtr)->data1; value2 = (*headPtr)->data2; value3 = (*headPtr)->data3; tempPtr = *headPtr; *headPtr = (*headPtr)->nextPtr; if (*headPtr == NULL) *tailPtr = NULL; free(tempPtr); } int isEmpty(QueueNodePtr headPtr) { return headPtr == NULL; }
gallopsys
7년 전
1
28번 줄 while에 방문할 때마다 map[headPtr->data1][headPtr->data2] = 0;를 해서 visited를 체크하게 되면 중복으로 탐색하는 경우가 기하급수적으로 늘어나게 됩니다.
따라서 enqueue를 해주실 때, map의 해당좌표를 0으로 만들어 또 enqueue가 되지 않도록 만들어주셔야 합니다.
jun2077681
7년 전
0
감사합니다 ㅠ.ㅠ
댓글을 작성하려면
로그인
해야 합니다.
jun2077681 7년 전
저 코드대로 했는데 메모리 초과 뜨네요... 어떻게 해결해야하나요??ㅠㅠ