您的位置:首页 > 其它

POJ 3414 Pots (BFS)

2015-09-24 11:13 393 查看
#include <stdio.h>
#define MAX 101

int volumnOfPotA, volumnOfPotB, litersToYield;

int stateVisited[MAX][MAX];

typedef struct {
int litersInA;
int litersInB;
int operation;
int pre;
} ELEMENT;

ELEMENT queue[MAX * MAX];
int head, tail;

int stackOfOpe[MAX * MAX];
int top;

void BFS(){
ELEMENT push;
stateVisited[0][0] = 1;
push.litersInA = 0;
push.litersInB = 0;
push.operation = 0;
push.pre = 0;
queue[tail++] = push;

while (head < tail){
ELEMENT pop = queue[head++];
int indexOfOpe;
for (indexOfOpe = 1; indexOfOpe <= 6; indexOfOpe++){
int litersInA = pop.litersInA;
int litersInB = pop.litersInB;
switch (indexOfOpe){
case 1:
litersInA = volumnOfPotA;
break;
case 2:
litersInB = volumnOfPotB;
break;
case 3:
litersInA = 0;
break;
case 4:
litersInB = 0;
break;
case 5:
litersInA = pop.litersInA + pop.litersInB;
litersInB = 0;
if (litersInA > volumnOfPotA){
litersInB = litersInA - volumnOfPotA;
litersInA = volumnOfPotA;
}
break;
case 6:
litersInB = pop.litersInB + pop.litersInA;
litersInA = 0;
if (litersInB > volumnOfPotB){
litersInA = litersInB - volumnOfPotB;
litersInB = volumnOfPotB;
}
break;
}

if (stateVisited[litersInA][litersInB] != 0)
continue;

stateVisited[litersInA][litersInB] = 1;

if (litersInA == litersToYield || litersInB == litersToYield){
stackOfOpe[top++] = indexOfOpe;
int preElement = head - 1;
while (preElement != 0){
stackOfOpe[top++] = queue[preElement].operation;
preElement = queue[preElement].pre;
}
printf("%d\n", top);
int index;
for (index = --top; index >= 0; index--){
switch (stackOfOpe[index]){
case 1:
printf("FILL(1)\n");
break;
case 2:
printf("FILL(2)\n");
break;
case 3:
printf("DROP(1)\n");
break;
case 4:
printf("DROP(2)\n");
break;
case 5:
printf("POUR(2,1)\n");
break;
case 6:
printf("POUR(1,2)\n");
break;
}
}
return;
}

push.litersInA = litersInA;
push.litersInB = litersInB;
push.pre = head - 1;
push.operation = indexOfOpe;
queue[tail++] = push;
}//end of for indexOfOpe
}//end of  while (head < tail)
printf("impossible\n");
}

int main()
{
scanf("%d %d %d", &volumnOfPotA, &volumnOfPotB, &litersToYield);
BFS();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POJ 3414 Pots BFS