POJ 3600 Subimage Recognition 搜索
来源:互联网 发布:淘宝上办签证被骗了 编辑:程序博客网 时间:2024/05/29 14:00
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.
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 211103 3101000100
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;}
- POJ 3600 Subimage Recognition 搜索
- POJ 3600 Subimage Recognition dfs
- [POJ 3600] Subimage Recognition [枚举+贪心]
- POJ 3600 Subimage Recognition(dfs)
- POJ 3600 Subimage Recognition【递归DFS + 模拟枚举】
- Subimage Recognition
- Subimage Recognition
- 2016弱校联盟十一专场10.7(12点场)-M. Subimage Recognition
- POJ搜索题目总结
- POJ 2976 参数搜索
- 简单搜索1979@POJ
- 广度搜索-POJ 1753
- POJ搜索题目
- POJ - 1011 搜索剪枝
- POJ 搜索题
- poj搜索汇总
- poj 1411(搜索+剪枝)
- poj 2488( 搜索 )
- html 03 浮动布局以及清除浮动
- 【译】4个你需要知道的Asset Catalog的秘密
- 05node.js事件驱动程序
- How Many Tables(HDU1213)(入门)
- ContentProvider中常用的常量及字段
- POJ 3600 Subimage Recognition 搜索
- Hadoop安装(一)
- PHP 远程文件包含的介绍和代码防御
- angular自定义的tab切换
- 多线程(一)
- JAVA设计模式-责任链模式
- Python学习 自定义函数
- 关于原生dom操作
- QT---项目的构成及原理