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.

아 함수만 밖으로 뺄수도 있었군요.. 코드가 훨씬깔끔해지는거 같습니다. 잘써먹겠습니다 !


}