经典的倒水问题,bfs隐式图遍历加上简单的模拟就行了,注意判优的条件是倒水量而不是倒水的次数,用一个map记录bfs过程中所有出现过的小于目标水量的被子中的水量和对应的已经倒水的总水量,这样如果最后bfs找不到结果的话,直接把map里面最后一项取出来就是寻找失败的时候应该输入的数据。
#include <stdio.h>
#include <queue>
#include <set>
#include <map>
#include <string.h>
using namespace std;
int target;
int max_a;
int max_b;
int max_c;
struct state
{
int a, b, c;
int water;
};
bool operator < (const struct state &a, const struct state &b)
{
return a.water > b.water;
}
priority_queue<struct state> pq;
typedef unsigned int HASH_VALUE;
set<HASH_VALUE> visited;
typedef int WATER_IN_CUP;
typedef int WATER_EXCHANGE;
map<WATER_IN_CUP, WATER_EXCHANGE> m;
unsigned int _hash(const struct state &sta)
{
unsigned int v;
v = 201*201*sta.c + 201*sta.b + sta.a;
return v;
}
bool is_last_state(const struct state &sta)
{
if(sta.a==target || sta.b==target || sta.c==target)
return true;
return false;
}
bool find_target;
void bfs()
{
struct state sta, sta_top;
unsigned int hv;
while(!pq.empty())
{
memcpy(&sta_top, &(pq.top()), sizeof(struct state));
pq.pop();
//printf("pop:%d\t%d\t%d\t%d\n", sta_top.a, sta_top.b, sta_top.c, sta_top.water);
if(is_last_state(sta_top))
{
find_target = true;
printf("%d %d\n", sta_top.water, target);
break;
}
if(sta_top.a<target && m.find(sta_top.a)==m.end())
m[sta_top.a] = sta_top.water;
if(sta_top.b<target && m.find(sta_top.b)==m.end())
m[sta_top.b] = sta_top.water;
if(sta_top.c<target && m.find(sta_top.c)==m.end())
m[sta_top.c] = sta_top.water;
//把水倒入另一个杯子
memcpy(&sta, &sta_top, sizeof(struct state));
if(sta.a+sta.b >= max_b)
{
sta.water += max_b-sta.b;
sta.a = sta.a+sta.b-max_b;
sta.b = max_b;
hv = _hash(sta);
if(visited.find(hv) == visited.end())
{
visited.insert(hv);
pq.push(sta);
//printf("push:%d\t%d\t%d\t%d\n", sta.a, sta.b, sta.c, sta.water);
}
}
else
{
sta.water += sta.a;
sta.b += sta.a;
sta.a = 0;
hv = _hash(sta);
if(visited.find(hv) == visited.end())
{
visited.insert(hv);
pq.push(sta);
//printf("push:%d\t%d\t%d\t%d\n", sta.a, sta.b, sta.c, sta.water);
}
}
memcpy(&sta, &sta_top, sizeof(struct state));
if(sta.a+sta.c >= max_c)
{
sta.water += max_c-sta.c;
sta.a = sta.a+sta.c-max_c;
sta.c = max_c;
hv = _hash(sta);
if(visited.find(hv) == visited.end())
{
visited.insert(hv);
pq.push(sta);
//printf("push:%d\t%d\t%d\t%d\n", sta.a, sta.b, sta.c, sta.water);
}
}
else
{
sta.water += sta.a;
sta.c += sta.a;
sta.a = 0;
hv = _hash(sta);
if(visited.find(hv) == visited.end())
{
visited.insert(hv);
pq.push(sta);
//printf("push:%d\t%d\t%d\t%d\n", sta.a, sta.b, sta.c, sta.water);
}
}
memcpy(&sta, &sta_top, sizeof(struct state));
if(sta.b+sta.a >= max_a)
{
sta.water += max_a - sta.a;
sta.b = sta.a+sta.b-max_a;
sta.a = max_a;
hv = _hash(sta);
if(visited.find(hv) == visited.end())
{
visited.insert(hv);
pq.push(sta);
//printf("push:%d\t%d\t%d\t%d\n", sta.a, sta.b, sta.c, sta.water);
}
}
else
{
sta.water += sta.b;
sta.a += sta.b;
sta.b = 0;
hv = _hash(sta);
if(visited.find(hv) == visited.end())
{
visited.insert(hv);
pq.push(sta);
//printf("push:%d\t%d\t%d\t%d\n", sta.a, sta.b, sta.c, sta.water);
}
}
memcpy(&sta, &sta_top, sizeof(struct state));
if(sta.b+sta.c >= max_c)
{
sta.water += max_c - sta.c;
sta.b = sta.b+sta.c-max_c;
sta.c = max_c;
hv = _hash(sta);
if(visited.find(hv) == visited.end())
{
visited.insert(hv);
pq.push(sta);
//printf("push:%d\t%d\t%d\t%d\n", sta.a, sta.b, sta.c, sta.water);
}
}
else
{
sta.water += sta.b;
sta.c += sta.b;
sta.b = 0;
hv = _hash(sta);
if(visited.find(hv) == visited.end())
{
visited.insert(hv);
pq.push(sta);
//printf("push:%d\t%d\t%d\t%d\n", sta.a, sta.b, sta.c, sta.water);
}
}
memcpy(&sta, &sta_top, sizeof(struct state));
if(sta.c+sta.a >= max_a)
{
sta.water += max_a - sta.a;
sta.c = sta.a+sta.c-max_a;
sta.a = max_a;
hv = _hash(sta);
if(visited.find(hv) == visited.end())
{
visited.insert(hv);
pq.push(sta);
//printf("push:%d\t%d\t%d\t%d\n", sta.a, sta.b, sta.c, sta.water);
}
}
else
{
sta.water += sta.c;
sta.a += sta.c;
sta.c = 0;
hv = _hash(sta);
if(visited.find(hv) == visited.end())
{
visited.insert(hv);
pq.push(sta);
//printf("push:%d\t%d\t%d\t%d\n", sta.a, sta.b, sta.c, sta.water);
}
}
memcpy(&sta, &sta_top, sizeof(struct state));
if(sta.c+sta.b >= max_b)
{
sta.water += max_b-sta.b;
sta.c = sta.c+sta.b-max_b;
sta.b = max_b;
hv = _hash(sta);
if(visited.find(hv) == visited.end())
{
visited.insert(hv);
pq.push(sta);
//printf("push:%d\t%d\t%d\t%d\n", sta.a, sta.b, sta.c, sta.water);
}
}
else
{
sta.water += sta.c;
sta.b += sta.c;
sta.c = 0;
hv = _hash(sta);
if(visited.find(hv) == visited.end())
{
visited.insert(hv);
pq.push(sta);
//printf("push:%d\t%d\t%d\t%d\n", sta.a, sta.b, sta.c, sta.water);
}
}
}
}
void func(const struct state &sta)
{
unsigned int hv;
visited.clear();
while(!pq.empty()) pq.pop();
m.clear();
hv = _hash(sta);
visited.insert(hv);
pq.push(sta);
find_target = false;
bfs();
if(!find_target)
{
map<int,int>::iterator it;
it = m.end();
it--;
printf("%d %d\n", it->second, it->first);
}
}
int main(void)
{
int n;
int a, b, c, d;
struct state sta;
//freopen("input.dat", "r", stdin);
scanf("%d", &n);
while(n--)
{
scanf("%d %d %d %d", &a, &b, &c, &d);
max_a = a;
max_b = b;
max_c = c;
sta.a = sta.b = 0;
sta.c = max_c;
target = d;
sta.water = 0;
func(sta);
}
return 0;
}