7569번: 토마토
첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100,
www.acmicpc.net
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
#define MAX_GRAPH_SIZE 100
#define ALL_DIRECTION 6
// 스택 오버플로우 방지
int _arrGraph[MAX_GRAPH_SIZE][MAX_GRAPH_SIZE][MAX_GRAPH_SIZE];
namespace Tomato
{
struct Coordinate
{
int height;
int row;
int col;
};
class BFS
{
private:
//초기 값
int arrDirections[ALL_DIRECTION][ALL_DIRECTION / 2] = { {1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0},
{0, 0, 1}, {0, 0, -1} };
int depthCnt;
// 입력 값
int heightNum;
int rowNum;
int colNum;
void Init()
{
memset(_arrGraph, 0, sizeof(_arrGraph));
this->depthCnt = 0;
}
void Input()
{
cin >> this->colNum >> this->rowNum >> this->heightNum;
for (int i = 0; i < this->heightNum; i++)
{
for (int j = 0; j < this->rowNum; j++)
{
for (int k = 0; k < this->colNum; k++)
{
int tempNode;
cin >> tempNode;
_arrGraph[i][j][k] = tempNode;
}
}
}
}
void Solve()
{
queue<Coordinate> qBuffer;
bool isFull = true;
for (int i = 0; i < this->heightNum; i++)
{
for (int j = 0; j < this->rowNum; j++)
{
for (int k = 0; k < this->colNum; k++)
{
if (_arrGraph[i][j][k] == 1)
{
qBuffer.push({ i, j, k });
}
if (isFull && _arrGraph[i][j][k] == 0)
{
isFull = false;
}
}
}
}
if (isFull)
{
return;
}
while (!qBuffer.empty())
{
int depthSize = qBuffer.size();
for (int i = 0; i < depthSize; i++)
{
Coordinate curCoord = qBuffer.front();
qBuffer.pop();
for (int j = 0; j < ALL_DIRECTION; j++)
{
int curHeight = curCoord.height + this->arrDirections[j][0];
int curRow = curCoord.row + this->arrDirections[j][1];
int curCol = curCoord.col + this->arrDirections[j][2];
if (curCol >= 0 && curCol < this->colNum &&
curRow >= 0 && curRow < this->rowNum &&
curHeight >= 0 && curHeight < this->heightNum &&
_arrGraph[curHeight][curRow][curCol] == 0)
{
qBuffer.push({ curHeight, curRow, curCol });
_arrGraph[curHeight][curRow][curCol] = 1;
}
}
}
this->depthCnt++;
}
for (int i = 0; i < this->heightNum; i++)
{
for (int j = 0; j < this->rowNum; j++)
{
for (int k = 0; k < this->colNum; k++)
{
if (_arrGraph[i][j][k] == 0)
{
this->depthCnt = -1;
return;
}
}
}
}
this->depthCnt--;
}
void Output()
{
cout << this->depthCnt;
}
public:
BFS()
{
this->Init();
this->Input();
this->Solve();
this->Output();
}
};
}
int main()
{
Tomato::BFS();
}
리팩토링 필요
'Algorithm > C++ BOJ' 카테고리의 다른 글
C++) [BOJ] 8980 택배 (0) | 2024.03.02 |
---|---|
C++) [BOJ] 2644 촌수계산 (0) | 2024.02.12 |
C++) [BOJ] 2667 단지번호붙이기 (0) | 2024.01.28 |
C++) [BOJ] 2606 바이러스 (1) | 2024.01.22 |
C++) [BOJ] 11726 2×n 타일링 (0) | 2023.07.19 |