poj 3740 dfs

60 篇文章 0 订阅

Easy Finding
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17610 Accepted: 4825

Description

Given a  M× N matrix  AA ij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn contains and only contains one 1.

Input

There are multiple cases ended by  EOF. Test case up to 500.The first line of input is  MN ( M ≤ 16,  N ≤ 300). The next  M lines every line contains  N integers separated by space.

Output

For each test case, if you could find it output "Yes, I found it", otherwise output "It is impossible" per line.

Sample Input

3 3
0 1 0
0 0 1
1 0 0
4 4
0 0 0 1
1 0 0 0
1 1 0 1
0 1 0 0

Sample Output

Yes, I found it
It is impossible



题意:就是任意选几行组成一行,在这一行中全部为 1  ,不能多也不能少于 1 

c++比G++时间用的短一些


正确代码

注意的地方:

两个减枝的时候比较合适,如果在其他地方放,就会比较麻烦

这里的套路就是 减枝和准备进行下一层调用与回溯分开的,这样避免了很多没有必要的事情

但另外一个调用judge_2的时候若是提到dfs 开头,但是这样会加大时间,会有时候调用没有那个必要的

所以放到后面

这里减少了第二种方法的循环,本程序就是通过最后的再一次进行递归调用等效替代的,pos就是循环因子

#include<stdio.h>
#include<string.h>

int a[20][310];
int temp[310];
int m,n;

//-------------判断是不是全为 1-----------------//
int judge_1()
{
    for(int i=0;i<n;i++)
        if(temp[i]!=1) return 0;
    return 1;
}
//-------------判断是不是超出1------------------//
int judge_2()
{
    for(int i=0;i<n;i++)
        if(temp[i]>1) return 0;
    return 1;
}
//-------------深搜+回溯-------------------------//
int dfs(int pos)
{
//-----------------减枝--------------------//
    if(judge_1())
        return 1;
    if(pos>=m)
        return judge_1();

//-----------------搜索--------------------//
    for(int i=0;i<n;i++)
        temp[i]+=a[pos][i];
    if(judge_2())
        if(dfs(pos+1))
            return 1;

//-----------------回溯--------------------//
    for(int i=0;i<n;i++)
        temp[i]-=a[pos][i];

    if(dfs(pos+1))
        return 1;
    return 0;

}

int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        memset(temp,0,sizeof(temp));
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
                scanf("%d",&a[i][j]);

        if(dfs(0))
            printf("Yes, I found it\n");
        else
            printf("It is impossible\n");
    }
    return 0;
}


自己写的超时代码

循环太多了,数据比较大

#include<stdio.h>
#include<string.h>

int a[40][400];
int temp[400];
int m,n;
int used[40];

int dfs()
{
    int tag=1;
    for(int i=0;i<n;i++){
        if(temp[i]>1)
            return 0;
        if(temp[i]<1){
            tag=0;
            break;
        }
    }
    if(tag)
        return 1;


    for(int i=0;i<m;i++){
        if(!used[i]){
            for(int j=0;j<n;j++)
                temp[j]+=a[i][j];
            used[i]=1;

            if(dfs())
                return 1;
            else{
                for(int j=0;j<n;j++)
                    temp[j]-=a[i][j];
                used[i]=0;
            }
        }
    }
}

int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        memset(temp,0,sizeof(temp));
        memset(used,0,sizeof(used));
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
                scanf("%d",&a[i][j]);


        if(dfs())
            printf("Yes, I found it\n");
        else
            printf("It is impossible\n");
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值