题目三:
有一个M*N的材料和一个s*t的模板,从材料中切除模板,求最大能切出来的模板的数量。
sample input:
3 4
a b c d
c d a b
a c c d
2 2
a b
c d
sample out
2
本文只是为了博主本人考研复习,不保证写的代码和思路一定正确,如有错误之处,还望指出~
思路:
1.使用DFS来遍历整个大图(材料)
2.在DFS中,若在大图中以位置(x, y)作为左上角可以匹配小图(模板),则有两种选择:去切除这块模板或者不切这块模板。
若(x, y)不能匹配小图,则继续利用DFS遍历其他未切除位置。
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1000;
int m, n, s, t; //大图的行、列, 小图的行、列
int maxCnt = 0; //记录最大模板数
char big[maxn][maxn]; //大图,材料
char small[maxn][maxn]; //小图,模板
bool vis[maxn][maxn]; //是否已切除, vis[x][y] = false表示(x, y)未切除
//检查以(x,y)为左上角 是否能匹配模板
bool check(int x, int y){
if(x + s > m || y + t > n