POJ 3600 Subimage Recognition【递归DFS + 模拟枚举】

POJ 3600 Subimage Recognition【递归DFS + 模拟枚举】http://poj.org/problem?id=3600

Subimage Recognition
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 2486 Accepted: 932

Description

An image A is said to be a subimage of another image B if it is possible to remove some rows and/or columns of pixels from B so that the resulting image is identical toA. Figure 6 illustrates an example. Image A, shown in Figure 6(a), is a subimage of image B, shown in Figure 6(b), because the image resulting from the removal of the middle row and column of pixels from B is identical to A.

 
  
 
  
   
   
(a) Image A (b) Image B

Figure 6: An example of a subimage

Given two black-and-white images A and B, determine whether A is a subimage of B.

Input

The input contains a single test case. The first line contains two integers r and c (1 ≤ rc ≤ 20), the dimensions of A. The following r lines, each containing a string of length c, give an r × c 0-1 matrix representing the pixels of A. The next line contains two integers R and C (r ≤ R ≤ 20; c ≤ C ≤ 20), the dimensions of B. The followingR lines, each containing a string of length C, give an R × C 0-1 matrix representing the pixels of B. A 0 indicates a white pixel; a 1 indicates a black pixel.

Output

If A is a subimage of B, print “Yes”; otherwise, print “No”.

Sample Input

2 2
11
10
3 3
101
000
100

Sample Output

Yes

Source

[Submit]   [Go Back]   [Status]   [Discuss]


【题意】给出一个小矩阵,有黑白两个状态1和0,还有一个大矩阵,是否能在大矩阵中删除若干整行 或 整列而得到小矩阵,Yes或No!

【分析】开始的思路就是 行递归选择行,再列递归选择列 那就是2^400中情况  肯定会超时!

巧妙的思路就是  枚举小矩形的第一行在大矩形中的位置(比如在大矩形的第k行) 然后递归在大矩形中

选择N(小矩形的列数)然后从K行开始 能否找到小矩形的对应行。。。这样就一次递归2^20次方是可以接受的

【代码如下】

//poj-3600	Accepted	232K	219MS	C++	1312B
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

int mmin[22][22], mmax[22][22];
int col[22];
int r1, c1, r2, c2;
int k, cnt;
bool flag;

bool check()
{
    int r=1, i, j;
    for(i=k; i<=r2; i++)
    {
        for(j=1; j<=c1; j++)
            if(mmin[r][j] != mmax[i][col[j]])
                break;
        if(j > c1){
            r++;
            if(r > r1) break;
        }
    }
    if(r == (r1+1)) return 1;
    return 0;
}

void dfs(int x)
{
    if(cnt > c1){
        flag = check();
        return;
    }
    if(x > c2) return;
    col[cnt++] = x;
    dfs(x+1);
    if(flag) return;

    cnt--;
    dfs(x+1);
}

int main()
{
    char ch;
    while(cin>>r1>>c1)
    {
        for(int i=1; i<=r1; i++)
            for(int j=1; j<=c1; j++)
                cin>>ch, mmin[i][j]=ch-'0';
        cin>>r2>>c2;
        for(int i=1; i<=r2; i++)
            for(int j=1; j<=c2; j++)
                cin>>ch, mmax[i][j]=ch-'0';

        flag=0;
        for(k=1; k<=r2-r1+1; k++)
        {
            if(flag) break;
            cnt=1;
            dfs(1);
        }
        if(flag)    printf("Yes\n");
        else    printf("No\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值