알고리즘/BFS & DFS

[백준/BFS&DFS/C++] 9205번 맥주 마시면서 걸어가기

데메즈 2023. 2. 17. 09:28
728x90
반응형

문제는 여기!

#include <bits/stdc++.h>

using namespace std;
int t, n;

struct Point{
    int x;
    int y;
};
Point house, festival;
Point cvs[101];
bool isvisited[101];

bool solve(){
    queue<pair<int, int>> q;
    q.push({house.x, house.y});

    while(!q.empty()){
        int x = q.front().first;
        int y = q.front().second;
        q.pop();

        if(abs(x-festival.x) + abs(y-festival.y) <= 1000) return true;

        for(int i=0; i<n; i++){
            if(isvisited[i]) continue;
            if(abs(x-cvs[i].x) + abs(y-cvs[i].y) <= 1000) {
                isvisited[i] = true;
                q.push({cvs[i].x, cvs[i].y});
            }
        }
    }
    return false;
}

void input() {
    cin >> t;

    for(int i=0; i<t; i++){
        fill(cvs, cvs+101, Point{0, 0});
        fill(isvisited, isvisited+101, false);
        cin >> n;
        cin >> house.x >> house.y;
        for(int c=0; c<n; c++){
            cin >> cvs[c].x >> cvs[c].y;
        }
        cin >> festival.x >> festival.y;

        if(solve()) cout << "happy\n";
        else cout << "sad\n";
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    input();

    return 0;
}
728x90
반응형