题意: 将题目意思抽象出来就是,给一个二维的大的字符数组,再给一个 3×3 的二维字符数组,拿 3×3 的字符数组去大数组中匹配,匹配过程中可以将 3×3 的字符数组旋转 90 度、180 度、270 度。求匹配到的点。
思路: 这题可以用枚举,我们可以这样分析一下计算量:顶多枚举
(200−2)×(200−2)
个位置,每个位置可能有 4 种可能,所以最多枚举大约 160000 个,不会超时。
我的想法是将
3×3
的二维数组变成一维数组,这样就比较好比较了。又由于有四种可能,所以针对给出的一个
3×3
数组我们能产生出四个一维数组。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<utility>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
#include<sstream>
using namespace std;
typedef long long LL;
int N, M;
char G[210][210];
char hi[5][5];
char hi1[10], hi2[10], hi3[10], hi4[10];
bool Judge(int x, int y)
{
char tmp[10];
tmp[0] = G[x-1][y-1]; tmp[1] = G[x-1][y]; tmp[2] = G[x-1][y+1]; tmp[3] = G[x][y+1];
tmp[4] = G[x+1][y+1]; tmp[5] = G[x+1][y]; tmp[6] = G[x+1][y-1]; tmp[7] = G[x][y-1];
tmp[8] = '\0';
//printf("i= %d, j= %d, tmp: %s\n", x, y, tmp);
bool ok = true;
for(int i=0; i<8; i++){
if(tmp[i] != hi1[i]){
ok = false;
break;
}
}
if(ok) return true;
ok = true;
for(int i=0; i<8; i++){
if(tmp[i] != hi2[i]){
ok = false;
break;
}
}
if(ok) return true;
ok = true;
for(int i=0; i<8; i++){
if(tmp[i] != hi3[i]){
ok = false;
break;
}
}
if(ok) return true;
ok = true;
for(int i=0; i<8; i++){
if(tmp[i] != hi4[i]){
ok = false;
break;
}
}
if(ok) return true;
return false;
}
int main()
{
//freopen("in.txt", "r", stdin);
while(scanf("%d%d", &N, &M) == 2){
for(int i=0; i<N; i++){
scanf("%s", G[i]);
}
for(int i=0; i<3; i++){
scanf("%s", hi[i]);
}
hi1[0] = hi[0][0]; hi1[1] = hi[0][1]; hi1[2] = hi[0][2]; hi1[3] = hi[1][2];
hi1[4] = hi[2][2]; hi1[5] = hi[2][1]; hi1[6] = hi[2][0]; hi1[7] = hi[1][0];
hi1[8] = '\0';
//printf("hi1: %s\n", hi1);
hi2[0] = hi[2][0]; hi2[1] = hi[1][0]; hi2[2] = hi[0][0]; hi2[3] = hi[0][1];
hi2[4] = hi[0][2]; hi2[5] = hi[1][2]; hi2[6] = hi[2][2]; hi2[7] = hi[2][1];
hi2[8] = '\0';
//printf("hi2: %s\n", hi2);
hi3[0] = hi[2][2]; hi3[1] = hi[2][1]; hi3[2] = hi[2][0]; hi3[3] = hi[1][0];
hi3[4] = hi[0][0]; hi3[5] = hi[0][1]; hi3[6] = hi[0][2]; hi3[7] = hi[1][2];
hi3[8] = '\0';
//printf("hi3: %s\n", hi3);
hi4[0] = hi[0][2]; hi4[1] = hi[1][2]; hi4[2] = hi[2][2]; hi4[3] = hi[2][1];
hi4[4] = hi[2][0]; hi4[5] = hi[1][0]; hi4[6] = hi[0][0]; hi4[7] = hi[0][1];
hi4[8] = '\0';
//printf("hi4: %s\n", hi4);
for(int i=1; i<N-1; i++){
for(int j=1; j<M-1; j++){
if(G[i][j] == hi[1][1]){
if(Judge(i, j)) printf("%d %d\n", i+1, j+1);
}
}
}
}
return 0;
}