POJ 3414 Pots(BFS)
倒水问题。1Y。
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <string> 5 #include <map> 6 #include <algorithm> 7 #include <queue> 8 #include <vector> 9 using namespace std; 10 int quea[100000],queb[100000],pre[100000],o[101][101]; 11 int fun[100000],que[100001]; 12 int flag,j,str,end,i; 13 void cle(int x,int y) 14 { 15 if(!o[x][y]) 16 { 17 o[x][y] = 1; 18 quea[end+j] = x; 19 queb[end+j] = y; 20 pre[end+j] = i; 21 fun[end+j] = flag; 22 j ++; 23 } 24 flag ++; 25 } 26 void show(int x) 27 { 28 int num = 1; 29 while(x != 1) 30 { 31 que[num++] = x; 32 x = pre[x]; 33 } 34 for(i = num-1;i >= 1;i --) 35 { 36 if(fun[que[i]] == 1) 37 printf("DROP(1)\n"); 38 else if(fun[que[i]] == 2) 39 printf("DROP(2)\n"); 40 else if(fun[que[i]] == 3) 41 printf("FILL(1)\n"); 42 else if(fun[que[i]] == 4) 43 printf("FILL(2)\n"); 44 else if(fun[que[i]] == 5) 45 printf("POUR(1,2)\n"); 46 else 47 printf("POUR(2,1)\n"); 48 } 49 } 50 int main() 51 { 52 int a,b,c,ans,x,y,z; 53 scanf("%d%d%d",&a,&b,&c); 54 ans = 0; 55 quea[1] = 0; 56 queb[1] = 0; 57 str = end = 1; 58 o[0][0] = 1; 59 z = 0; 60 while(str <= end) 61 { 62 j = 1; 63 for(i = str;i <= end;i ++) 64 { 65 if(quea[i] == c||queb[i] == c) 66 break; 67 } 68 if(i != end+1) 69 { 70 z = 1; 71 printf("%d\n",ans); 72 show(i); 73 break; 74 } 75 for(i = str; i <= end; i ++) 76 { 77 flag = 1; 78 x = 0; 79 y = queb[i]; 80 cle(x,y); 81 x = quea[i]; 82 y = 0; 83 cle(x,y); 84 x = a; 85 y = queb[i]; 86 cle(x,y); 87 x = quea[i]; 88 y = b; 89 cle(x,y); 90 if(b > quea[i]+queb[i]) 91 { 92 y = quea[i]+queb[i]; 93 x = 0; 94 } 95 else 96 { 97 y = b; 98 x = quea[i]+queb[i]-b; 99 } 100 cle(x,y); 101 if(a > quea[i]+queb[i]) 102 { 103 x = quea[i]+queb[i]; 104 y = 0; 105 } 106 else 107 { 108 x = a; 109 y = quea[i]+queb[i]-a; 110 } 111 cle(x,y); 112 } 113 ans ++; 114 str = end + 1; 115 end = end + j - 1; 116 } 117 if(!z) printf("impossible\n"); 118 return 0; 119 }