ShovelingLife
A Game Programmer
ShovelingLife
전체 방문자
오늘
어제
  • 분류 전체보기 (1072)
    • 그래픽스 (57)
      • 공통 (19)
      • 수학 물리 (22)
      • OpenGL & Vulkan (1)
      • DirectX (14)
    • 게임엔진 (183)
      • Unreal (69)
      • Unity (103)
      • Cocos2D-X (3)
      • 개인 플젝 (8)
    • 코딩테스트 (221)
      • 공통 (7)
      • 프로그래머스 (22)
      • 백준 (162)
      • LeetCode (19)
      • HackerRank (2)
      • 코딩테스트 알고리즘 (8)
    • CS (235)
      • 공통 (21)
      • 네트워크 (44)
      • OS & 하드웨어 (55)
      • 자료구조 & 알고리즘 (98)
      • 디자인패턴 (6)
      • UML (4)
      • 데이터베이스 (7)
    • 프로그래밍 언어 (348)
      • C++ (167)
      • C# (90)
      • Java (9)
      • Python (33)
      • SQL (30)
      • JavaScript (8)
      • React (7)
    • 그 외 (9)
      • Math (5)
      • 일상 (5)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

  • Source Code 좌측 상단에 복사 버튼 추가 완료
  • 언리얼 엔진 C++ 빌드시간 단축 꿀팁
  • 게임 업계 코딩테스트 관련
  • 1인칭 시점으로 써내려가는 글들

인기 글

태그

  • 문자열
  • Unity
  • 언리얼
  • 오블완
  • C++
  • 포인터
  • 티스토리챌린지
  • 알고리즘
  • 함수
  • 백준
  • 유니티
  • SQL
  • 그래픽스
  • 배열
  • 프로그래머스
  • C
  • string
  • 파이썬
  • 클래스
  • c#

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ShovelingLife

A Game Programmer

코딩테스트/백준

[골5] 7569 - 토마토

2022. 7. 4. 22:53
#include <iostream>
#include <algorithm>
#include <exception>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <utility>

using namespace std;
using int_pair = pair<int, int>;

#define SIZE 1000
#define y first
#define x second

// 윗층 뒤 우 아래층 잎 좌
pair<int, int_pair> arr_pos[]
{
    {1, {0, 0}},
    {0, {-1, 0}},
    {0, {0, 1}},
    {-1, {0, 0}},
    {0, {1, 0}},
    {0, {0, -1}}
};

vector<vector<vector<int>>> graph;

// w:넓이, h:높이, l:층수
int w, h, l;
queue<pair<int,int_pair>> q;

void BFS()
{
#define ARR_POS_SIZE 6
    while (!q.empty())
    {
        auto front = q.front(); q.pop();
        //cout << front.y << front.x.y << front.x.x << endl;

        for (int i = 0; i < ARR_POS_SIZE; i++)
        {
            auto first = front.x.y, second = front.x.x;
            auto nz = front.y + arr_pos[i].y; // 다음 층
            int ny = first + arr_pos[i].x.y;  // 다음 앞 뒤
            int nx = second + arr_pos[i].x.x; // 다음 좌 우

            // 범위 체크
            if (nx < 0 ||
                nx >= w ||
                ny < 0 ||
                ny >= h ||
                nz < 0 ||
                nz >= l)
                continue;

            // 토마토가 존재
            if (graph[nz][ny][nx] == 0)
            {
                q.push({ nz, { ny,nx} });
                graph[nz][ny][nx] = graph[front.y][first][second] + 1;
            }
        }
    }
}

int main()
{
    cin >> w >> h >> l;

    // 그래프 입력
    graph.resize(l);

    for (int i = 0; i < l; i++)
    {
        graph[i].resize(h);
        for (int y = 0; y < h; y++)
        {
            graph[i][y].resize(w);
            for (int x = 0; x < graph[i][y].size(); x++)
            {
                cin >> graph[i][y][x];

                if (graph[i][y][x] == 1)
                    q.push({ i, { y,x } });
            }
        }
    }
    BFS();
    int result = 0;

    for (int i = 0; i < l; i++)
    {
        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
                // 익지않은 토마토가 존재할 경우
                if (graph[i][y][x] == 0)
                {
                    cout << -1;
                    return 0;
                }
                // 익은 토마토일 시
                result = max(result, graph[i][y][x]);
            }
        }
    }
    cout << result - 1;
    return 0;
}
저작자표시 (새창열림)

'코딩테스트 > 백준' 카테고리의 다른 글

[실3] 15650 - n과 m(2)  (0) 2022.07.05
[실3] 15649 - n과 m(1)  (0) 2022.07.05
[골5] 7576 - 토마토  (0) 2022.07.04
[골3] 2812 - 크게 만들기  (0) 2022.07.01
[플5] 16496 - 큰 수 만들기  (0) 2022.07.01
    '코딩테스트/백준' 카테고리의 다른 글
    • [실3] 15650 - n과 m(2)
    • [실3] 15649 - n과 m(1)
    • [골5] 7576 - 토마토
    • [골3] 2812 - 크게 만들기
    ShovelingLife
    ShovelingLife
    Main skill stack => Unity C# / Unreal C++ Studying Front / BackEnd, Java Python

    티스토리툴바