Headmaster's Headache UVA - 10817

被这个bug卡了一个晚上,以后在也不写没有返回值的dfs了。。。

WA原因:当同一种状况再次发生时直接否定这个状况


AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>

using namespace std;

#define INF 0x3f3f3f3f
#define LL long long

struct teach
{
    int t;     //count
    int c[10];
    int co;     //spend
}tea[110];

int me[1 << 16][105];

int a, b;
char ch;
int sum;
int s, n, m;
int init;
int ans;
int mx;

int dp(int status, int num)
{
    int &k = me[status][num];

    if(k != INF)
        return k;

    if(num < n)
    {

        int a, b = status;
        int i, j;
        for(i = 0; i < tea[num].t; i++)
        {
            a = tea[num].c[i];

            if(b & (1 << (a + s)))
            {

            }
            else if(b & (1 << a))
            {
                b += (1 << (a + s));
            }
            else
            {
                b += (1 << a);
            }
        }
        k = min(k, min(dp(status, num + 1), dp(b, num + 1) + tea[num].co));
    }
    if(k == INF)
    {
        k = INF + 1;
    }
    return k;
}

int main()
{
    int i, j, k;



    while(scanf("%d %d %d", &s, &m, &n) != EOF)
    {
        ans = INF;
        memset(me, 0, sizeof(me));
        mx = (1 << (s * 2)) - 1;

        memset(tea, 0, sizeof(tea));
        init = 0;
        sum = 0;

        if(!s)
            break;
        for(i = 0; i < m; i++)
        {
            scanf("%d", &k);
            sum += k;
            while(scanf("%c", &ch))
            {
                if(ch >= '1' && ch <= '8')
                {
                    a = ch - '1';

                    if(init & (1 << (a + s)))
                    {

                    }
                    else if(init & (1 << a))
                    {
                        init += (1 << (a + s));
                    }
                    else
                    {
                        init += (1 << a);
                    }
                }
                else if(ch == '\n')
                    break;
            }
        }

        for(i = 0; i < n; i++)
        {
            scanf("%d", &tea[i].co);
            while(scanf("%c", &ch))
            {
                if(ch == '\n')
                    break;
                else if(ch >= '1' && ch <= '8')
                {
                    tea[i].c[tea[i].t++] = ch - '1';
                  //  cout << tea[i].t << endl;
                }
            }
        }

        for(int i = init; i < mx ;i++)
            for(int j = 0; j < n + 1; j++)
                me[i][j] = INF;
        for(int i = 0; i < n + 1; i++)
            me[mx][i] = 0;

        cout << dp(init, 0) + sum << endl;

    }
    return 0;
}

WA代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>

using namespace std;

#define INF 0x3f3f3f3f
#define LL long long

struct teach
{
    int t;     //count
    int c[10];
    int co;     //spend
}tea[110];

int me[1 << 16][10];

int a, b;
char ch;
int sum;
int s, n, m;
int init;
int ans;
int mx;

void dfs(int status, int num, int sums)
{
    if(status == mx)
    {
        ans = min(ans, sums);
        return ;
    }

    if(me[status][num])
    {
        return ;
    }

    me[status][num]++;

    if(num >= n)
        return ;

    dfs(status, num + 1, sums);

    int a, b = status;
    int i, j;
    for(i = 0; i < tea[num].t; i++)
    {
        a = tea[num].c[i];

        if(b & (1 << (a + s)))
        {

        }
        else if(b & (1 << a))
        {
            b += (1 << (a + s));
        }
        else
        {
            b += (1 << a);
        }
    }

    dfs(b, num + 1, sums + tea[num].co);
}

int main()
{
    int i, j, k;



    while(scanf("%d %d %d", &s, &m, &n) != EOF)
    {
        ans = INF;
        memset(me, 0, sizeof(me));
        mx = (1 << (s * 2)) - 1;

        memset(tea, 0, sizeof(tea));
        init = 0;
        sum = 0;
        if(!s)
            break;
        for(i = 0; i < m; i++)
        {
            scanf("%d", &k);
            sum += k;
            while(scanf("%c", &ch))
            {
                if(ch >= '1' && ch <= '8')
                {
                    a = ch - '1';

                    if(init & (1 << (a + s)))
                    {

                    }
                    else if(init & (1 << a))
                    {
                        init += (1 << (a + s));
                    }
                    else
                    {
                        init += (1 << a);
                    }
                }
                else if(ch == '\n')
                    break;
            }
        }

        for(i = 0; i < n; i++)
        {
            scanf("%d", &tea[i].co);
            while(scanf("%c", &ch))
            {
                if(ch == '\n')
                    break;
                else if(ch >= '1' && ch <= '8')
                {
                    tea[i].c[tea[i].t++] = ch - '1';
                  //  cout << tea[i].t << endl;
                }
            }
        }

     //   cout << mx << ' ' << init;
        dfs(init, 0, 0);

        cout << ans + sum << endl;

    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值