1일1알

백준 12886번 돌 그룹 C++ 본문

알고리즘

백준 12886번 돌 그룹 C++

영춘권의달인 2022. 3. 2. 12:10

출처 : https://www.acmicpc.net/problem/12886

 

a에서 b로 옮기는 경우와 b에서 a로 옮기는 경우가 배치되는 순서만 다를 뿐, 결과는 같기 때문에 순서를 신경쓰지 않고 방문 표시를 하며 bfs 탐색으로 문제를 해결하였다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>

using namespace std;
using ll = long long;

struct Stones {
	int a;
	int b;
	int c;
};

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int a, b, c;
	cin >> a >> b >> c;
	vector<vector<bool>> found(1503, vector<bool>(1503, false));
	queue<Stones> q;
	q.push({ a,b,c });
	int ans = 0;
	while (!q.empty()) {
		auto curr = q.front();
		q.pop();
		if (curr.a == curr.b && curr.b == curr.c) {
			ans = 1;
			break;
		}
		int small, big, nextA, nextB, nextC;
		// a,b
		small = min(curr.a, curr.b);
		big = max(curr.a, curr.b);
		nextA = small * 2;
		nextB = big - small;
		if (!found[nextA][nextB]) {
			found[nextA][nextB] = true;
			found[nextB][nextA] = true;
			q.push({ nextA,nextB,curr.c });
		}	
		// a,c
		small = min(curr.a, curr.c);
		big = max(curr.a, curr.c);
		nextA = small * 2;
		nextC = big - small;
		if (!found[nextA][nextC]) {
			found[nextA][nextC] = true;
			found[nextC][nextA] = true;
			q.push({ nextA,curr.b,nextC });
		}
		// b,c
		small = min(curr.b, curr.c);
		big = max(curr.b, curr.c);
		nextB = small * 2;
		nextC = big - small;
		if (!found[nextB][nextC]) {
			found[nextB][nextC] = true;
			found[nextC][nextB] = true;
			q.push({ curr.a,nextB,nextC });
		}
	}
	cout << ans;
};