티스토리 뷰
https://www.acmicpc.net/problem/15686
15686번: 치킨 배달
크기가 N×N인 도시가 있다. 도시는 1×1크기의 칸으로 나누어져 있다. 도시의 각 칸은 빈 칸, 치킨집, 집 중 하나이다. 도시의 칸은 (r, c)와 같은 형태로 나타내고, r행 c열 또는 위에서부터 r번째 칸
www.acmicpc.net
이전에 재귀를 이용해 풀었는데, 이번엔 조합을 이용해 풀어보았습니다.
치킨집을 저장한 벡터 자체를 사용하면 시간초과 문제가 있어서 check 벡터를 만들어 next_permutation에 사용하였습니다.
#include <cstdio>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
int n,m,ans = 1e9;
vector<pair<int, int> > chi; //치킨집 위치
vector<pair<int, int> > h; //집 위치
int main(){
scanf("%d %d", &n, &m);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
// 맵은 풀이에 이용하지 않으므로 값만 입력받음
int tmpa;
scanf("%d", &tmpa);
if(tmpa==1) {
h.push_back(make_pair(i,j));
}else if(tmpa==2){
chi.push_back(make_pair(i,j));
}
}
}
//next permutation 에 사용할 배열
vector<int> check((int)chi.size());
for(int i=0;i<m;i++){
check[i]=1;
}
sort(check.begin(),check.end());
do {
int distsum =0;
for(int i=0;i<h.size();i++){
int min = 1e8;
for(int j=0;j<chi.size();j++){
if (check[j]==0) continue;
int now = abs(chi[j].first-h[i].first)+abs(chi[j].second-h[i].second);
if (now < min) min = now;
}
distsum+=min;
}
if(distsum < ans) ans = distsum;
}while (next_permutation(check.begin(),check.end()));
printf("%d\n", ans);
return 0;
}'Algorithm > 알고리즘 문제풀이' 카테고리의 다른 글
| 프로그래머스) Lv2 - 기능개발 (0) | 2020.06.15 |
|---|---|
| BOJ) 12872 - 플레이리스트 (0) | 2020.06.15 |
| BOJ) 1261 - 알고스팟 (0) | 2020.06.12 |
| BOJ) 15684 - 사다리 조작 (0) | 2020.06.12 |
| BOJ) 15683 - 감시 (0) | 2020.06.11 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 알고리즘
- Swift
- infallible
- 백준
- Lottie
- 안드로이드
- blendshapes
- 백준온라인저지
- ARKit
- ios
- Swift unowned
- 프로그래머스
- GraphDB
- SWEA
- rxswift6
- Reactivex
- blendshape
- disposeBag
- rxswift
- DispatchQueue
- 코코아팟
- cocoapods
- Kotlin
- Swift weak
- UIHostingController
- C++
- SwiftUI
- coreml
- boj
- 카카오인턴십
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
글 보관함