限时10s,所以不用考虑什么算法了,暴力吧
分别按照3x3视野的四个方向去地图上匹配,把符合的地点标记出来,最后统一按照从上到下,从左到右的顺序输出。
代码:
1 #include <iostream> 2 3 using namespace std; 4 5 #define MAP_SIZE 250 6 7 bool match(char map[MAP_SIZE][MAP_SIZE], char sight[3][3], int r, int c) { 8 for (int i = 0; i < 3; i++) 9 for (int j = 0; j < 3; j++) 10 if (map[r + i][c + j] != sight[i][j]) 11 return false; 12 return true; 13 } 14 15 void find(char map[MAP_SIZE][MAP_SIZE], char sight[3][3], char res[MAP_SIZE][MAP_SIZE], int N, int M) { 16 for (int i = 0; i < N - 2; i++) { 17 for (int j = 0; j < M - 2; j++) { 18 if (match(map, sight, i, j)) 19 res[i + 1][j + 1] = 1; 20 } 21 } 22 } 23 24 void rotate(char sight[3][3]) { 25 for (int i = 0; i < 2; i++) 26 for (int j = 0; j < 2; j++) 27 swap(sight[i][j], sight[2 - j][2 - i]); 28 for (int i = 0; i < 3; i++) 29 swap(sight[0][i], sight[2][i]); 30 } 31 32 int main() { 33 int N, M; 34 char map[MAP_SIZE][MAP_SIZE]; 35 char res[MAP_SIZE][MAP_SIZE]; 36 char sight[3][3]; 37 38 cin >> N >> M; 39 for (int i = 0; i < N; i++) 40 for (int j = 0; j < M; j++) 41 cin >> map[i][j]; 42 43 for (int i = 0; i < 3; i++) 44 for (int j = 0; j < 3; j++) 45 cin >> sight[i][j]; 46 47 memset(res, 0, MAP_SIZE * MAP_SIZE * sizeof(char)); 48 49 for (int i = 0; i < 4; i++) { 50 find(map, sight, res, N, M); 51 rotate(sight); 52 } 53 54 for (int i = 1; i < N - 1; i++) 55 for (int j = 1; j < M - 1; j++) 56 if (res[i][j]) 57 cout << i + 1 << " " << j + 1 << endl; 58 59 return 0; 60 }