1. 程式人生 > 實用技巧 >hdu6789 Fight(暴力列舉)

hdu6789 Fight(暴力列舉)

#include<bits/stdc++.h>
typedef  long long ll ;
#define mod 99999997
#define gcd __gcd
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define int ll
using namespace std;
const int maxn = 1e5+9;
const int N = 35;

//此題要抓住本質,最優解與打架的先後順序無關,只與ab,bc,ac各打了幾次
//所以列舉ab,bc打了幾次.就能確定ac打了幾次答案取最小值
void solve(){
    int x , y , z ;
    scanf("%lld%lld%lld" , &x , &y , &z);
    int xy = di((ll)1000 , max(x , y));
    int yz = di((ll)1000 , max(y , z));
    int ans = INF;
    rep(i , 0 , xy){//列舉ab
        int fa = 1000 - i*y;
        int fb = 1000 - i*x;
        int fc = 1000 ;
        rep(j , 0 , yz){
            int b = fb - j*z;
            int c = fc - j*y;
            int a = fa ;
            int k = 0;
            if(a > 0 && b > 0 && c > 0){
                if(di(a , z) == di(c , x)){//如果ac能夠同時消滅,則是一組解,否則不能組成一組解
                    ans = min(ans , i+j+di(a , z));
                }
                continue;
            }
            if(a > 0 && b > 0 && c <= 0){//c已經被消滅
                k = min(di(a , y) , di(b , x));
                ans = min(ans , i+j+k);
            }
            else if(b > 0 && c > 0 && a <= 0){//a已經被消滅
                k = min(di(b , z) , di(c , y));
                ans = min(ans , i+j+k);
            }
            else if(a > 0 && c > 0 && b <= 0){//b已經被消滅
                k = min(di(a , z) , di(c , x));
                ans = min(ans , i+j+k);
            }
            else{//已經有兩個消滅了
                ans = min(ans , i+j);
            }
        }
    }
    printf("%lld\n" , ans);
}

signed main()
{
/*#ifdef ONLINE_JUDGE
#else
    freopen("D:\\c++\\in.txt", "r", stdin);
#endif*/
    int t ; scanf("%lld" , &t); while(t--)
        solve();
}