-
맥주 마시면서 걸어가기백준알고리즘 2019. 10. 1. 22:511234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859#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<int, int> > q;q.push({ sx, sy });vector<pair<int, int> > 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, false, sizeof(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로 변경해서 값을 출력한다.