μ•Œκ³ λ¦¬μ¦˜

λ°±μ€€_12886_돌그룹

shj718 2022. 6. 24. 16:47

πŸ™‹‍♀️ 문제

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

 

12886번: 돌 그룹

였늘 κ°•ν˜ΈλŠ” λŒμ„ μ΄μš©ν•΄ μž¬λ―ΈμžˆλŠ” κ²Œμž„μ„ ν•˜λ €κ³  ν•œλ‹€. λ¨Όμ €, λŒμ€ μ„Έ 개의 그룹으둜 λ‚˜λˆ„μ–΄μ Έ 있으며 각각의 κ·Έλ£Ήμ—λŠ” 돌이 A, B, Cκ°œκ°€ μžˆλ‹€. κ°•ν˜ΈλŠ” λͺ¨λ“  그룹에 μžˆλŠ” 돌의 개수λ₯Ό κ°™κ²Œ λ§Œλ“€λ €

www.acmicpc.net

 

🧩 μ•Œκ³ λ¦¬μ¦˜

DFS / BFS μ•Œκ³ λ¦¬μ¦˜ λ‘˜λ‹€ μ‚¬μš© κ°€λŠ₯ (μ΄λ²ˆμ—λŠ” DFS둜 ν’€μ–΄λ΄€λ‹€.)

 

πŸ’‘ 아이디어

정점: (a,b,c) = 돌의 개수 μƒνƒœ

κ°„μ„ : (a,b,c) → (a´,b´,c´) = ν•œ λ‹¨κ³„μ—μ„œ 돌의 이동

전체 돌의 κ°œμˆ˜λŠ” λ³€ν•˜μ§€ μ•ŠλŠ”λ‹€λŠ” 점을 μ΄μš©ν•΄μ„œ κ³΅κ°„λ³΅μž‘λ„λ₯Ό 쀄인닀. (dfs ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜λ‘œ 2개의 돌 그룹만 ν•„μš”)

 

πŸ‘©‍πŸ’» μ½”λ“œ

#include <iostream>

using namespace std;

int sum;
bool visited[1501][1501];

void dfs(int a, int b) {
	if (visited[a][b]) return;
	visited[a][b] = true;
	int arr[3] = { a,b,sum - a - b };
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			if (arr[i] < arr[j]) {
				int changed[3] = { a,b,sum - a - b };
				changed[i] += arr[i];
				changed[j] -= arr[i];
				dfs(changed[i], changed[j]);
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int a, b, c;
	cin >> a >> b >> c;
	sum = a + b + c;
	if (sum % 3 != 0) {
		cout << 0 << '\n';
		return 0;
	}
	dfs(a, b);
	if (visited[sum / 3][sum / 3]) {
		cout << 1 << '\n';
	}
	else {
		cout << 0 << '\n';
	}
	return 0;
}