본문 바로가기

Problem/BFS

[백준알고리즘] 2178번 미로탐색

https://www.acmicpc.net/problem/2178




시간 초과 때문에 10분 버리고

(dfs -> bfs 로 변경)


메모리 초과 때문에 10분 버렸다..

(문자열 동적할당 -> 문자배열로 선언, ==> 포인터 및 할당했던 무작위 공간의 많은 메모리 제거! 최대 char*[101] 100개 ==> char[101]로 대체105*100 = 10500byte ==> 101byte

이거 free하면 되는데 왜 free가 잘 안되었는지.. free 하는데 왜 오류가 날까.... 그래서 걍 내놨더니 메모리 많이 잡앗어.....


visited 배열 -> arr 배열 내에서 방문 여부 판단) ==> char[101][101] 제거 ==> 10201byte 절약



최대 20600byte 절약 ==> 20KB 정도?


엥 그렇게 많이 안들었는데...

HEAP 메모리 영역 사용하면 메모리 초과날 확률이 높은 건강...?.... 잘 모르겠다.....



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<stdio.h>
#include<queue>
#include<iostream>
using namespace std;
 
queue <pair<intint>>q;
 
int answer_min = 0;
char arr[101][101];
char s[101];
int dx[4= { 0,0,-1,1 };
int dy[4= { 1,-1,0,0 };
 
int main()
{
    int N, M;
 
    cin >> N >> M;
 
    for (int i = 0; i < N; i++)
    {
        cin >> s;
 
        for (int j = 0; j < M; j++)
        {
            arr[i][j] = s[j];
        }
    }
 
    q.push(make_pair(00));
 
    while (1)
    {
        int sz = q.size();
        answer_min++;
 
        while (sz--)
        {
            pair<intint>= q.front();
            q.pop();
            int _r = p.first;
            int _c = p.second;
 
            arr[_r][_c] = '1';
 
            if (_r == N - 1 && _c == M - 1)
            {
                cout << answer_min;
                return 0;
            }
 
            for (int i = 0; i < 4; i++)
            {
                int mx = _r + dx[i];
                int my = _c + dy[i];
 
                if (mx >= 0 && mx < N && my >= 0 && my < M && arr[mx][my] == '1')
                {
                    q.push(make_pair(mx, my));
                    arr[mx][my] = '0';
                }
            }
        }
    }
}
cs