3414Pots
不知道是谁出这道题的...坑爹,又是一开始错,没怎么改动,接着又通过了
我的代码ACCEPT
#include "iostream" #include "queue" #include "algorithm" #include "string.h" using namespace std; struct Point{ int a,b,count; int list[1000]; }; int min(int a,int b){return a>b?b:a;} Point tem,ans; int A,B,C; char rea[6][20]={{"FILL(2)"},{"FILL(1)"},{"DROP(2)"},{"DROP(1)"},{"POUR(1,2)"},{"POUR(2,1)"}}; void pour(int a){ int x=tem.a+tem.b; if(a==1){ ans.b=x>B?B:x; ans.a=x-ans.b; } else{ ans.a=x>A?A:x; ans.b=x-ans.a; } } int dir[6][2]={{0,1},{1,0},{0,-1},{-1,0},{2,3},{3,2}}; int main(){ int i,j,map[110][110],flag=0; cin>>A>>B>>C; memset(map,0,sizeof(map)); Point pt; pt.a=0;pt.b=0;pt.count=1; queue<Point> q; q.push(pt); while(!q.empty()){ tem=q.front(); q.pop(); if(map[tem.a][tem.b]==1)continue; map[tem.a][tem.b]=1; //cout<<tem.a<<' '<<tem.b<<' '<<tem.count<<endl;system("pause"); for(i=0;i<6;i++){ if(i==0){ans.a=tem.a;ans.b=B;tem.list[tem.count]=0;} else if(i==1){ans.a=A;ans.b=tem.b;tem.list[tem.count]=1;} else if(i==2){ans.a=tem.a;ans.b=0;tem.list[tem.count]=2;} else if(i==3){ans.a=0;ans.b=tem.b;tem.list[tem.count]=3;} else if(i==4){pour(1);tem.list[tem.count]=4;} else {pour(-1);tem.list[tem.count]=5;} ans.count=tem.count+1; for(j=1;j<ans.count;j++){ans.list[j]=tem.list[j];} //cout<<ans.a<<' '<<ans.b<<' '<<ans.count<<endl;system("pause"); q.push(ans); if(ans.a==C||ans.b==C){flag=1;goto l1;} } //cout<<endl<<endl; } l1:if(flag){cout<<ans.count-1<<endl;for(i=1;i<ans.count;i++)cout<<rea[ans.list[i]]<<endl;} else cout<<"impossible"<<endl; }
人家的代码,也不错,不用queue,直接用数组
#include<iostream> #include<cstdio> #include<string> #include<cstring> #define Min(a,b)a<b?a:b #define MAX 105 using namespace std; int a,b,c; bool vs[MAX][MAX]; char str[6][10]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)", "POUR(1,2)","POUR(2,1)"}; struct node { int ope,a,b,step; node *pre; }qu[MAX*MAX]; void print(node now)//输出很有技巧 看一大神的思路 { if(now.pre!=NULL) { print(*now.pre); printf("%s\n",str[now.ope]); } } void bfs()//六种操作 { int beg=0,end=0; qu[beg].a=qu[beg].b=0; qu[beg].step=0; qu[beg].pre=NULL; while(beg<=end) { node t=qu[beg]; if(t.a==c||t.b==c) { printf("%d\n",t.step); print(t); return ; } if(!vs[a][t.b]) { end++; qu[end].ope=0; qu[end].a=a; qu[end].b=t.b; qu[end].step=t.step+1; qu[end].pre=&qu[beg]; vs[a][t.b]=1; } if(!vs[t.a][b]) { end++; qu[end].ope=1; qu[end].a=t.a; qu[end].b=b; qu[end].step=t.step+1; qu[end].pre=&qu[beg]; vs[t.a][b]=1; } if(!vs[0][t.b]) { end++; qu[end].ope=2; qu[end].a=0; qu[end].b=t.b; qu[end].step=t.step+1; qu[end].pre=&qu[beg]; vs[0][t.b]=1; } if(!vs[t.a][0]) { end++; qu[end].ope=3; qu[end].a=t.a; qu[end].b=0; qu[end].step=t.step+1; qu[end].pre=&qu[beg]; vs[t.a][0]=1; } int min1=Min(t.a,b-t.b); if(!vs[t.a-min1][t.b+min1]) { end++; qu[end].ope=4; qu[end].a=t.a-min1; qu[end].b=t.b+min1; qu[end].step=t.step+1; qu[end].pre=&qu[beg]; vs[t.a-min1][t.b+min1]=1; } int min2=Min(a-t.a,t.b); if(!vs[t.a+min2][t.b-min2]) { end++; qu[end].ope=5; qu[end].a=t.a+min2; qu[end].b=t.b-min2; qu[end].step=t.step+1; qu[end].pre=&qu[beg]; vs[t.a+min2][t.b-min2]=1; } beg++; } printf("impossible\n"); return ; } int main() { scanf("%d%d%d",&a,&b,&c); memset(vs,0,sizeof(vs)); bfs(); return 0; }