회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
직접 구현한 Linked List Queue 메모리 초과 이유가 무엇일까요?
2638번 - 치즈
invrtd_h
2년 전
0
분명 어딘가에서 메모리 누수가 발생하고 있는 것 같은데 그게 어딘지를 모르겠습니다...
#include<cstdio> #include<stdexcept> #include<vector> using namespace std; class Node { public: int elem; Node* next; explicit Node(int _elem, Node* _next = NULL) { elem = _elem; next = _next; } }; class LiQueue { public: Node headnode; Node* head; Node* tail; int size; LiQueue() : headnode(Node(-1)) { head = &headnode; tail = &headnode; size = 0; } ~LiQueue() { while (head->next != NULL) { Node* tobedel = head->next; head->next = head->next->next; delete tobedel; } } void push(const int n) { Node* temp = new Node(n); tail->next = temp; tail = tail->next; if (head->next == NULL) { head->next = temp; } size++; } int dequeue() { if (!size) { return -1; } int temp = head->next->elem; Node* tobedel = head->next; head->next = head->next->next; delete tobedel; size--; return temp; } int get_size() const { return size; } bool is_empty() const { return size == 0; } int get_front() const { if (size == 0) { return -1; } return head->next->elem; } int get_back() const { if (size == 0) { return -1; } return tail->elem; } void _print() const { if (head->next == NULL) { return; } Node* travel = head->next; while (travel != NULL) { printf("%d ", travel->elem); travel = travel->next; } puts(""); } }; int main() { int n, m; scanf("%d%d", &n, &m); int arr[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &arr[i][j]); } } int count = 0; while (1) { bool is_zero = true; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (arr[i][j] != 0) { is_zero = false; } } } if (is_zero) { break; } count++; LiQueue q; q.push(0); arr[0][0] = 9; while (!q.is_empty()) { int temp = q.dequeue(); int temp_r = temp + 1; if (temp_r % 1000 != m && arr[temp_r / 1000][temp_r % 1000] == 0) { arr[temp_r / 1000][temp_r % 1000] = 9; q.push(temp_r); } int temp_d = temp + 1000; if (temp_d / 1000 != n && arr[temp_d / 1000][temp_d % 1000] == 0) { arr[temp_d / 1000][temp_d % 1000] = 9; q.push(temp_d); } int temp_l = temp - 1; if (temp % 1000 != 0 && arr[temp_l / 1000][temp_l % 1000] == 0) { arr[temp_l / 1000][temp_l % 1000] = 9; q.push(temp_l); } int temp_u = temp - 1000; if (temp / 1000 != 0 && arr[temp_u / 1000][temp_u % 1000] == 0) { arr[temp_u / 1000][temp_u % 1000] = 9; q.push(temp_u); } } for (int i = 1; i < n - 1; i++) { for (int j = 1; j < m - 1; j++) { if (arr[i][j] == 1 && arr[i - 1][j] + arr[i + 1][j] + arr[i][j - 1] + arr[i][j + 1] >= 18) { arr[i][j] = 0; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (arr[i][j] == 9) { arr[i][j] = 0; } } } } printf("%d\n", count); }
댓글을 작성하려면
로그인
해야 합니다.
invrtd_h 2년 전
분명 어딘가에서 메모리 누수가 발생하고 있는 것 같은데 그게 어딘지를 모르겠습니다...