Skip to content

260122 : [BOJ 2251] 물통#2326

Merged
alirz-pixel merged 1 commit intomainfrom
munhyeong/2325/1
Jan 25, 2026
Merged

260122 : [BOJ 2251] 물통#2326
alirz-pixel merged 1 commit intomainfrom
munhyeong/2325/1

Conversation

@alirz-pixel
Copy link
Member

@alirz-pixel alirz-pixel commented Jan 22, 2026

🚀 이슈 번호

Resolve: {#2325}

🧩 문제 해결

스스로 해결:

🔎 접근 과정

문제 해결을 위한 접근 방식을 설명해주세요.

  • 🔹 어떤 알고리즘을 사용했는지: 브루트포스
  • 🔹 어떤 방식으로 접근했는지

BFS를 이용하여 각 물병을 옮기는 케이스들을 구현하였다.

또한, set으로 a==0 일 때의 c 값을 저장하여 정답을 추출하였다.

⏱️ 시간 복잡도

시간 복잡도 분석을 작성해주세요.
최악의 경우 수행 시간은 어느 정도인지 분석합니다.

  • Big-O 표기법: O(?)
  • 이유:

💻 구현 코드

#include <iostream>
#include <queue>
#include <set>

using namespace std;

struct Node {
	int a;
	int b;
	int c;

	bool operator<(const Node& other) const {
		if (a != other.a) return a < other.a;
		if (b != other.b) return b < other.b;
		return c < other.c;
	}
};

void insert_queue(queue<Node>& q, set<Node> &visited, int a, int b, int c) {
	if (visited.insert({ a, b, c }).second) {
		q.push({ a, b, c });
	}
}

int main() {
	int a, b, c;
	cin >> a >> b >> c;
	
	set<Node> visited;
	queue<Node> q;

	q.push({ 0, 0, c });
	visited.insert(q.front());

	set<int> answer;
	while (!q.empty()) {
		Node front = q.front();
		q.pop();

		if (!front.a)
			answer.insert(front.c);
		// cout << front.a << " " << front.b << " " << front.c << "\n";

		int move;
		// a -> b
		move = min(front.a, (b - front.b));
		insert_queue(q, visited, front.a - move, front.b + move, front.c);

		// a -> c
		move = min(front.a, (c - front.c));
		insert_queue(q, visited, front.a - move, front.b, front.c + move);

		// b -> a
		move = min(front.b, (a - front.a));
		insert_queue(q, visited, front.a + move, front.b - move, front.c);

		// b -> c
		move = min(front.b, (c - front.c));
		insert_queue(q, visited, front.a, front.b - move, front.c + move);

		// c -> a
		move = min(front.c, (a - front.a));
		insert_queue(q, visited, front.a + move, front.b, front.c - move);

		// c -> b
		move = min(front.c, (b - front.b));
		insert_queue(q, visited, front.a, front.b + move, front.c - move);
	}
	
	for (auto e : answer)
		cout << e << " ";

	return 0;
}

@alirz-pixel alirz-pixel linked an issue Jan 22, 2026 that may be closed by this pull request
@alirz-pixel alirz-pixel self-assigned this Jan 22, 2026
@alirz-pixel alirz-pixel merged commit a19911e into main Jan 25, 2026
2 checks passed
@alirz-pixel alirz-pixel deleted the munhyeong/2325/1 branch January 25, 2026 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

260122 : 코딩테스트

1 participant