USACO: Mother's Milk
2014-11-26 21:43
225 查看
中文题目:http://www.nocow.cn/index.php/Translate:USACO/milk3
搜索题, 可以用dfs做,每一步都有6种选择,a->b, b->a, a->c...............
因为牛奶总数不变,三维的状态(a,b,c) 可以降至二维的(a,b)。 用数组记录已经访问过的状态,如果访问过了,就返回。
搜索题, 可以用dfs做,每一步都有6种选择,a->b, b->a, a->c...............
因为牛奶总数不变,三维的状态(a,b,c) 可以降至二维的(a,b)。 用数组记录已经访问过的状态,如果访问过了,就返回。
/* PROG: milk3 LANG: C++11 */ #include <cstdio> #include <cmath> #include <cstring> #include <string> #include <cstdlib> #include <climits> #include <ctype.h> #include <queue> #include <stack> #include <vector> #include <utility> #include <deque> #include <set> #include <map> #include <iostream> #include <fstream> #include <algorithm> using namespace std; #define mst(a,b) memset(a,b,sizeof(a)) #define eps 10e-8 typedef long long ll; const int N = 100010; const ll MOD = 1000000007; const int INF = 0x7fffffff; bool state[21][21]; bool record[21] = {false}; vector<int> ans; int a, b, c; void dfs(int x, int y){ // when to terminate int c1 = c - x - y; if(state[x][y]) return; if( !record[c1] && c1 >= c-b) ans.push_back(c1); record[c1] = true; state[x][y] = true; // a -> b int b1 = min(b , x + y); int a1 = x + y - b1; dfs(a1, b1); // b -> a a1 = min(a, x + y); b1 = x + y - a1; dfs(a1, b1); // a -> c a1 = 0; // total milk = c b1 = y; dfs(a1, b1); // c -> a a1 = min(a, x + c1); b1 = y; dfs(a1, b1); // b -> c a1 = x; b1 = 0; dfs(a1, b1); // c -> b a1 = x; b1 = min(b, y + c1); dfs(a1, b1); } int main() { ifstream fin("milk3.in"); ofstream fout("milk3.out"); fin >> a >> b >> c; dfs(0, 0); sort(ans.begin(), ans.end()); for(int i = 0; i <= ans.size()-2; i++){ fout << ans[i] << " "; } fout << ans[ans.size()-1] << endl; return 0; }
相关文章推荐
- USACO1.4 母亲的牛奶 Mother's Milk
- USACO1.4.2 Mother's Milk (milk3)
- USACO-Section1.4 Mother's Milk
- USACO:Mother's Milk
- Usaco 1.4.4 母亲的牛奶(Mother's Milk)
- USACO-Section 1.4 Mother's Milk(BFS)
- USACO1.4.2 Mother's Milk (milk3)
- [USACO] 从Mother’s Milk看搜索
- 一大堆的福利之【USACO题库】Mother's Milk母亲的牛奶
- USACO Section 1.4 Mother's Milk 搜索
- [USACO 1.4.4] Mother's Milk
- USACO Section 1.4 - Mother's Milk(DFS + 记忆化)
- USACO 1.4.2 Mother's Milk
- 【USACO】Mother's Milk(搜索)
- USACO Section 1.4 Mother's Milk
- USACO Mother's Milk
- USACO Mother's Milk
- USACO:1.4.4 Mother's Milk 母亲的牛奶 解析
- USACO 1.4.5 Mother's Milk
- USACO 1.4 Mother's Milk (枚举 + DFS)