PersonalCorpus 版 (精华区)
36647 2002-09-24 18:11:21 Accepted
1005 C++ 00:00.02 1372K Big Guava
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <math.h>
#include <ctype.h>
char path[1000];
bool used [1001][1001];
int st0,st1;
int a,b,n;
bool ok;
void tryit(int dep)
{
if (ok) return;
if ((st0==n)||(st1==n)) {
ok=true;
for (int i=0;i<dep;i++) {
if (path[i]==1) printf("fill A\n");
if (path[i]==2) printf("fill B\n");
if (path[i]==3) printf("empty A\n");
if (path[i]==4) printf("empty B\n");
if (path[i]==5) printf("pour A B\n");
if (path[i]==6) printf("pour B A\n");
}
return;
}
int savea,saveb;
if ((st0!=a)&&(path[dep-1]!=3)) {
// fill A
savea=st0;
st0=a;
path[dep]=1;
if (!used[st0][st1]) {
used[st0][st1]=true;
tryit(dep+1);
used[st0][st1]=false;
}
st0=savea;
}
if ((st1!=b)&&(path[dep-1]!=4)) {
// fill B
saveb=st1;
st1=b;
path[dep]=2;
if (!used[st0][st1]) {
used[st0][st1]=true;
tryit(dep+1);
used[st0][st1]=false;
}
st1=saveb;
}
if ((st0!=0)&&(path[dep-1]!=1)) {
// empty A
savea=st0;
st0=0;
path[dep]=3;
if (!used[st0][st1]) {
used[st0][st1]=true;
tryit(dep+1);
used[st0][st1]=false;
}
st0=savea;
}
if ((st1!=0)&&(path[dep-1]!=2)) {
// empty B
saveb=st1;
st1=0;
path[dep]=4;
if (!used[st0][st1]) {
used[st0][st1]=true;
tryit(dep+1);
used[st0][st1]=false;
}
st1=saveb;
}
if ((st1!=b)&&(st0!=0)) {
// pour A B
savea=st0;
saveb=st1;
st0+=st1;
if (st0>=b) st1=b; else st1=st0;
st0-=st1;
path[dep]=5;
if (!used[st0][st1]) {
used[st0][st1]=true;
tryit(dep+1);
used[st0][st1]=false;
}
st0=savea;
st1=saveb;
}
if ((st1!=0)&&(st0!=a)) {
// pour B A
savea=st0;
saveb=st1;
st1+=st0;
if (st1>=a) st0=a; else st0=st1;
st1-=st0;
path[dep]=6;
if (!used[st0][st1]) {
used[st0][st1]=true;
tryit(dep+1);
used[st0][st1]=false;
}
st0=savea;
st1=saveb;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("c:\\0acm\\zju\\i1005","r",stdin);
freopen("c:\\0acm\\zju\\o1005","w",stdout);
#endif
while (scanf("%d%d%d\n",&a,&b,&n)==3) {
st0=st1=0;
path[0]=10000; ok=false;
memset(used,0,sizeof(used));
tryit(1);
printf("success\n");
}
return 0;
}
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.332毫秒