嗜血魂K

导航

1.4 Mother's Milk

好高欣~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

好激动~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

一个月了吧,终于搞掉这道题了,bfs =-=

下面开始总结- -

我的思路比较繁杂,总体上是bfs

对每个状态6种方法倒水都写了呃,但是这个函数真的纠结了我很久,不好搞清楚究竟要传递什么值过去,修改了很多,编程基本功不行吧,最终办法是传递这个状态的指针(int模拟),然后from,to两个指针,

//等等,突然觉得我好像写错了,但为什么也过了.....按照我的思路,用temp记录下当前状态要操作两个瓶子的水,尝试完之后 都 要回溯回去呃,而不是重复才回溯........这样可能漏状态?= =......以后注意下,这次过了就

//算了..因为要睡觉了0 0

状态判重,个人是开了个bool数组,某两个瓶子的水量 A*100+B就可以判重了嘛....

记录输出的C:也是开的bool数组,无意开的,发现挺方便,因为要顺序输出嘛.

------------------------------------------------------------------------------------------------------------------------------------

关于 有两个变量地址开到一起了 是某个数组开太大了

本来最后提交还有几个小错误的,一时想不起来了= =

 ------------------------------------------------------------------------------------------------------------------------------------

看了看题解,标程差不多,唔..这应该叫DFS..概念啊.......

然后倒水的函数 传递的只是杯子代号 这样就比我少用一个变量.

nocow上的其他算法就没看了- -

State
pour(State s, int from, int to)
{
int amt;

amt = s.a[from];
if(s.a[to]+amt > cap[to])
amt = cap[to] - s.a[to];

s.a[from] -= amt;
s.a[to] += amt;
return s;
}
//////////////////////////////////////////////
for(i=0; i<3; i++)
for(j=0; j<3; j++)
search(pour(s, i, j));



 

 

/*
ID:y7276571
LANG: C++
TASK:milk3
*/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<climits>
using namespace std;
const int MAXN = 1000;
struct water{
int a, b, c;
};
water tosearch[MAXN];
int posc[MAXN], front = 0, rear = 1, hash; //possible c and a point
int MAX_A, MAX_B, MAX_C;
bool hash_c[4000] = {0}, pos_c[20];
bool check(int p)
{
hash = 100*tosearch[p].b+tosearch[p].c;
for(int i = 0; i < rear;i++)
if(i != p && hash_c[hash]) return false;
return true;
}
void pour(int *from,int *to, int MAX)
{
int temp1 = *from, temp2 = *to, p = front;
if(*from+*to > MAX) { *from = *from-MAX+*to; *to = MAX; }
else { *to += *from; *from = 0; }
//check
if(check(p))
{
//add
tosearch[rear].a = tosearch[p].a;
tosearch[rear].b = tosearch[p].b;
tosearch[rear++].c = tosearch[p].c;
hash_c[hash] =true;
if(!tosearch[p].a) pos_c[tosearch[p].c] = true;
// cout << "ADD:" << tosearch[p].c << endl;
// cout << tosearch[p].a << " " << tosearch[p].b << " " << tosearch[p].c << endl;
}
//recover
else { *from = temp1; *to = temp2; }
}
void bfs()
{
tosearch[front].c = MAX_C;
hash_c[MAX_C] = pos_c[MAX_C] = true;
while(front < rear)
{
pour(&tosearch[front].a, &tosearch[front].b, MAX_B);
pour(&tosearch[front].c, &tosearch[front].b, MAX_B);
pour(&tosearch[front].a, &tosearch[front].c, MAX_C);
pour(&tosearch[front].b, &tosearch[front].c, MAX_C);
pour(&tosearch[front].b, &tosearch[front].a, MAX_A);
pour(&tosearch[front].c, &tosearch[front].a, MAX_A);
front++;
}
}
int main()
{
freopen("milk3.in", "r", stdin);
freopen("milk3.out", "w", stdout);
cin >> MAX_A >> MAX_B >> MAX_C;
bfs();
for(int i = 0, first = 1; i <= MAX_C; i++)
if(pos_c[i])
{
if(first) { cout << i; first = 0; }
else cout << ' ' << i;
}
cout << endl;
return 0;
}

 

posted on 2012-01-25 23:51  嗜血魂K  阅读(235)  评论(0编辑  收藏  举报