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

인기 글

태그

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

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ShovelingLife

A Game Programmer

코딩테스트/백준

[골5] 15686 - 치킨 배달

2022. 12. 22. 13:34
#include <limits.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

using IntPair = pair<int, int>;

#define y first
#define x second

vector<vector<int>> board;
vector<IntPair> house, chicken;
int m, n, res = 0;
bool vis[13];

int distance(IntPair a, IntPair b) { return abs(a.y - b.y) + abs(a.x - b.x); }

void DFS(int start = 0, int end = 0)
{
    if (end == m)
    {
        int tmpRes = 0;

        for (int i = 0; i < house.size(); i++)
        {
            int dist = INT_MAX;

            for (int j = 0; j < chicken.size(); j++)
            {
                if (vis[j])
                    dist = min(dist, distance(house[i], chicken[j]));
            }
            tmpRes += dist;
        }
        res = min(res, tmpRes);
        return;
    }
    if (start == chicken.size())
        return;

    vis[start] = true;
    DFS(start + 1, end + 1);

    vis[start] = false;
    DFS(start + 1, end);
}

int main() 
{
    //무방향성
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;
    board = vector<vector<int>>(n, vector<int>(n));

    for (int y = 0; y < n; y++)
    {
        for (int x = 0; x < n; x++)
        {
            cin >> board[y][x];
            auto num = board[y][x];
            IntPair pos{ y,x };

            if (num == 1)
                house.push_back(pos);

            else if (num == 2)
                    chicken.push_back(pos);
        }
    }
    res = INT_MAX;
    DFS();
    cout << res;
    return 0;
}
저작자표시 (새창열림)

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

[실3] 2407 - 조합  (0) 2022.12.27
[골4] 11404 - 플로이드  (0) 2022.12.22
[골4] 1504 - 특정한 최단 경로  (0) 2022.12.21
[골4] 1043 - 거짓말  (0) 2022.12.20
[골5] 1916 - 최소 비용 구하기  (0) 2022.12.19
    '코딩테스트/백준' 카테고리의 다른 글
    • [실3] 2407 - 조합
    • [골4] 11404 - 플로이드
    • [골4] 1504 - 특정한 최단 경로
    • [골4] 1043 - 거짓말
    ShovelingLife
    ShovelingLife
    Main skill stack => Unity C# / Unreal C++ Studying Front / BackEnd, Java Python

    티스토리툴바