题意:用三个瓶子,a瓶装满水,b,c是空瓶,Va=Vb+Vc,可以互相倒水,判断是否能将水平分。
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495
思路:隐式图,有6种倒水的情况,广搜即可。
注意点:代码量比较大,其他没什么问题。
以下为AC代码:
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
11926834 | 2014-10-21 00:33:12 | Accepted | 1495 | 15MS | 384K | 9251 B | G++ | luminous11 |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
bool vis[106][106];
int ra, rb, rc, res;
int swap ( int &a, int &b )
{
int tmp = a;
a = b;
b = tmp;
}
struct node
{
int a;
int b;
int c;
int t;
node( int x, int y, int z, int k )
{
a = x;
b = y;
c = z;
t = k;
}
};
int BFS ( int a, int b, int c )
{
int ta, tb, tc, tt;
struct node pt( a, b, c, 0 );
queue<node> q;
q.push( pt );
while ( ! q.empty() )
{
pt = q.front();
q.pop();
if ( pt.a == res )
{
return pt.t;
}
struct node tmp ( 0, 0, 0, 0 );
for ( int i = 0; i < 6; i ++ )
{
if ( i == 0 && pt.a )//1->2
{
if ( pt.b == rb )
{
continue;
}
else
{
if ( ( rb - pt.b ) > pt.a )
{
tmp.a = 0;
tmp.b = pt.b + pt.a;
tmp.c = pt.c;
tmp.t = pt.t + 1;
if ( ! vis[tmp.a][tmp.b] )
{
q.push( tmp );
vis[tmp.a][tmp.b] = true;
}
}
else
{
tmp.a = pt.a - ( rb - pt.b );
tmp.b = rb;
tmp.c = pt.c;
tmp.t = pt.t + 1;
if ( tmp.a == res )
{
return tmp.t;
}
if ( ! vis[tmp.a][tmp.b] )
{
vis[tmp.a][tmp.b] = true;
q.push( tmp );
}
}
}
}
if ( i == 1 && pt.a )//1->3
{
if ( pt.c == rc )
{
continue;
}
else
{
if ( pt.a > ( rc - pt.c ) )
{
tmp.a = pt.a - ( rc - pt.c );
tmp.b = pt.b;
tmp.c = rc;
tmp.t = pt.t + 1;
if ( tmp.a == res )
{
return tmp.t;
}
if ( ! vis[tmp.a][tmp.b] )
{
q.push ( tmp );
vis[tmp.a][tmp.b] = true;
}
}
else
{
tmp.a = 0;
tmp.b = pt.b;
tmp.c = pt.c + pt.a;
tmp.t = pt.t + 1;
if ( ! vis[tmp.a][tmp.b] )
{
vis[tmp.a][tmp.b] = true;
q.push ( tmp );
}
}
}
}
if ( i == 2 && pt.b )//2->3
{
if ( pt.c == rc )
{
continue;
}
else
{
if ( pt.b > ( rc - pt.c ) )
{
tmp.a = pt.a;
tmp.b = pt.b - ( rc - pt.c );
tmp.c = rc;
tmp.t = pt.t + 1;
if ( ! vis[tmp.a][tmp.b] )
{
q.push( tmp );
vis[tmp.a][tmp.b] = true;
}
}
else
{
tmp.a = pt.a;
tmp.b = 0;
tmp.c = pt.b + pt.c;
tmp.t = pt.t + 1;
if ( ! vis[tmp.a][tmp.b] )
{
q.push( tmp );
vis[tmp.a][tmp.b] = true;
}
}
}
}
if ( i == 3 && pt.b )//2->1
{
if ( ra == pt.a )
{
continue;
}
else
{
if ( pt.b > ( ra - pt.a ) )
{
tmp.a = ra;
tmp.b = pt.b - ( ra - pt.a );
tmp.c = pt.c;
tmp.t = pt.t + 1;
if ( ! vis[tmp.a][tmp.b] )
{
q.push ( tmp );
vis[tmp.a][tmp.b] = true;
}
}
else
{
tmp.a = pt.a + pt.b;
tmp.b = 0;
tmp.c = pt.c;
tmp.t = pt.t + 1;
if ( tmp.a == res )
{
return tmp.t;
}
if ( ! vis[tmp.a][tmp.b] )
{
q.push ( tmp );
vis[tmp.a][tmp.b] = true;
}
}
}
}
if ( i == 4 && pt.c )//3->1
{
if ( pt.a == ra )
{
continue;
}
else
{
if ( pt.c > ( ra - pt.a ) )
{
tmp.a = ra;
tmp.b = pt.b;
tmp.c = pt.c - ( ra - pt.a );
tmp.t = pt.t + 1;
if ( ! vis[tmp.a][tmp.b] )
{
vis[tmp.a][tmp.b] = true;
q.push ( tmp );
}
}
else
{
tmp.a = pt.a + pt.c;
tmp.b = pt.b;
tmp.c = 0;
tmp.t = pt.t + 1;
if ( tmp.a == res )
{
return tmp.t;
}
if ( ! vis[tmp.a][tmp.b] )
{
vis[tmp.a][tmp.b] = true;
q.push ( tmp );
}
}
}
}
if ( i == 5 && pt.c )//3->2
{
if ( pt.b == rb )
{
continue;
}
else
{
if ( pt.c > ( rb - pt.b ) )
{
tmp.a = pt.a;
tmp.b = rb;
tmp.c = pt.c - ( rb - pt.b );
tmp.t = pt.t + 1;
if ( ! vis[tmp.a][tmp.b] )
{
vis[tmp.a][tmp.b] = true;
q.push ( tmp );
}
}
else
{
tmp.a = pt.a ;
tmp.b = pt.b + pt.c;
tmp.c = 0;
tmp.t = pt.t + 1;
if ( ! vis[tmp.a][tmp.b] )
{
vis[tmp.a][tmp.b] = true;
q.push ( tmp );
}
}
}
}
}
}
return 0;
}
int main()
{
while ( cin >> ra >> rb >> rc )
{
if ( ! ra && ! rb && ! rc )
{
return 0;
}
if ( rb < rc )
{
swap ( rb, rc );
}
if ( ra & 1 )
{
cout << "NO" << endl;
continue;
}
res = ra / 2;
memset ( vis, false, sizeof ( vis ) );
vis[ra][0] = true;
int sum = BFS ( ra, 0, 0 );
if ( sum == 0 )
{
cout << "NO" << endl;
}
else
{
cout << sum << endl;
}
}
return 0;
}