diff --git a/flydongwoo/AlgoLeadMe_Week21_prob01.cpp b/flydongwoo/AlgoLeadMe_Week21_prob01.cpp new file mode 100644 index 0000000..a9074c5 --- /dev/null +++ b/flydongwoo/AlgoLeadMe_Week21_prob01.cpp @@ -0,0 +1,75 @@ +#include +#include +#include + +#define MAX_SIZE 1000 + 1 + +using namespace std; + +struct tomato { + int y, x; +}; + +queue q; + +// ¿ì,ÇÏ,ÁÂ,»ó +int dx[4] = { 1, 0, -1, 0 }; +int dy[4] = { 0, 1, 0, -1 }; + +int n, m, result = 0; +int graph[MAX_SIZE][MAX_SIZE]; + +bool IsInside(int ny, int nx) { + return (0 <= nx && 0 <= ny && nx < m && ny < n); +} + +void bfs(void) { + while (!q.empty()) { + int y = q.front().y; + int x = q.front().x; + q.pop(); + + for (int i = 0; i < 4; i++) { + int ny = y + dy[i]; + int nx = x + dx[i]; + + if (IsInside(ny, nx) == 1 && graph[ny][nx] == 0) { + graph[ny][nx] = graph[y][x] + 1; + q.push({ ny, nx }); + } + } + } +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> m >> n; + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + cin >> graph[i][j]; + if (graph[i][j] == 1) { + q.push({ i, j }); + } + } + } + + bfs(); + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (graph[i][j] == 0) { + cout << -1 << "\n"; + return 0; + } + if (result < graph[i][j]) { + result = graph[i][j]; + } + } + } + + cout << (result - 1) << endl; + return 0; +} diff --git a/flydongwoo/README.md b/flydongwoo/README.md index 3e5cafc..3c025a9 100644 --- a/flydongwoo/README.md +++ b/flydongwoo/README.md @@ -21,5 +21,6 @@ | 17차시 | 2025.08.06 | DFS와 BFS | [ë°”ì´ëŸ¬ìФ](https://www.acmicpc.net/problem/2606)|https://github.com/AlgoLeadMe/AlgoLeadMe-15/pull/63| | 18차시 | 2025.08.07 | BFS | [숨바꼭질](https://www.acmicpc.net/problem/1697)|https://github.com/AlgoLeadMe/AlgoLeadMe-15/pull/64| | 19차시 | 2025.08.14 | Sliding Window | [수열](https://www.acmicpc.net/problem/2559)|https://github.com/AlgoLeadMe/AlgoLeadMe-15/pull/71| -| 20차시 | 2025.08.27 | Greedy Algorithm | [ATM](https://www.acmicpc.net/problem/2559)|https://github.com/AlgoLeadMe/AlgoLeadMe-15/pull/81| +| 20차시 | 2025.08.27 | Greedy Algorithm | [ATM](https://www.acmicpc.net/problem/11399)|https://github.com/AlgoLeadMe/AlgoLeadMe-15/pull/81| +| 21차시 | 2025.08.28 | BFS | [토마토](https://www.acmicpc.net/problem/7576)|https://github.com/AlgoLeadMe/AlgoLeadMe-15/pull/82|