zoj 3158 DFS

Li Lei and Han Meimei love each other so much that they can not be separated for even one minute. They have promised each other to share all the happy things and delicious foods. In the Saint Valentine’s Day in 2009, they bought one big tasty cake and tried to divide it into two shares with only one cut. Here comes the problem:

The cake they bought is in the shape of rectangle which is made up of m*n units(each units is a 1*1 square). Since the cake is delicious, each unit in the cake has a nutrition_num. For each unit, the nutrition_num can be either positive or negative because there may be various kinds of nutrition in different kinds of food(either useful or harmful for our health). They want to divide the cake into two shares with the difference between the total nutrition of the two does not exceed an expeced value t(The total nutrition of a share is defined as the sum of all the units’ nutrition_num in it). If it is possible, they’ll eat their cake happily. Otherwise, they may buy another one =_=b

When cutting the cake, they’ll keep their knife moving just along the edge between two units in order to save more nutrition(we can assume that the center of one unit contains the main nurition). They’ll cut the cake from the upside to the downside(from line 1 to line m) without moving the knife off the cake. Each cutting must be forward, which means it is not allowed to move to the units in line k-1 next time when the knife is in line k(1 <= k <= m). And to make the two shares they finally get in good shape, they never take the knife up before it reachs the downside.

The picture below shows you a possible cut for a given cake:

这里写图片描述

In the picture, the two shares they get will be the two parts divided by the bold line. The total nutrition of the left part will be 1+2+4+5+(-6)+0+7+(-1)=12, and the nutrition of the right one will be (-9)+7+2+4+(-3)+1+3+2=7, so there difference is 5.

Now you are given the description for the cake they’ve got, could you write a program to tell them whether they can achieve their goal?

Input

The input file will contain multiple test cases.
In each case, the first line contains two integers m and n(1 <= m,n <=7) showing the size of the cake.Then m lines follow, each line contains n integers.Every interger represents the nutrition_num for the corresponding unit in the cake.The last line for each case contains only one positive integer t, the expected value they wish the difference of the two shares be.

Output

For each test case, if it is possible to find a way cutting the cake up to their expectation, output the minimum difference you can get in a single line. Output “You’d better buy another one!” in a single line otherwise.

Sample Input

2 2
1 2
3 4
5
3 3
1 -2 9
2 10 33
-100 2 4
7
Sample Output

2
You’d better buy another one!

思路就是:暴力搜每行的切边,sum[i][j] 第i行1~j的和 ,坑点就是不能切边缘

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
int sum[9][9],sum2;
int m,n,t,a;
const int inf=9999999;
int ans;
void dfs(int step,int cur) {

    if(step==m+1) {
        if(abs((sum2-cur)-cur)>t) return;
    //  cout<<ans<<endl;
        ans=min(ans,abs((sum2-cur)-cur));
        return;
    }
    for(int i=1;i<n;i++) {
        dfs(step+1,cur+sum[step][i]);
    }
}

int main() {
//  freopen("input.txt","r",stdin);
    while(scanf("%d%d",&m,&n)!=EOF) {
        sum2=0;
        for(int i=1;i<=m;i++) {
            for(int j=1;j<=n;j++) {
                scanf("%d",&a);
                sum2+=a;
                sum[i][j]=sum[i][j-1]+a;
            }
        }
        scanf("%d",&t);
        ans=inf;
        dfs(1,0);
        if(ans==inf) printf("You'd better buy another one!\n");
        else printf("%d\n",ans);
    }   

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值