POJ 1067(广搜水题)
2012-07-05 11:06
337 查看
参考某大牛的算法,还写这么久,真悲剧
#include <iostream> #include <cstdio> #include <cstdlib> using namespace std; #define Afull 1 #define Bfull 2 #define Aempty 3 #define Bempty 4 #define AtoB 5 #define BtoA 6 int head,tail; char step[7][10]= {"0","fill A","fill B","empty A","empty B","pour A B","pour B A"}; struct state { int mark; int a; int b; struct state *front; }; state Q[100000]; void enq(state a) { Q[head++]=a; } state outq() { return Q[tail++]; } int check(state a) { for(int i=0; i<head; i++) { if(Q[i].a==a.a&&Q[i].b==a.b) return 1; } return 0; } int empty() { if(head==tail) return 1; else return 0; } int main() { int A,B,N,i; state temp,*point,s,*search,*result[100000]; while(scanf("%d%d%d",&A,&B,&N)!=EOF) { head=0; tail=0; temp.a=0; temp.b=0; temp.mark=0; temp.front=NULL; enq(temp); while(!empty()) { temp=outq(); point=&Q[tail-1]; if(temp.a==N||temp.b==N) break; s=temp; s.a=A; if(!check(s)&&temp.a!=A) { s.front=point; s.mark=Afull; enq(s); } s=temp; s.b=B; if(!check(s)&&temp.b!=B) { s.front=point; s.mark=Bfull; enq(s); } s=temp; s.a=0; if(!check(s)&&temp.a) { s.front==point; s.mark=Aempty; enq(s); } s=temp; s.b=0; if(!check(s)&&temp.b) { s.front=point; s.mark=Bempty; enq(s); } s=temp; if(s.a!=0&&s.b<B&&s.a<=B-s.b) { s.b+=s.a; s.a=0; if(!check(s)) { s.front=point; s.mark=AtoB; enq(s); } } else { if(s.a!=0&&s.b<B) { s.a-=(B-s.b); s.b=B; if(!check(s)) { s.front=point; s.mark=AtoB; enq(s); } } } if(s.b!=0&&s.a<A&&s.b<=A-s.a) { s.a+=s.b; s.b=0; if(!check(s)) { s.front=point; s.mark=BtoA; enq(s); } } else { if(s.b!=0&&s.a<A) { s.a=A; s.b-=(A-s.b); if(!check(s)) { s.front=point; s.mark=BtoA; enq(s); } } } } search=point; for(i=0; search!=NULL; i++) { result[i]=search; search=search->front; } for(i-=2; i>=0; i--) puts(step[result[i]->mark]); printf("success\n"); } return 0; }
相关文章推荐
- poj 1411 素数 水题
- POJ 1067 取石子游戏
- UVALive5520 UVA305 POJ1012 HDU1443 Joseph【数学计算+打表+水题】
- poj 3009 DFS水题
- poj 1503(水题)
- POJ 2029 Get Many Persimmon Trees(水题)
- POJ 3086 Triangular Sums(水题)
- POJ 1007 DNA(水题)
- 算法导论第二章POJ水题
- poj-1067 取石子游戏
- POJ 2545+2591+2247+1338简单水题
- THE DRUNK JAILER - POJ 1218 水题
- poj入门水题整理1--按刷题顺序解释
- Poj 1316 Self Numbers(水题)
- The Hardest Problem Ever - POJ 1298 水题
- POJ 1118 Lining Up 水题一道
- poj 1951 Extra Krunch 字符串处理水题
- POJ 3369 Meteor Shower (BFS,水题)
- POJ 3628 Bookshelf 2 0-1背包 or DFS 水题
- [ACM] poj 1067 取石子游戏(威佐夫博奕)