백준알고리즘

맥주 마시면서 걸어가기

먼지의삶 2019. 10. 1. 22:51

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<queue>
#include<vector>
#include<utility>
#include<cstring>
 
using namespace std;
 
int tc, n;
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> tc;
    for (int t = 0; t < tc; t++) {
        cin >> n;
        int sx, sy;
        cin >> sx >> sy;
        queue<pair<intint> > q;
        q.push({ sx, sy });
        vector<pair<intint> > con;
        for (int i = 0; i < n; i++) {
            int x, y;
            cin >> x >> y;
            con.push_back({ x,y });
        }
        int ex, ey;
        cin >> ex >> ey;
        con.push_back({ ex,ey });
        bool d[101];
        memset(d, falsesizeof(d));
        bool gameset = false;
        while (!q.empty()) {
            int x = q.front().first; int y = q.front().second;
            //cout << '!' << x << ' ' << y << '\n';
            if (x == ex && y == ey) {
                gameset = true;
                break;
            }
            q.pop();
            for (int i = 0; i < con.size(); i++) {
                if (d[i] == false) {
                    int nx = (con[i].first - x); int ny = (con[i].second - y);
                    if (nx < 0) nx = -nx; if (ny < 0) ny = -ny;
                    //cout << nx + ny << '\n';
                    if (nx + ny <= 1000) {
                        d[i] = true;
                        q.push({ con[i].first, con[i].second });
                    }
                }
            }
        }
        if (gameset)
            cout << "happy" << '\n';
        else cout << "sad" << '\n';
 
 
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

간단한 BFS문제,

그냥 테스트 케이스에 따라 해당노드에 따라 다음노드에서 맥주를 구입할수 있는지 없는지 확인하고 살수있다면 방문

그리고 방문한 지점이 끝지점이면, gameset 을true로 변경해서 값을 출력한다.