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 |
哪位大神帮忙看看 为什么是Time Limit Exceeded#include<cstdlib> #include<cstring><span style="color:#ff0000;"> #include<iostream> #include<cstdio> #include<queue> using namespace std; int a,b,c; int sum; int vis[101][101]; struct pot{ int wa; int wb; int ans; string str; }; pot start,now; bool bfs(){ queue<pot> q; q.push(start); pot next; while(!q.empty()){ now=q.front(); q.pop(); if(now.wa==c||now.wb==c){ sum=now.ans; printf("%d",sum); cout<<now.str<<endl; return true; } if(vis[now.wa][now.wb]==0){//fill a vis[now.wa][now.wb]==1; next.wb=now.wb; next.wa=a; next.ans=now.ans+1; next.str=now.str+"\nFILL(1)"; q.push(next); } if(vis[now.wa][now.wb]==0){//fill b vis[now.wa][now.wb]==1; next.wa=now.wa; next.wb=b; next.ans=now.ans+1; next.str=now.str+"\nFILL(2)"; q.push(next); } if(vis[now.wa][now.wb]==0){//drop a vis[now.wa][now.wb]==1; next.wb=now.wb; next.wa=0; next.ans=now.ans+1; next.str=now.str+"\nDROP(1)"; q.push(next); } if(vis[now.wa][now.wb]==0){//drop b vis[now.wa][now.wb]==1; next.wa=now.wa; next.wb=0; next.ans=now.ans+1; next.str=now.str+"\nDROP(2)"; q.push(next); } if(vis[now.wa][now.wb]==0){//a倒入b if(now.wa+now.wb<=b){ vis[now.wa][now.wb]==1; next.wa=0; next.wb=now.wa+now.wb; next.ans=now.ans+1; next.str=now.str+"\nPOUR(1,2)"; q.push(next); } else{ vis[now.wa][now.wb]==1; next.wb=b; next.wa=now.wa-b+now.wb; next.ans=now.ans+1; next.str=now.str+"\nPOUR(1,2)"; q.push(next); } } if(vis[now.wa][now.wb]==0){//b倒入a if(now.wa+now.wb<=a){ vis[now.wa][now.wb]==1; next.wb=0; next.wa=now.wa+now.wb; next.ans=now.ans+1; next.str=now.str+"\nPOUR(2,1)"; q.push(next); } else{ vis[now.wa][now.wb]==1; next.wa=a; next.wb=now.wb-a+now.wa; next.ans=now.ans+1; next.str=now.str+"\nPOUR(2,1)"; q.push(next); } } } } int main(){ scanf("%d%d%d",&a,&b,&c); memset(vis,0,sizeof(vis)); if(bfs()) ; else printf("impossible\n"); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator