diff --git a/src/week3/BOJ3197/BOJ3197_V1.cpp b/src/week3/BOJ3197/BOJ3197_V1.cpp new file mode 100644 index 0000000..61693ac --- /dev/null +++ b/src/week3/BOJ3197/BOJ3197_V1.cpp @@ -0,0 +1,87 @@ +#include +using namespace std; +queue> swan; +queue> water; +int day; +int dy[4] = {0,0,1,-1}; +int dx[4] = {1,-1,0,0}; +int water_visited[1501][1501]; +int swan_visited[1501][1501]; +char lake_map[1501][1501]; +int main() { + + int N,M; + cin >> M >> N; + bool swan_once = false; + for (int i=1; i<=M; i++) { + string s; + cin >> s; + for (int j=1; j<=N; j++) { + lake_map[i][j] = s[j-1]; + if (lake_map[i][j] == 'L' && !swan_once) { + swan_once = true; + swan_visited[i][j] = 1; + swan.push({i, j}); + lake_map[i][j] = '.'; + } else if (lake_map[i][j] != 'X') + { + water.push({i,j}); + water_visited[i][j] = 1; + } + } + } + + while(true) { + queue> next_swan; + queue> next_water; + + while(!swan.empty()) { + pair curr_swan = swan.front(); + swan.pop(); + + if (lake_map[curr_swan.first][curr_swan.second] == 'L') { + cout << day << '\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 < 1 || ny > M || nx < 1 || nx > N) continue; + if (swan_visited[ny][nx]) continue; + + if (lake_map[ny][nx] == 'X') { + next_swan.push(make_pair(ny, nx)); + } else { + swan.push(make_pair(ny, nx)); + } + swan_visited[ny][nx] = 1; + } + } + + 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 < 1 || ny > M || nx < 1 || nx > N) continue; + if (water_visited[ny][nx]) continue; + if (lake_map[ny][nx] == 'X') { + next_water.push(make_pair(ny, nx)); + lake_map[ny][nx] = '.'; + } + water_visited[ny][nx] = 1; + } + } + + swan = next_swan; + water = next_water; + day++; + } + + + + return 0; +} \ No newline at end of file