반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- BFS
- OFFSET
- 물채우기
- @P0
- 연결요소
- 13908
- incr
- 오퍼레터
- 백준
- 기술면접
- msSQL
- 성적평가
- boj #19237 #어른 상어
- dfs
- 6987
- BOJ
- 퇴사통보
- softeer
- 파라메트릭
- 매개변수탐색
- upper_bound
- 소프티어
- 처우산정
- Docker
- 경력
- compose
- 백트래킹
- Kafka
- 처우협의
- 이분탐색
Archives
- Today
- Total
기술 블로그
4179번 불! 본문
728x90
반응형
문제 : https://www.acmicpc.net/problem/4179
테스트 케이스 : http://acm.student.cs.uwaterloo.ca/~acm00/090613/data/
참고로 B번이다.
기본적인 유형의 BFS이다.
틀리면 안 된다.
핵심은 지훈이와 불에 대한 방문 처리를 각각 따로 해준다.
그리고 return 할 때, 그 상태(p==0)가 지훈이인지도 조건문에 넣어주면 된다.
참고로 불을 먼저 큐에 push해준다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> using namespace std; int R = 0, C = 0, jy = 0, jx = 0; int dy[4] = { 0, -1, 0, 1 }; int dx[4] = { -1, 0, 1, 0 }; char Map[1001][1001]; queue<pair< pair<int, int>, int > > q; // 0 = 지훈, 1 = 불 bool Fire[1001][1001] = { false, }; bool Jee[1001][1001] = { false, }; int BFS() { // 지훈 q.push({ { jy, jx }, 0 }); Jee[jy][jx] = true; int ret = 1; while (!q.empty()) { int qSize = q.size(); while (qSize--) { int y = q.front().first.first; int x = q.front().first.second; int p = q.front().second; q.pop(); if ((x == 0 || x == C - 1 || y == 0 || y == R - 1) && p == 0) { return ret; } if (p == 0) // 지훈 { for (int i = 0; i < 4; i++) { int ny = y + dy[i]; int nx = x + dx[i]; if (ny < 0 || ny >= R || nx < 0 || nx >= C || Jee[ny][nx]) continue; if (Map[ny][nx] == '.') { q.push({ { ny, nx }, 0 }); Jee[ny][nx] = true; } } } else if (p == 1) // 불 { for (int i = 0; i < 4; i++) { int ny = y + dy[i]; int nx = x + dx[i]; if (ny < 0 || ny >= R || nx < 0 || nx >= C || Fire[ny][nx]) continue; if (Map[ny][nx] == '.') { q.push({ { ny, nx }, 1 }); Fire[ny][nx] = true; Map[ny][nx] = 'F'; } } } } ++ret; } return -1; } int main(void) { scanf("%d %d", &R, &C); for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { cin >> Map[i][j]; if (Map[i][j] == 'J') { jy = i; jx = j; Map[i][j] = '.'; } else if (Map[i][j] == 'F') { q.push({ { i, j }, 1 }); // 불 Fire[i][j] = true; } } } int r = BFS(); if (r != -1) printf("%d\n", r); else printf("IMPOSSIBLE\n"); return 0; } | cs |
728x90
반응형
'알고리즘 문제 > BOJ' 카테고리의 다른 글
16724번 피리 부는 사나이 (0) | 2019.01.01 |
---|---|
16724번 원영이는 ZOAC과 영원하고 싶다 (0) | 2018.12.31 |
1019번 책 페이지 (0) | 2018.12.29 |
1120번 문자열 (0) | 2018.12.29 |
10448번 유레카 이론 (0) | 2018.12.29 |