您的位置:首页 > 其它

2016弱校联盟十一专场10.7(12点场)-M. Subimage Recognition

2016-10-07 16:11 330 查看

M. Subimage Recognition

Time Limit: 1000msCase Time Limit: 1000msMemory Limit: 131072KB64-bit integer IO format: %lld      Java class name: MainSubmit StatusFont Size: + -An image A is said to be a subimage of another image B if it is possible to removesome rows and/or columns of pixels fromB so that the resulting image is identical to A. Figure 6 illustratesan example. Image A, shown in Figure 6(a), is a subimage of image B, shown in Figure 6(b), because the imageresulting from the removal of the middle row and column of pixels fromB is identical to A.
(a) Image A(b) Image B
Figure 6: An example of a subimageGiven two black-and-white images A and B, determine whether A isa 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-1matrix 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-1matrix 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
题目大意:给出两个图A和B,如果在B中删去某些行某些列得到的图形与A相同的输出Yes
否则输出No
解题思路:k从第1行到第r2-r1+1行进行匹配,
从第一列开始深搜,当列数大于c1时return,
从第k行到第r2行匹配,如果成果返回1,否则返回0;
#include<iostream>using namespace std;int A[25][25],B[25][25];int col[25];int r1,c1,r2,c2;int k,cnt;bool flag;bool check(){int r=1;int i,j;for(i=k;i<=r2;i++){for(j=1;j<=c1;j++){if(A[r][j]!=B[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(){ios::sync_with_stdio(false);cin.tie(0);int i,j;char ch;cin>>r1>>c1;for(i=1;i<=r1;i++){for(j=1;j<=c1;j++){cin>>ch;A[i][j]=ch-'0';}}cin>>r2>>c2;for(i=1;i<=r2;i++){for(j=1;j<=c2;j++){cin>>ch;B[i][j]=ch-'0';}}flag=0;for(k=1;k<=r2-r1+1;k++){if(flag)  break;cnt=1;dfs(1);}//cout<<col[1]<<" "<<col[2]<<endl;if(flag)  cout<<"Yes"<<endl;else      cout<<"No"<<endl;return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: