From c47ad4c84e3c4c87795a1d2999ecc3ec0a7daa5f Mon Sep 17 00:00:00 2001 From: sh0723 Date: Sat, 27 Jun 2026 12:50:26 +0900 Subject: [PATCH] =?UTF-8?q?3197=20:=20=EB=B0=B1=EC=A1=B0=EC=9D=98=20?= =?UTF-8?q?=ED=98=B8=EC=88=98=20resolve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/week3/BOJ3197/BOJ3197_V2.cpp | 80 ++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/week3/BOJ3197/BOJ3197_V2.cpp diff --git a/src/week3/BOJ3197/BOJ3197_V2.cpp b/src/week3/BOJ3197/BOJ3197_V2.cpp new file mode 100644 index 0000000..bcd6dbc --- /dev/null +++ b/src/week3/BOJ3197/BOJ3197_V2.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; +queue> swan; +queue> water; + +bool water_visited[1500][1500]; +bool swan_visited[1500][1500]; +char inp[1500][1500]; +int dy[4] = {0, 1, 0, -1}; +int dx[4] = {1, 0, -1, 0}; +int R,C; +int main() { + + cin >> R >> C; + bool find_swan = false; + for (int i = 0; i < R; i++) { + string s; + cin >> s; + for (int j = 0; j < C; j++) { + inp[i][j] = s[j]; + if (s[j] != 'X') { + water.push({i, j}); + water_visited[i][j] = true; + } + if (s[j] == 'L' && !find_swan) { + swan.push({i, j}); + find_swan = true; + inp[i][j] = '.'; + swan_visited[i][j] = true; + } + } + } + int cnt = 0; + while(true) { + queue> next_swan; + queue> next_water; + while(!swan.empty()) { + pair curr_swan = swan.front(); + swan.pop(); + if (inp[curr_swan.first][curr_swan.second] == 'L') { + cout << cnt << '\n'; + return 0; + } + for (int i = 0; i < 4; i++) { + int ny = curr_swan.first + dy[i]; + int nx = curr_swan.second + dx[i]; + + if (ny < 0 || nx < 0 || ny >= R || nx >= C || swan_visited[ny][nx]) continue; + if (inp[ny][nx] == 'X') { + next_swan.push({ny, nx}); + } else { + swan.push({ny, nx}); + } + swan_visited[ny][nx] = true; + } + } + while(!water.empty()) { + pair curr_water = water.front(); + water.pop(); + for (int i = 0; i < 4; i++) { + int ny = curr_water.first + dy[i]; + int nx = curr_water.second + dx[i]; + + if (ny < 0 || nx < 0 || ny >= R || nx >= C || water_visited[ny][nx]) continue; + if (inp[ny][nx] == 'X') { + next_water.push({ny, nx}); + inp[ny][nx] = '.'; + } else { + water.push({ny, nx}); + } + water_visited[ny][nx] = true; + } + } + cnt++; + swan = next_swan; + water = next_water; + } + + return 0; +} \ No newline at end of file