구글에 다른분들이 푼문제들을 살펴봐도 제가 푼것이랑 차이가 있어보이지 않는데


어디서 메모리를 더 사용하는지 모르겠습니다.


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


#include <bits/stdc++.h>
using namespace std;

int dx[4] = { -1, 1, 0, 0 };
int dy[4] = { 0, 0, -1, 1 };

char board[1001][1001];
int dist1[1001][1001]; // 화르륵
int dist2[1001][1001]; // 상근쓰
int w, h;

int main() {
    cout.sync_with_stdio(0);
    cin.tie(0);
    int t; cin >> t;
    while (t--) {
        bool flag = false;
        cin >> w >> h;
        for (int i = 0; i < h; i++) {
            fill(dist1[i], dist1[i] + w, -1);
            fill(dist2[i], dist2[i] + w, -1);
        }
        queue<pair<int, int>> Q1;
        queue<pair<int, int>> Q2;

        for (int i = 0; i < h; i++) {
            string input; cin >> input;
            for (int j = 0; j < w; j++) {
                board[i][j] = input[j];
                if (board[i][j] == '*') {
                    dist1[i][j] = 0;
                    Q1.push({ i, j });
                }
                if (board[i][j] == '@') {
                    dist2[i][j] = 0;
                    Q2.push({ i, j });
                }
            }
        }

        while (!Q1.empty()) {
            auto cur = Q1.front(); Q1.pop();
            for (int i = 0; i < 4; i++) {
                int nx = cur.first + dx[i];
                int ny = cur.second + dy[i];
                if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
                if (board[nx][ny] == '#' || dist1[nx][ny] >= 0) continue;
                dist1[nx][ny] = dist1[cur.first][cur.second] + 1;
                Q1.push({ nx, ny });
            }
        }

        while (!Q2.empty()) {
            auto cur = Q2.front(); Q2.pop();
            for (int i = 0; i < 4; i++) {
                int nx = cur.first + dx[i];
                int ny = cur.second + dy[i];
                if (nx < 0 || nx >= h || ny < 0 || ny >= w) {
                    cout << dist2[cur.first][cur.second] + 1 << '\n';
                    flag = true;
                    break;
                }
                if (board[nx][ny] == '#' || dist1[nx][ny] <= dist2[cur.first][cur.second] + 1) continue;
                dist2[nx][ny] = dist2[cur.first][cur.second] + 1;
                Q2.push({ nx, ny });
            }
            if (flag) break;
        }
        if(!flag) cout << "IMPOSSIBLE" << '\n';
    }

}