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 ACM 1001
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)
- Surrounded Regions
- Word Ladder, Gray Code
- 【高手回避】poj3268,一道很水的dijkstra算法题
- UVA 11624