您的位置:首页 > 其它

USACO Section 1.4: Mother's Milk

2014-04-07 10:28 337 查看
这题其实暴力是可以的,因为给的限制条件就很小了,难点在于编程,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: