[BOJ] 9205 맥주 마시면서 걸어가기

Time Lapse :11min 9sec

9205.cpp

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
32
33
34
35
#include <stdio.h>
#include <memory.h>
#include <algorithm>
#include <queue>
using namespace std;
int N, tc;
bool visit[102];
int dist[102][2];
bool reachable(int c, int d){
return abs(dist[c][0]-dist[d][0]) + abs(dist[c][1]-dist[d][1]) <= 1000;
}
int main(){
scanf("%d",&tc);
while(tc--){
scanf("%d",&N);
memset(visit,false,sizeof(visit));
for(int i = 0; i < N+2; ++i)
scanf("%d %d",&dist[i][0],&dist[i][1]);
queue<int> q;
q.push(0);
visit[0] = true;
while(!q.empty()&&!visit[N+1]){
int c = q.front();
q.pop();
for(int i = 1; i < N+2; ++i){
if(visit[i]) continue;
if(reachable(c,i)){
q.push(i);
visit[i] = true;
}
}
}
visit[N+1] ? printf("happy\n") : printf("sad\n");
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/9205/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.