Little Hi gets lost in the city. He does not know where he is. He does not know which direction is north.
Fortunately, Little Hi has a map of the city. The map can be considered as a grid of N*M blocks. Each block is numbered by a pair of integers. The block at the north-west corner is (1, 1) and the one at the south-east corner is (N, M). Each block is represented by a character, describing the construction on that block: '.' for empty area, 'P' for parks, 'H' for houses, 'S' for streets, 'M' for malls, 'G' for government buildings, 'T' for trees and etc.
Given the blocks of 3*3 area that surrounding Little Hi(Little Hi is at the middle block of the 3*3 area), please find out the position of him. Note that Little Hi is disoriented, the upper side of the surrounding area may be actually north side, south side, east side or west side.
Line 1: two integers, N and M(3 <= N, M <= 200).
Line 2~N+1: each line contains M characters, describing the city's map. The characters can only be 'A'-'Z' or '.'.
Line N+2~N+4: each line 3 characters, describing the area surrounding Little Hi.
Line 1~K: each line contains 2 integers X and Y, indicating that block (X, Y) may be Little Hi's position. If there are multiple possible blocks, output them from north to south, west to east.
8 8 ...HSH.. ...HSM.. ...HST.. ...HSPP. PPGHSPPT PPSSSSSS ..MMSHHH ..MMSH.. SSS SHG SH.样例输出
5 4
解题思路,很明显看出3*3的矩阵可以旋转0°,90°,180°,270°,共四种类型(此处本人直接列出四种情况下的比较操作)。写一个判断函数,然后对大矩阵内的元素依次判断。 PS:在网上查到一种比较四种类型的情况的方法(本人尚未能理解):
可以把子图存储成一维数组。然后按照四中形态的遍历顺序去大地图中进行遍历。
#include
char a[201][201];
int IsIt(int x,int y,char b[][3]){
//判断(x,y)在b这个3*3矩阵中心
if(a[x][y]!=b[1][1])
return 0;
if( (a[x-1][y-1]==b[0][0]&&a[x-1][y]==b[0][1]&&a[x-1][y+1]==b[0][2]
&&a[x][y-1]==b[1][0]&&a[x][y+1]==b[1][2]
&&a[x+1][y-1]==b[2][0]&&a[x+1][y]==b[2][1]&&a[x+1][y+1]==b[2][2]) ||
(a[x-1][y-1]==b[2][2]&&a[x-1][y]==b[2][1]&&a[x-1][y+1]==b[2][0]
&&a[x][y-1]==b[1][2]&&a[x][y+1]==b[1][0]
&&a[x+1][y-1]==b[0][2]&&a[x+1][y]==b[0][1]&&a[x+1][y+1]==b[0][0]) ||
(a[x-1][y-1]==b[0][2]&&a[x-1][y]==b[1][2]&&a[x-1][y+1]==b[2][2]
&&a[x][y-1]==b[0][1]&&a[x][y+1]==b[2][1]
&&a[x+1][y-1]==b[0][0]&&a[x+1][y]==b[1][0]&&a[x+1][y+1]==b[2][0]) ||
(a[x-1][y-1]==b[2][0]&&a[x-1][y]==b[1][0]&&a[x-1][y+1]==b[0][0]
&&a[x][y-1]==b[2][1]&&a[x][y+1]==b[0][1]
&&a[x+1][y-1]==b[2][2]&&a[x+1][y]==b[1][2]&&a[x+1][y+1]==b[0][2]) )
return 1;
return 0;
}
int main(){
int N,M;
int i,j;
char b[3][3];
scanf("%d %d",&N,&M);
for(i=0;i