newB = b;
}
else
{
newB = newA + newB;
newA = 0;
}
if(!visit[newA][newB])
{
q.push(newA, newB, current.stepID + 1, q.front - 1, POUR12);
visit[newA][newB] = true;
}
break;
}
case POUR21:
{
int newA = current.liter1;
int newB = current.liter2;
if(newA + newB > a)
{
newB = newA + newB - a;
newA = a;
}
else
{
newA = newA + newB;
newB = 0;
}
if(!visit[newA][newB])
{
q.push(newA, newB, current.stepID + 1, q.front - 1, POUR21);
visit[newA][newB] = true;
}
break;
}
case DROP1:
{
{
q.push(0, nxt.liter2, current.stepID + 1, q.front - 1, DROP1);
visit[0][nxt.liter2] = true;
}
break;
}
case DROP2:
{
if( !visit[nxt.liter1][0] )
{
q.push(nxt.liter1, 0, current.stepID + 1, q.front - 1, DROP2);
visit[nxt.liter1][0] = true;
}
break;
}
}
}
}
if( q.front == q.rear)
{
printf("impossible\n");
}
else
{
printf("%d\n", q.steps[q.front-1].stepID);
PrintPath(q.front-1);
}
}
int main()
{
int a,b,c;
while(scanf("%d%d%d", &a, &b, &c) != EOF)
{
memset(visit, 0, sizeof(visit));
BFS(a, b, c);
}
return 0;
}
编写的时候犯了好几个bug,导致半天都调不通
bug1, 在newA newB赋值的时候,将顺序写错,忽略了两个值存在依赖关系的事实。
bug2, 最后遍历的时候,没有写front-1, 其实front在上次取完了的时候,就已经-1.
作者:hopeztm