Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10-dhlee777 #47

Merged
merged 1 commit into from
May 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 λΆ€λΆ„ μƒκΉ€μƒˆκ°€ κΈ°λ¬˜ν•΄μ„œ 쑰금 μ΄ν•΄ν•˜λŠ”λ° μ‹œκ°„μ΄ κ±Έλ Έλ„€μš”... 이런 뢀뢄은 μ‹€ν–‰ν•  ν•¨μˆ˜λ₯Ό λ°–μœΌλ‘œ λΉΌκ³  값에 λŒ€ν•΄μ„œλ§Œ 삼항연산을 ν•˜λ©΄ 가독성이 μ’‹μ•„μ§ˆ 것 κ°™μ•„μš”!

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ•„ ν•¨μˆ˜λ§Œ λ°–μœΌλ‘œ λΊ„μˆ˜λ„ μžˆμ—ˆκ΅°μš”.. μ½”λ“œκ°€ ν›¨μ”¬κΉ”λ”ν•΄μ§€λŠ”κ±° κ°™μŠ΅λ‹ˆλ‹€. μž˜μ¨λ¨Ήκ² μŠ΅λ‹ˆλ‹€ !


}