uva11218 - KTV

简单的暴力问题。。。。

三层循环,,,

为了省时,尽量每一层都加上判断。。

及时“剪枝"~~~

代码如下:

#include <cstdio>
#include <cstring>
int n, vis[15], a[100], b[100], c[100], s[100];
int main ()
{
    int cas = 0, max, sco;
    while(scanf("%d",&n),n)
    {
        max = sco = 0;
        memset(vis,0,sizeof(vis));
        for(int i = 0; i < n; i++) scanf("%d%d%d%d", &a[i],&b[i],&c[i],&s[i]);
        for(int i = 0; i < n; i++)
        {
            vis[a[i]] = vis[b[i]] = vis[c[i]] = 1;
            sco+=s[i];
            for(int j = i+1; j < n; j++)
            {
                if(vis[a[j]]||vis[b[j]]||vis[c[j]]||i==j) continue;
                vis[a[j]] = vis[b[j]] = vis[c[j]] = 1;
                sco+=s[j];
                for(int k = j+1; k < n; k++)
                {
                    if(vis[a[k]]||vis[b[k]]||vis[c[k]]||i==k||j==k) continue;
                    sco+=s[k];
//                    printf("%d %d %d-->%d\n",i,j,k,sco);
                    if(max<sco) max = sco;
                    sco-=s[k];
                }
                vis[a[j]] = vis[b[j]] = vis[c[j]] = 0;
                sco-=s[j];
            }
            vis[a[i]] = vis[b[i]] = vis[c[i]] = 0;
            sco-=s[i];
        }
        printf("Case %d: ",++cas);
        max==0?printf("-1\n"):printf("%d\n",max);
    }
    return 0;
}


你可能感兴趣的:(uva11218 - KTV)