Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Floyd
- Union-Find
- Bridge
- mergesorttree
- Math
- greedy
- TwoPointers
- backtracking
- topologicalsort
- BinarySearch
- scc
- Sweeping
- ArticulationPoint
- SlidingWindow
- Tree
- Flow
- KMP
- MST
- Dijkstra
- DP
- DFS
- 2-sat
- BFS
- Implementation
- Bellman-Ford
- IndexedTree
- FenwickTree
- ShortestPath
- LIS
- LCA
Archives
- Today
- Total
정리충의 정리노트
[백준] 9205: 맥주 마시면서 걸어가기 본문
0. 문제 주소
https://www.acmicpc.net/problem/9205
9205번: 맥주 마시면서 걸어가기
문제 송도에 사는 상근이와 친구들은 송도에서 열리는 펜타포트 락 페스티벌에 가려고 한다. 올해는 맥주를 마시면서 걸어가기로 했다. 출발은 상근이네 집에서 하고, 맥주 한 박스를 들고 출발한다. 맥주 한 박스에는 맥주가 20개 들어있다. 목이 마르면 안되기 때문에 50미터에 한 병씩 마시려고 한다. 상근이의 집에서 페스티벌이 열리는 곳은 매우 먼 거리이다. 따라서, 맥주를 더 구매해야 할 수도 있다. 미리 인터넷으로 조사를 해보니 다행히도 맥주를 파는 편의
www.acmicpc.net
1. 풀이
사실 DFS 카테고리에서 고른게 아니었으면 조금 헤맸을 수도 있었을 문제.
배열은 총 3개를 사용했다.
pii pos[MAX];
int d[MAX][MAX];
bool visited[MAX];
정수 좌표를 받는 pair<int, int>배열 pos,
플로이드-와샬때처럼 d[i][j] = i ~ j 사이의 거리(taxi metric),
DFS에 필요한 visited 배열
단순하게 생각해서 어떤 편의점에서 다른 지점으로 이동할 때, 50m 당 맥주 1병 * 20병이 최대이므로
거리가 1000m 이하인 곳을 인접한 곳으로 생각하고 DFS를 돌렸다.
2. 풀이 코드
* 유의할 점
#include <bits/stdc++.h>
#define pii pair<int, int>
#define MAX 105
using namespace std;
int T;
int n;
pii pos[MAX];
int d[MAX][MAX];
bool visited[MAX];
int dist(pii a, pii b){
int ai = a.first, aj = a.second, bi = b.first, bj = b.second;
return (abs(ai - bi) + abs(aj - bj));
}
void DFS(int a){
visited[a] = true;
for (int i=0;i<=n+1;i++){
if (i == a) continue;
if (!visited[i] && d[a][i] <= 1000) DFS(i);
}
}
int main(){DFS(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif // ONLINE_JUDGE
scanf("%d", &T);
for (int x=0;x<T;x++){
memset(d, 0, sizeof(d));
memset(visited, 0, sizeof(visited));
scanf("%d", &n);
int i_s, j_s, i_e, j_e;
scanf("%d %d", &i_s, &j_s);
pos[0] = pii(i_s, j_s);
for (int i=1;i<=n;i++){
int a, b;
scanf("%d %d", &a, &b);
pos[i] = pii(a, b);
}
scanf("%d %d", &i_e, &j_e);
pos[n+1] = pii(i_e, j_e);
for (int i=0;i<=n+1;i++){
for (int j=0;j<=n+1;j++){
if (i == j) continue;
d[i][j] = dist(pos[i], pos[j]);
}
}
DFS(0);
if (visited[n+1]) printf("happy\n");
else printf("sad\n");
}
}
'PS > BFS & DFS' 카테고리의 다른 글
[백준] 3055: 탈출 (0) | 2020.02.03 |
---|---|
[백준] 2668: 숫자고르기 (0) | 2020.01.19 |
[백준] 2146: 다리 만들기 (0) | 2020.01.18 |
[백준] 9466: 텀 프로젝트 (0) | 2020.01.18 |
[백준] 1707: 이분 그래프 (0) | 2020.01.17 |
Comments