问题:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2130
这是一道比较简单的模拟类问题,使用二维数组作为数据结构比较好,由于只有O和X两种,所以另其分别对应于0和1。以左上角的点为基点,遍历图像,对比斑点的每个像素和图像对应坐标的值是否一致即可。需要注意对应关系,以及坐标界的问题。
AC代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <cstdio>
using namespace std;
int H, W, h, w, figure[100][100], spot[100][100];
bool matching(int x, int y){
for (int i = 0; i < h; i++){
for (int j = 0; j < w; j++){
if (spot[i][j]>=0 && spot[i][j] != figure[i+x][j+y])
return false;
else continue;
}
}
return true;
}
int main()
{
int i,j,count;
char tmp;
// FILE *fp;
// fp= fopen("D:\\c++\\test.txt", "r");
while (scanf("%d%d%d%d",&H,&W,&h,&w)!=EOF)
{
count = 0;
for (i = 0; i < H; i++){
scanf("%c", &tmp);
for (j = 0; j < W; j++){
scanf("%c", &tmp);
if (tmp == 'O')
figure[i][j] = 0;
else figure[i][j] = 1;
}
}
for (i = 0; i < h; i++){
scanf("%c", &tmp);
for (j = 0; j < w; j++){
scanf("%c", &tmp);
if (tmp == 'O')
spot[i][j] = 0;
else if (tmp == 'X')
spot[i][j] = 1;
else spot[i][j] = -1;
}
}
for (i = 0; i < H - h + 1; i++){
for (j = 0; j < W - w + 1; j++){
if (matching(i,j) == true)
count++;
}
}
scanf("%c", &tmp);
printf("%d\n", count);
}
// fclose(fp);
return 0;
}