poj1606

简单题

View Code
   
     
#include < iostream >
#include
< cstdio >
#include
< cstdlib >
#include
< cstring >
using namespace std;

#define maxn 105

struct Node
{
int va, vb, step, pre, opr;
} q[maxn
* maxn];

int a, b, c, l, r;
bool vis[maxn][maxn];
int stk[maxn * maxn], top;

void make( int va, int vb, int opr)
{
if (vis[va][vb])
return ;
vis[va][vb]
= true ;
q[r].va
= va;
q[r].vb
= vb;
q[r].step
= q[l].step + 1 ;
q[r].opr
= opr;
q[r].pre
= l;
r
++ ;
}

void output()
{
top
= 0 ;
while (l != 0 )
{
stk[top
++ ] = q[l].opr;
l
= q[l].pre;
}
for ( int i = top - 1 ; i >= 0 ; i -- )
switch (stk[i])
{
case 1 :
printf(
" fill A\n " );
break ;
case 2 :
printf(
" fill B\n " );
break ;
case 3 :
printf(
" empty A\n " );
break ;
case 4 :
printf(
" empty B\n " );
break ;
case 5 :
printf(
" pour B A\n " );
break ;
case 6 :
printf(
" pour A B\n " );
break ;
}
printf(
" success\n " );
}

void bfs()
{
q[
0 ].va = 0 ;
q[
0 ].vb = 0 ;
q[
0 ].step = 0 ;
vis[
0 ][ 0 ] = true ;
l
= 0 ;
r
= 1 ;
while (l != r)
{
if (q[l].vb == c)
{
output();
return ;
}
int xa, xb, x;
xa
= a;
xb
= q[l].vb;
make(xa, xb,
1 );
xa
= q[l].va;
xb
= b;
make(xa, xb,
2 );
xa
= 0 ;
xb
= q[l].vb;
make(xa, xb,
3 );
xa
= q[l].va;
xb
= 0 ;
make(xa, xb,
4 );
x
= min(a - q[l].va, q[l].vb);
xa
= q[l].va + x;
xb
= q[l].vb - x;
make(xa, xb,
5 );
x
= min(b - q[l].vb, q[l].va);
xa
= q[l].va - x;
xb
= q[l].vb + x;
make(xa, xb,
6 );
l
++ ;
}
}

int main()
{
// freopen("t.txt", "r", stdin);
while (scanf( " %d%d%d " , & a, & b, & c) != EOF)
{
memset(vis,
0 , sizeof (vis));
bfs();
}
return 0 ;
}

你可能感兴趣的:(poj)