POJ 3600 Subimage Recognition 搜索
2016-10-07 20:30
351 查看
Subimage Recognition
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
Sample Output
题意:给一个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;
}
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 3072 | Accepted: 1176 |
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 |
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;
}
相关文章推荐
- POJ 3600 Subimage Recognition(dfs)
- [POJ 3600] Subimage Recognition [枚举+贪心]
- POJ 3600 Subimage Recognition【递归DFS + 模拟枚举】
- poj 3600 Subimage Recognition (枚举+dfs)
- POJ 3600 Subimage Recognition dfs
- POJ-2011-Primary X-Subfactor Series【位运算】【记忆化搜索】【好题】
- Subimage Recognition
- Subimage Recognition
- 2016弱校联盟十一专场10.7(12点场)-M. Subimage Recognition
- [POJ3600]Subimage Recognition(状态压缩,枚举,暴力)
- poj--1579--(DFS+记忆化搜索之经典)
- poj 搜索题目列表
- poj 3258 River Hopscotch(二分搜索)
- vggnet笔记(Very Deep Convolutional Networks for Large-Scale Image Recognition)
- POJ 2251 Dungeon Master 搜索入门题
- poj 2197 hoj 2027 Jill's Tour Paths 搜索
- [kuangbin带你飞]专题一 简单搜索H - Pots(POJ 3414)
- poj 最长公共子序列 1458 记忆式搜索
- [kuangbin带你飞]专题1 简单搜索 K - 迷宫问题 POJ - 3984
- 2016 MSR Image Recognition Challenge (IRC) Results