Skip to content

Commit

Permalink
Merge pull request #47 from AlgoLeadMe/10-dhlee777
Browse files Browse the repository at this point in the history
10-dhlee777
  • Loading branch information
dhlee777 authored May 19, 2024
2 parents bd75fbf + 1d687de commit 3ded618
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions dhlee777/bfs/ν† λ§ˆν† .cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include<iostream>
#include<queue>
using namespace std;
int tom[101][101][101]; //ν† λ§ˆν† μ˜ μƒνƒœλ₯Ό μ €μž₯ν•˜κΈ°μœ„ν•œ λ°°μ—΄
int col, row, height;
int x[6] = { 0,0,-1,1,0,0 };
int y[6] = { -1,1,0,0,0,0 };
int z[6] = { 0,0,0,0,1,-1 };
struct tomato { //ν† λ§ˆν† μ˜ μ’Œν‘œλ₯Ό μ €μž₯ν•˜κΈ°μœ„ν•œ ꡬ쑰체
int x1; //μ—΄
int y1; //ν–‰
int z1; //높이
tomato(int _x1, int _y1, int _z1) {
x1 = _x1; y1 = _y1; z1 = _z1;

}
};
queue<tomato>q;
void bfs() {
while (!q.empty()) {
tomato t = q.front();
q.pop();
for (int i = 0; i < 6; i++) { //νƒμƒ‰λ²”μœ„(상,ν•˜,쒌,우,μœ„,μ•„λž˜)
int _x = t.x1 + x[i];
int _y = t.y1 + y[i];
int _z = t.z1 + z[i];
if (_x < col && _x >= 0 && _y < row && _y >= 0 && _z < height && _z >= 0 && tom[_z][_y][_x] == 0) {
tom[_z][_y][_x] = tom[t.z1][t.y1][t.x1] + 1;
q.push(tomato(_x, _y, _z));

}
}


}

}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> col >> row >> height;

for (int i = 0; i < height; i++) {
for (int j = 0; j < row; j++) {
for (int k = 0; k < col; k++) {
cin >> tom[i][j][k];
if (tom[i][j][k] == 1) //μ΅μ€ν† λ§ˆν† κ°€ λ“€μ–΄μžˆμœΌλ©΄
q.push(tomato(k, j, i)); //νƒμƒ‰μ‹œμž‘μ μ„μœ„ν•΄ κ·Έ μ’Œν‘œλ₯Ό 큐에 λ„£λŠ”λ‹€.
}
}
}

bfs(); //
int sum = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < row; j++) {
for (int k = 0; k < col; k++) {
if (tom[i][j][k] == 0) //bfsν›„ μ•ˆμ΅μ€ν† λ§ˆν† κ°€ μžˆλ‹€λ©΄ λͺ¨λ‘μ΅μ§€λͺ»ν•˜λŠ” μƒν™©μ΄λ―€λ‘œ -1을 좜λ ₯
{
cout << -1;
return 0;
}
if (sum < tom[i][j][k]) sum = tom[i][j][k]; //bfs ν›„ κ±Έλ¦°μ‹œκ°„μ„ μ°ΎκΈ°μœ„ν•΄ tomλ°°μ—΄μ˜ μ΅œλŒ€κ°’μ„ μ°Ύμ•„ sum에 μ €μž₯ν•˜λŠ”κ³Όμ •

}
}
}

sum == 1 ? cout << 0 : cout << sum - 1;

}

0 comments on commit 3ded618

Please sign in to comment.