ZOJ Problem Set - 1005 Jugs
2012-02-23 14:56
369 查看
简单BFS
传说中一种特判: 不能理解。
#include <iostream> #include <queue> using namespace std; struct Node { int a,b; string str; }; queue <Node> qt; bool cot[1001][1001]; int main() { int aV,bV,aimV,i,j; Node tmp1,tmp,sta; while(cin>>aV>>bV>>aimV) { for(i = 0;i <= bV;i ++) for(j = 0;j <= bV;j ++) cot[i][j] = 0; cot[0][0] = 1; sta.a = sta.b = 0; sta.str = ""; qt.push(sta); while(!qt.empty()) { tmp = qt.front();qt.pop();tmp1 = tmp; //cout<<tmp.a<<" "<<tmp.b<<" "<<tmp.str<<endl; tmp1.a = aV; if(tmp1.b == aimV) { for(i = 0;i < tmp1.str.size();i ++) { switch ( tmp1.str[i]){ case '1': cout<<"fill A"<<endl;break; case '2': cout<<"fill B"<<endl;break; case '3': cout<<"empty A"<<endl;break; case '4': cout<<"empty B"<<endl;break; case '5': cout<<"pour A B"<<endl;break; case '6': cout<<"pour B A"<<endl;break; } } cout<<"success"<<endl; break; } if(cot[tmp1.a][tmp1.b] == 0) { tmp1.str += '1'; qt.push(tmp1); cot[tmp1.a][tmp1.b] = 1; } tmp1 = tmp; tmp1.b = bV; if(cot[tmp1.a][tmp1.b] == 0) { tmp1.str += '2'; qt.push(tmp1); cot[tmp1.a][tmp1.b] = 1; } tmp1 = tmp; tmp1.a = 0; if(cot[tmp1.a][tmp1.b] == 0) { tmp1.str += '3'; qt.push(tmp1); cot[tmp1.a][tmp1.b] = 1; } tmp1 = tmp; tmp1.b = 0; if(cot[tmp1.a][tmp1.b] == 0) { tmp1.str += '4'; qt.push(tmp1); cot[tmp1.a][tmp1.b] = 1; } tmp1 = tmp; if(tmp1.a + tmp1.b <= bV) { tmp1.b += tmp1.a;tmp1.a = 0; if(cot[tmp1.a][tmp1.b] == 0) { tmp1.str += '5'; qt.push(tmp1); cot[tmp1.a][tmp1.b] = 1; } } else { tmp1.a -= (bV - tmp1.b); tmp1.b = bV; if(cot[tmp1.a][tmp1.b] == 0) { tmp1.str += '5'; qt.push(tmp1); cot[tmp1.a][tmp1.b] = 1; } } tmp1 = tmp; if(tmp1.a + tmp1.b <= aV) { tmp1.a += tmp1.b;tmp1.b = 0; if(cot[tmp1.a][tmp1.b] == 0) { tmp1.str += '6'; qt.push(tmp1); cot[tmp1.a][tmp1.b] = 1; } } else { tmp1.b -= (aV - tmp1.a);tmp1.a = aV; if(cot[tmp1.a][tmp1.b] == 0) { tmp1.str += '6'; qt.push(tmp1); cot[tmp1.a][tmp1.b] = 1; } } } while(!qt.empty()) qt.pop(); } return 0; }
传说中一种特判: 不能理解。
#include <stdio.h> int main(){ int ca,cb,n; int a,b,bnow; int i; while (scanf("%d%d%d",&ca,&cb,&n) != EOF) { a = b = 0; while (b != n) { for ( i=0; i<=(cb-b)/ca; i++) { printf ("fill A\n"); printf ("pour A B\n"); bnow = b; bnow = b+ca; if (bnow == n)break; } if (bnow == n) break; printf ("empty B\n"); printf ("pour A B\n"); a = ca-(cb-b)%ca; b=a; if(b==n)break; } printf ("success\n"); } return 0; }
相关文章推荐
- ZOJ Problem Set - 1005 Jugs
- ZOJ Problem Set - 1005 Jugs
- ZOJ Problem Set - 1005
- ZOJ Problem Set - 1005
- ZOJ Problem Set - 1005
- ZOJ Problem Set - 1005
- ZOJ Problem Set - 1970 All in All
- ZOJ Problem Set - 1365 Mileage Bank
- ZOJ Problem Set - 1045 HangOver
- ZOJ Problem Set - 1004 Anagrams by Stack
- ZOJ Problem Set - 1760 Doubles
- ZOJ Problem Set - 2417 Lowest Bit
- 日期问题模版ZOJ Problem Set -3950 How Many NinesZOJ 找9
- ZOJ Problem Set - 1201 Inversion
- ZOJ Problem Set - 3665 Yukari's Birthda
- ZOJ Problem Set - 3782 Ternary Calculation
- ZOJ Problem Set - 3317 Murder in Restaurant
- ZOJ Problem Set - 2947 Abbreviation
- ZOJ Problem Set - 1061
- ZOJ Problem Set - 2027