구글에 다른분들이 푼문제들을 살펴봐도 제가 푼것이랑 차이가 있어보이지 않는데
어디서 메모리를 더 사용하는지 모르겠습니다.
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';
}
}
Q2 에 데이터가 무한히 쌓이고있는듯 합니다. Q1은 멀쩡할거 같은데., Q1과 Q2를 본인이 어떻게 다르게 짰는지 확인해보세용
맞네요... 중복해서 넣는 데이터가 많았군요 ㅠㅠ 감사합니다