POJ 3600 Subimage Recognition 搜索

Subimage Recognition
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 3072 Accepted: 1176

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 to A. 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 following R 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

题意:给一个01矩阵A,给一个01矩阵B,问删除B的某些行和B的某些列能否使得矩阵A和矩阵B一模一样。

解法:删除一些行和列使得和A相等,也可以理解成在B中是否存在和A相同的子矩阵(元素不一定,行列的相对坐标对齐就行)。我的方法是在B的每一行中搜索与A第一行相同的所有方案,对可行方案的列进行标记。对于每一种方案,从开始标记的那一行开始往下逐行判断,能够成一个A矩阵就yes,不然就继续搜。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int N = 100;
int an,am,bn,bm;
char A[N][N];
char B[N][N];
bool vis[N];  ///标记合法方案的列
bool flag;    ///标记是否能构成A矩阵

void judge()  ///对于B标记的列,每行与A进行匹配
{
    int ax = 1,ay = 1;
    for(int bx = 1;bx <= bn;bx++){
        bool lead = true;
        for(int i = 1;i <= bm;i++){
            if(vis[i]){
                if(A[ax][ay++] != B[bx][i]){
                    lead = false;
                    break;
                }
            }
        }
        if(lead) ax++;  ///成功匹配一行,就匹配下一行
        ay = 1;
        if(ax > an) break;
    }
    if(ax > an) flag = true;
}

void dfs(int a,int b,int c)  ///A中第一行的第a个元素,B中c行的第b个元素
{
    if(flag) return;
    if(a == am+1){
        judge();
        return;
    }
    if(b > bm) return;
    for(int i = b;i <= bm;i++){
        if(A[1][a] == B[c][i]){
            vis[i] = true;
            dfs(a+1,i+1,c);
            vis[i] = false;
        }
    }
}

int main(void)
{
    scanf("%d%d",&an,&am);
    flag = false;
    memset(vis,false,sizeof vis);
    for(int i = 1;i <= an;i++)
        scanf("%s",A[i]+1);
    scanf("%d%d",&bn,&bm);
    for(int i = 1;i <= bn;i++)
        scanf("%s",B[i]+1);
    for(int i = 1;i <= bn;i++){
        for(int j = 1;j <= bm;j++){
            if(A[1][1] == B[i][j]){
                dfs(1,j,i);
            }
        }
    }
    if(flag) puts("Yes");
    else puts("No");
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值