USACO Section 1.4: Mother's Milk
2014-04-07 10:28
423 查看
这题其实暴力是可以的,因为给的限制条件就很小了,难点在于编程,struct state的写法和pour函数都是关键点
/* ID: leetcod3 PROG: milk3 LANG: C++ */ #include <iostream> #include <fstream> #include <string> #include <map> #include <vector> #include <set> #include <algorithm> #include <queue> #include <cmath> #include <list> #include <cstring> #include <cstdlib> #include <limits> #include <stack> using namespace std; ofstream fout ("milk3.out"); ifstream fin ("milk3.in"); bool visit[21][21][21] = {false}; bool ans[21] = {false}; int c[3]; struct state { int a[3]; void getvisit() {visit[a[0]][a[1]][a[2]] = true;} bool isvisit() {return visit[a[0]][a[1]][a[2]];} }cur; queue<state> S; void pour(state &t, int s, int d) { int tmp = c[d] - t.a[d]; if (tmp == 0) return; else if (t.a[s] <= tmp) { t.a[d] += t.a[s]; t.a[s] = 0; } else { t.a[d] = c[d]; t.a[s] -= tmp; } } int main() { fin >> c[0] >> c[1] >> c[2]; cur.a[0] = cur.a[1] = 0; cur.a[2] = c[2]; cur.getvisit(); S.push(cur); while (!S.empty()) { state front = S.front(); S.pop(); if (front.a[0] == 0) ans[front.a[2]] = true; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == j) continue; state tmp = front; pour(tmp, i, j); if (!tmp.isvisit()) { S.push(tmp); tmp.getvisit(); //cout << "from state: " << front.a[0] << " " << front.a[1] << " " << front.a[2]; //cout << " to state: " << tmp.a[0] << " " << tmp.a[1] << " " << tmp.a[2] << endl; } } } } for (int i = 0; i <= c[2]; i++) { if (ans[i]) { fout << i; if (i == c[2]) fout << endl; else fout << " "; } } return 0; }
相关文章推荐
- USACO-Section1.4 Prime Cryptarithm (搜索)
- USACO-Section 1.4 Arithmetic Progressions
- USACO Section 1.4 Arithmetic Progressions(枚举)
- USACO-Section 1.4 Arithmetic Progressions(枚举)
- C++——USACO Section 1.4 题解
- USACO-Section1.4 Mother's Milk [搜索]
- USACO Section 1.4 - Mother's Milk(DFS + 记忆化)
- USACO Section1.4 Arithmetic Progressions
- USACO Section 1.4 More Search Technique
- USACO Section 1.4 Arithmetic Progressions
- USACO Section 1.4 Mother's Milk 搜索
- USACO Section 1.4 Mother's Milk
- USACO - Chapter1 Section 1.4 - Arithmetic Progressions
- USACO-Section1.4 Arithmetic Progressions[其他][暴力枚举]
- USACO-Section1.4 Arithmetic Progressions
- USACO Section 1.4 - Arithmetic Progressions (暴力)
- 等差数列 [USACO Training Section 1.4]
- USACO-Section 1.4 Mother's Milk(BFS)
- USACO - Chapter1 Section 1.4 - Mother's Milk
- USACO-Section1.4 Mother's Milk