之前用的LA,LB写的标记,表示x,y坐标之前的状态,我发现我写错了,这样的话会忽略很多组合,组合起来又是不同的了,本来可以有10^4,如果那样写就只有100*2的状态了,而且步行,所以我用了一个办法,L[t1.x*100+t1.y]=t.x*100+t.y; 这样压缩表示状态挺好的。知道错在哪里了然后就过了。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <stack> 8 #include <cmath> 9 #define maxn 100+10 10 using namespace std; 11 int step[maxn][maxn]; 12 bool v[maxn][maxn]; //true 表示访问过 false表示没有访问过 13 int L[maxn*maxn]; 14 int aim; 15 struct node 16 { 17 int maxa,maxb; 18 int a,b; 19 int step; 20 }; 21 node bfs(node temp) 22 { 23 queue<node>haha; 24 node t,t1,impossible; 25 impossible.step=-1; 26 int linshi; 27 int biaoji1,biaoji2; 28 haha.push(temp); 29 while(haha.size()>0) 30 { 31 t=haha.front(); 32 haha.pop(); 33 //fill a 操作ID是1 34 { 35 t1=t; 36 t1.a=t.maxa; 37 t1.b=t.b; 38 t1.step++; 39 if(v[t1.a][t1.b]==0) 40 { 41 v[t1.a][t1.b]=true; 42 step[t1.a][t1.b]=1; 43 L[t1.a*100+t1.b]=t.a*100+t.b; 44 haha.push(t1); 45 if(t1.a==aim||t1.b==aim) 46 return t1; 47 } 48 } 49 //fill b 操作ID是2 50 { 51 t1.a=t.a; 52 t1.b=t.maxb; 53 if(v[t1.a][t1.b]==0) 54 { 55 v[t1.a][t1.b]=true; 56 step[t1.a][t1.b]=2; 57 L[t1.a*100+t1.b]=t.a*100+t.b; 58 haha.push(t1); 59 if(t1.a==aim||t1.b==aim) 60 return t1; 61 } 62 } 63 //pour a to b 操作ID是3 64 { 65 linshi=t.a+t.b; 66 if(linshi<=t.maxb) 67 { 68 t1.a=0; 69 t1.b=linshi; 70 } 71 else if(linshi>t.maxb) 72 { 73 t1.a=linshi-t.maxb; 74 t1.b=t.maxb; 75 } 76 if(v[t1.a][t1.b]==0) 77 { 78 v[t1.a][t1.b]=true; 79 step[t1.a][t1.b]=3; 80 L[t1.a*100+t1.b]=t.a*100+t.b; 81 haha.push(t1); 82 if(t1.a==aim||t1.b==aim) 83 return t1; 84 } 85 } 86 //pour b to a ID = 4 87 { 88 linshi=t.a+t.b; 89 if(linshi<=t.maxa) 90 { 91 t1.a=linshi; 92 t1.b=0; 93 } 94 else if(linshi>t.maxa) 95 { 96 t1.a=t.maxa; 97 t1.b=linshi-t.maxa; 98 } 99 if(v[t1.a][t1.b]==0) 100 { 101 v[t1.a][t1.b]=true; 102 step[t1.a][t1.b]=4; 103 L[t1.a*100+t1.b]=t.a*100+t.b; 104 haha.push(t1); 105 if(t1.a==aim||t1.b==aim) 106 return t1; 107 } 108 } 109 { 110 t1.a=0; 111 t1.b=t.b; 112 if(v[t1.a][t1.b]==0) 113 { 114 v[t1.a][t1.b]=true; 115 step[t1.a][t1.b]=5; 116 L[t1.a*100+t1.b]=t.a*100+t.b; 117 haha.push(t1); 118 if(t1.a==aim||t1.b==aim) 119 return t1; 120 121 } 122 } 123 { 124 t1.a=t.a; 125 t1.b=0; 126 if(!v[t1.a][t1.b]) 127 { 128 v[t1.a][t1.b]=true; 129 step[t1.a][t1.b]=6; 130 L[t1.a*100+t1.b]=t.a*100+t.b; 131 haha.push(t1); 132 if(t1.a==aim||t1.b==aim) 133 return t1; 134 } 135 } 136 } 137 return impossible; 138 } 139 int main() 140 { 141 node begin,end; 142 int res,tx,ty,count,ttt,i,j; 143 stack<int>heihei; 144 while(~scanf("%d%d%d",&begin.maxa,&begin.maxb,&aim)) 145 { 146 for(i=0;i<100+5;++i) 147 { 148 for(j=0;j<100+5;++j) 149 { 150 v[i][j]=0; 151 step[i][j]=0; 152 } 153 } 154 begin.a=begin.b=begin.step=0; 155 v[0][0]=1; 156 end = bfs( begin ); 157 count=end.step; 158 if(count==-1) 159 { 160 printf("impossible\n"); 161 continue; 162 } 163 printf("%d\n",count); 164 tx=end.a; 165 ty=end.b; 166 while(count--) 167 { 168 heihei.push(step[tx][ty]); 169 int kkk; 170 kkk=tx*100+ty; 171 tx=L[kkk]/100; 172 ty=L[kkk]%100; 173 if(tx==0&&ty==0) 174 break; 175 } 176 while(heihei.size()>0) 177 { 178 res=heihei.top(); 179 if(res==1) printf("FILL(1)\n"); 180 else if(res==2) printf("FILL(2)\n"); 181 else if(res==3) printf("POUR(1,2)\n"); 182 else if(res==4) printf("POUR(2,1)\n"); 183 else if(res==5) printf("DROP(1)\n"); 184 else if(res==6) printf("DROP(2)\n"); 185 heihei.pop(); 186 } 187 } 188 return 0; 189 }