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

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

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

인기 글

태그

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

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ShovelingLife

A Game Programmer

코딩테스트/백준

[골4] 14502 - 연구소

2022. 12. 12. 12:24

참고해서 코드 작성

#include <iostream>
#include <vector>
#include <queue>
#include <map>

#define y first
#define x second

using namespace std;
using IntPair = pair<int, int>;
using TwoDVec = vector<vector<int>>;

IntPair dir[]
{
    {  1, 0 },
    {  0, 1 },
    { -1, 0 },
    {  0, -1 }
};

TwoDVec m, vm;
vector<IntPair> vis;
int h, w, ans = 0;

// 바이러스 지도 초기화 함수
void SetMap()
{
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
            vm[i][j] = m[i][j];
    }
}

void SpreadVirus()
{
    queue<IntPair> q;

    // 위치 초기화
    for (int i = 0; i < vis.size(); i++)
        q.push(vis[i]);

    // BFS
    while (!q.empty())
    {
        auto front = q.front(); q.pop();
        int y = front.y, x = front.x;
        
        for (int i = 0; i < 4; i++)
        {
            int ny = y + dir[i].y, nx = x + dir[i].x;

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

            if (!vm[ny][nx])
            {
                vm[ny][nx] = 2;
                q.push({ ny,nx });
            }
        }
    }
}

// 안전 지역의 개수를 구함
int GetSafeZone()
{
    int res = 0;
    
    for (int y = 0; y < h; y++)
    {
    	for (int x = 0; x < w; x++)
    	{
            if (!vm[y][x])
                res++;
    	}
    }
    return res;
}

void DFS(int row, int cnt)
{
    if (cnt == 3)
    {
        SetMap();
        SpreadVirus();
        ans = max(ans, GetSafeZone());
        return;
    }
    // DFS
    for (int y = 0; y < h; y++)
    {
    	for (int x = 0; x < w; x++)
    	{
            // 백트래킹
            if (!m[y][x])
            {
                m[y][x] = 1;
                DFS(y, cnt + 1);
                m[y][x] = 0;
            }
    	}
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    cin >> h >> w;
    m = TwoDVec(h + 1, vector<int>(w + 1));
    vm = TwoDVec(m);

    for (int y = 0; y < h; y++)
    {
    	for (int x = 0; x < w; x++)
    	{
            cin >> m[y][x];

            if (m[y][x] == 2)
                vis.push_back({ y,x });
    	}
    }
    DFS(0, 0);
    cout << ans << endl;
    return 0;
}
저작자표시 (새창열림)

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

[골5] 1916 - 최소 비용 구하기  (0) 2022.12.19
[골4] 1753 - 최단 경로  (0) 2022.12.14
[실1] 1074 - Z  (0) 2022.12.07
[실1] 1697 - 숨바꼭질  (0) 2022.12.07
[골5] 2170 - 선 긋기  (0) 2022.11.23
    '코딩테스트/백준' 카테고리의 다른 글
    • [골5] 1916 - 최소 비용 구하기
    • [골4] 1753 - 최단 경로
    • [실1] 1074 - Z
    • [실1] 1697 - 숨바꼭질
    ShovelingLife
    ShovelingLife
    Main skill stack => Unity C# / Unreal C++ Studying Front / BackEnd, Java Python

    티스토리툴바