您的位置:首页 > 其它

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)。 用数组记录已经访问过的状态,如果访问过了,就返回。

/*
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs