Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
牛人帮忙看看哪里错了,是在找不到了91 100 95 这组数据我答案是210步,牛人帮忙看看哪里错了 #include <iostream> #include <queue> #include <string> using namespace std; typedef struct node { int pone; // 第一个容器还剩多少水 int ptwo; // 第二个容器还剩多少水 int steps; // 一共有多少步骤 int path; // 是通过哪个步骤得到此状态的 struct node *last; // 指向前一个节点(指向 状态转换成此状态的那个状态) }ACM,*PACM; int data[2000000]; int visit[102][102]; string road[7]={ " " , "FILL(1)" , "FILL(2)" , "DROP(1)" , "DROP(2)" , "POUR(1,2)" , "POUR(2,1)" }; int main() { int p1,p2,end; while( cin>>p1>>p2>>end ) { queue<PACM> q; PACM pacm; pacm=(ACM*)malloc(sizeof(ACM)); memset(visit,0,sizeof(visit)); visit[0][0]=1; pacm->last=NULL; pacm->steps=1; pacm->pone=p1; pacm->ptwo=0; pacm->path=1; q.push(pacm); visit[p1][0]=1; pacm->pone=0; pacm->ptwo=p2; pacm->path=2; q.push(pacm); visit[0][p2]=1; int steps=1; int fit=0; while( !q.empty() ) { pacm=q.front(); q.pop(); int pone=pacm->pone; int ptwo=pacm->ptwo; int path=pacm->path; steps=pacm->steps; if( pone==end || ptwo==end ) { fit=1; break; } PACM Pacm; Pacm=(ACM*)malloc(sizeof(ACM)); Pacm->last=pacm; steps++; Pacm->steps=steps; if( pone<p1 && !visit[p1][ptwo] ) // FILL(1) { Pacm->pone=p1; Pacm->ptwo=ptwo; Pacm->path=1; q.push(Pacm); visit[p1][ptwo]=1; } if( pone>0 && !visit[0][ptwo] ) // DROP(1) { Pacm->pone=0; Pacm->ptwo=ptwo; Pacm->path=3; q.push(Pacm); visit[0][ptwo]=1; } if( ptwo<p2 && !visit[pone][p2] ) // FILL(2) { Pacm->pone=pone; Pacm->ptwo=p2; Pacm->path=2; q.push(Pacm); visit[pone][p2]=1; } if( ptwo>0 && !visit[pone][0] ) // DROP(2) { Pacm->pone=pone; Pacm->ptwo=0; Pacm->path=4; q.push(Pacm); visit[pone][0]=1; } if( pone>0 && ptwo<p2 ) // POUR(1,2) { if( pone<p2-ptwo && !visit[0][pone+ptwo] ) { Pacm->pone=0; Pacm->ptwo=pone+ptwo; Pacm->path=5; q.push(Pacm); visit[0][pone+ptwo]=1; } if( pone>=p2-ptwo && !visit[pone-(p2-ptwo)][p2] ) { Pacm->pone=pone-(p2-ptwo); Pacm->ptwo=p2; Pacm->path=5; q.push(Pacm); visit[pone-(p2-ptwo)][p2]=1; } } if( ptwo>0 && pone<p1 ) //POUR(2,1) { if( ptwo<p1-pone && !visit[pone+ptwo][0] ) { Pacm->pone=ptwo+pone; Pacm->ptwo=0; Pacm->path=6; q.push(Pacm); visit[ptwo+pone][0]=1; } if( ptwo>=p1-pone && !visit[p1][ptwo-(p1-pone)] ) { Pacm->pone=p1; Pacm->ptwo=ptwo-(p1-pone); Pacm->path=6; q.push(Pacm); visit[p1][ptwo-(p1-pone)]=1; } } } if(fit) { cout<<steps<<endl; int k=0; while( pacm ) { data[k++]=pacm->path; pacm=pacm->last; } for( int i=k-1;i>=0;i--) cout<<road[data[i]]<<endl; } else cout<<"impossible"<<endl; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator