[ACM] POJ 3740 Easy Finding (DFS)

来源:互联网 发布:苹果看视频软件 编辑:程序博客网 时间:2024/05/16 20:28
Easy Finding
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 16482 Accepted: 4476

Description

Given a M×N matrix AAij ∈ {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 30 1 00 0 11 0 04 40 0 0 11 0 0 01 1 0 10 1 0 0

Sample Output

Yes, I found itIt is impossible

Source

POJ Monthly Contest - 2009.08.23, MasterLuo

 

解题思路:

题意为给出n*m的01矩阵,问能不能从中选出一些行,使得这些行组成的新矩阵中,每列有且只有一个1.

本题用Dlx可解,明显的完全覆盖问题。这里练习DFs,用DFS按照行号递增的顺序枚举行就可以.一开始犯了一个很严重的错误,就是没有按行号递增的顺序,导致了重复,其实 1 2 5和 5 1 2其实是一样的,所以DFS(int row), 参数是行号,下一层递归为DFs(row+1), 每次进入DFs都要首先判断是否符合题意,即新矩阵每列有且只有一个1,用vis[j]数组表示新矩阵中第j列是否已经有一个1,如果满足该条件,就不用往下继续递归了,直接标记成功,return即可。

通过这道题,加深了对递归的理解,感觉只要想明白,递归也不是那么难理解。

假设当n=4时,搜索的行号是按这样的顺序:

1
1 2
1 2 3
1 2 3 4
1 2 4
1 3
1 3 4
1 4
2
2 3
2 3 4
2 4
3
3 4
4

代码:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <algorithm>  
  4. #include <string.h>  
  5. #include <stdlib.h>  
  6. #include <cmath>  
  7. #include <iomanip>  
  8. #include <vector>  
  9. #include <set>  
  10. #include <map>  
  11. #include <stack>  
  12. #include <queue>  
  13. #include <cctype>  
  14. using namespace std;  
  15. #define ll long long  
  16. const int maxn=18;  
  17. const int maxm=302;  
  18. int mp[maxn][maxm];  
  19. int n,m;  
  20. bool vis[maxm];// vis[i]表示第i列是否有一个1了  
  21. bool ok;  
  22. int hash[maxn];//第i行有多少个1  
  23.   
  24. bool yes(int row)//判断第row行可不可行  
  25. {  
  26.     for(int i=1;i<=m;i++)  
  27.         if(mp[row][i]&&vis[i])  
  28.         return false;  
  29.     return true;  
  30. }  
  31.   
  32. void DFS(int cnt,int row)//参数cnt为当前所选的行里面列已经有多少个1,row是当前选择的第几行  
  33. {  
  34.     if(cnt==m||(row>n&&cnt==m))///这里也要注意,当选择的行号大于n时,也要判断已选择的行号是否满足题意  
  35.     {  
  36.         ok=1;  
  37.         return;  
  38.     }  
  39.     if(row>n)  
  40.         return;  
  41.     for(int i=row;i<=n;i++)///所犯的一个很严重的错误,一开始写成i=1了,重复了,行号是按照递增的顺序来选择的,1 5 8和5 1 8是同一种情况  
  42.     {  
  43.         if(yes(i))  
  44.         {  
  45.             for(int j=1;j<=m;j++)  
  46.             {  
  47.                 if(mp[i][j])  
  48.                     vis[j]=1;  
  49.             }  
  50.             DFS(cnt+hash[i],i+1);  
  51.             if(ok)  
  52.                 break;  
  53.             for(int j=1;j<=m;j++)  
  54.             {  
  55.                 if(mp[i][j])  
  56.                     vis[j]=0;  
  57.             }  
  58.         }  
  59.     }  
  60. }  
  61.   
  62. int main()  
  63. {  
  64.     while(scanf("%d%d",&n,&m)!=EOF)  
  65.     {  
  66.         memset(hash,0,sizeof(hash));  
  67.         for(int i=1;i<=n;i++)  
  68.             for(int j=1;j<=m;j++)  
  69.         {  
  70.             scanf("%d",&mp[i][j]);  
  71.             if(mp[i][j])  
  72.                 hash[i]++;  
  73.         }  
  74.         ok=0;  
  75.         memset(vis,0,sizeof(vis));  
  76.         DFS(0,1);  
  77.         if(ok)  
  78.             printf("Yes, I found it\n");  
  79.         else  
  80.             printf("It is impossible\n");  
  81.   
  82.     }  
  83.     return 0;  
  84. }  
0 0
原创粉丝点击