您的位置:首页 > 其它

POJ 3600 Subimage Recognition 搜索

2016-10-07 20:30 351 查看
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 ≤ r, c ≤ 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

;
char B

;
bool vis
; ///标记合法方案的列
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  搜索