您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: