diff --git a/src/week3/BOJ4179/BOJ4179_V1.cpp b/src/week3/BOJ4179/BOJ4179_V1.cpp new file mode 100644 index 0000000..796b4a2 --- /dev/null +++ b/src/week3/BOJ4179/BOJ4179_V1.cpp @@ -0,0 +1,90 @@ +#include +using namespace std; +int N,M; +int dy[4] = {0,0,1,-1}; +int dx[4] = {1,-1,0,0}; +int min_exit = INT_MAX; +vector> inp_map; +vector> fires_map; +vector>> fires; +vector> visited; +bool can_exit = false; +void fire_flood(int level) { + for (int f=0; f= 0 && nx >= 0 && ny < N && nx < M && inp_map[ny][nx] != 0 && inp_map[ny][nx] != 2) { + fires_map[ny][nx] = 1; + fires[level+1].push_back(make_pair(ny,nx)); + } + } + } +} +void return_flood(int level) { + for (int f=0; f>()); + fire_flood(level); + for (int i=0; i<4; i++) { + int ny = y + dy[i]; + int nx = x + dx[i]; + if (ny >= 0 && nx >= 0 && ny < N && nx < M && inp_map[ny][nx] != 0) { + if (inp_map[ny][nx] == 1 && visited[ny][nx] == 0) { + dfs(ny, nx, level+1); + } + } + } + return_flood(level+1); + fires.pop_back(); + +} +int main() { + + ios_base::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + fires.push_back(vector>()); + cin >> N; + cin >> M; + int sty, stx; + for (int i=0; i()); + fires_map.push_back(vector(M,0)); + visited.push_back(vector(M,0)); + string temp; + cin >> temp; + for (int j=0; j +using namespace std; +int N,M; +int dy[4] = {0,0,1,-1}; +int dx[4] = {1,-1,0,0}; +vector> inp_map; +int fires[1004][1004]; +int human[1004][1004]; +queue> q; +int ret, sty,stx; +int max_val = INT_MAX; +int main() { + + cin >> N >> M; + fill(&fires[0][0], &fires[0][0] + 1004 * 1004, max_val); + + for (int i = 0; i < N; i++) { + string s; + cin >> s; + inp_map.push_back(vector()); + for (int j = 0; j < M; j++) { + if (s[j] == 'J') { + inp_map[i].push_back(s[j]); + sty = i, stx = j; + } else if (s[j] == 'F') { + fires[i][j] = 1; + inp_map[i].push_back(s[j]); + q.push(make_pair(i,j)); + } else { + inp_map[i].push_back(s[j]); + } + } + } + + while(!q.empty()) { + int y, x; + tie(y,x) = q.front(); + q.pop(); + for (int i=0; i<4; i++) { + int ny = y+dy[i]; + int nx = x+dx[i]; + if (ny >= N || nx >= M || ny < 0 || nx < 0) continue; + if (fires[ny][nx] != max_val || inp_map[ny][nx] == '#') continue; + fires[ny][nx] = fires[y][x] + 1; + q.push(make_pair(ny,nx)); + } + } + + human[sty][stx] = 1; + q.push(make_pair(sty,stx)); + while(!q.empty()) { + int y,x; + tie(y,x) = q.front(); + q.pop(); + if (y == N-1 || x == M-1 || y == 0 || x == 0) { + ret = human[y][x]; + break; + } + for (int i=0; i<4; i++) { + int ny = y+dy[i]; + int nx = x+dx[i]; + if (ny >= N || nx >= M || ny < 0 || nx < 0) continue; + if (human[ny][nx] || inp_map[ny][nx] == '#') continue; + if (fires[ny][nx] <= human[y][x] + 1) continue; + human[ny][nx] = human[y][x] + 1; + q.push(make_pair(ny,nx)); + } + } + + if (ret == 0) cout << "IMPOSSIBLE"; + else cout << ret; + + + return 0; +} \ No newline at end of file