题意:给出三个水壶,体积分别为A、B、C,(A=B+C),在这三个水壶之间互相倒水,是否能将这壶水平分成两份
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495
思路:对每个体积记录并广搜
注意点:无
以下为AC代码:
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
13001113 | 2015-02-28 17:17:58 | Accepted | 1495 | 124MS | 2324K | 3625 B | G++ | luminous |
#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 <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
//const double pi = acos(-1);
//const double eps = 1e-10;
//const int dir[[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
int a, b, c;
int ans;
int sum;
bool vis[105][105][105];
struct node{
int a, b, c, cnt;
node(){}
node( int _a, int _b, int _c, int _cnt ) : a(_a), b(_b), c(_c), cnt(_cnt) {}
};
inline bool judge ( const node & tmp )
{
int cnt = 0;
if ( tmp.a == sum )
cnt ++;
if ( tmp.b == sum )
cnt ++;
if ( tmp.c == sum )
cnt ++;
if ( cnt >= 2 ){
ans = tmp.cnt;
return true;
}
return false;
}
inline bool init()
{
clr ( vis, 0 );
cin >> a >> b >> c;
if ( a || b || c )
return true;
else
return false;
}
bool solve()
{
if ( a & 1 )return false;
sum = a / 2;
queue<node> q;
q.push ( node( a, 0, 0, 0 ) );
vis[a][0][0] = 1;
while ( ! q.empty() ){
node tmp = q.front();
q.pop();
if ( judge( tmp ) ){
return true;
}
if ( tmp.a && tmp.b < b ){
int t = min ( tmp.a, b - tmp.b );
if ( vis[tmp.a-t][tmp.b+t][tmp.c] == 0 ){
vis[tmp.a-t][tmp.b+t][tmp.c] = 1;
q.push ( node( tmp.a-t, tmp.b+t, tmp.c, tmp.cnt+1 ) );
}
}
if ( tmp.a && tmp.c < c ){
int t = min ( tmp.a, c - tmp.c );
if ( vis[tmp.a-t][tmp.b][tmp.c+t] == 0 ){
vis[tmp.a-t][tmp.b][tmp.c+t] = 1;
q.push ( node ( tmp.a-t, tmp.b, tmp.c+t, tmp.cnt+1 ) );
}
}
if ( tmp.b && tmp.a < a ){
int t = min ( tmp.b, a - tmp.a );
if ( vis[tmp.a+t][tmp.b-t][tmp.c] == 0 ){
vis[tmp.a+t][tmp.b-t][tmp.c] = 1;
q.push ( node( tmp.a+t, tmp.b-t, tmp.c, tmp.cnt+1 ) );
}
}
if ( tmp.b && tmp.c < c ){
int t = min ( tmp.b, c - tmp.c );
if ( vis[tmp.a][tmp.b-t][tmp.c+t] == 0 ){
vis[tmp.a][tmp.b-t][tmp.c+t] = 1;
q.push ( node ( tmp.a, tmp.b-t, tmp.c+t, tmp.cnt+1 ) );
}
}
if ( tmp.c && tmp.a < a ){
int t = min ( tmp.c, a - tmp.a );
if ( vis[tmp.a+t][tmp.b][tmp.c-t] == 0 ){
vis[tmp.a+t][tmp.b][tmp.c-t] = 1;
q.push ( node ( tmp.a+t, tmp.b, tmp.c-t, tmp.cnt+1 ) );
}
}
if ( tmp.c && tmp.b < b ){
int t = min ( tmp.c, b - tmp.b );
if ( vis[tmp.a][tmp.b+t][tmp.c-t] == 0 ){
vis[tmp.a][tmp.b+t][tmp.c-t] = 1;
q.push ( node ( tmp.a, tmp.b+t, tmp.c-t, tmp.cnt+1 ) );
}
}
}
}
int main()
{
ios::sync_with_stdio( false );
while ( init() ){
if ( solve() ){
cout << ans << endl;
}
else{
cout << "NO" << endl;
}
}
return 0;
}