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인칭 시점으로 써내려가는 글들

인기 글

태그

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

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ShovelingLife

A Game Programmer

코딩테스트/백준

[골5] 7576 - 토마토

2022. 7. 4. 20:55
#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

// 상 하 좌 우
int_pair arr_pos[]
{
    { 1, 0},
    {-1, 0},
    {0, -1},
    {0, 1}
};

vector<vector<int>> graph;

// w:넓이, h:높이, k:테스트 개수
int w, h, k;
queue<int_pair> q;

void BFS()
{
    while (!q.empty())
    {
        auto front = q.front(); q.pop();

        for (int i = 0; i < 4; i++)
        {
            int ny = front.y + arr_pos[i].y;
            int nx = front.x + arr_pos[i].x;

            if (nx < 0 ||
                nx >= w ||
                ny < 0 ||
                ny >= h)
                continue;

            if (graph[ny][nx] == 0)
            {
                q.push({ ny,nx });
                graph[ny][nx] = graph[front.y][front.x] + 1;
            }
        }
    }
}

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

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

    for (int y = 0; y < h; y++)
    {
        graph[y].resize(w);

        for (int x = 0; x < graph[y].size(); x++)
        {
            cin >> graph[y][x];

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

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

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

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

    티스토리툴바