-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from AlgoLeadMe/10-dhlee777
10-dhlee777
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |