lost in the city(枚举)

#1094 : Lost in the City

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

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:在网上查到一种比较四种类型的情况的方法(本人尚未能理解):    

可以把子图存储成一维数组。然后按照四中形态的遍历顺序去大地图中进行遍历。
                                                                                                              
编程中遇到两个点, 第一:
二维数组在逻辑上是二维的,但在内存中却是一维的。所以,当将二维数组作为参数传递时,不能使用类似于a[ ]这种形式,而应该 至少指明列数,这样系统才能判断出从首地址开始,有多少属于数组的内存。如下:
IsIt(int x,int y,char b[3][3])
(或者)
IsIt(int x,int y,char b[ ][3])
   第二:
对于二维数组如a[ ][ ],a[ ]的本质也是一个地址。所以可以按照如下方式使用:
scanf("%s",a[ ]);  //此处a是一个二维数组

下面是代码部分:
#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

你可能感兴趣的:(算法题,hihocoder,hihocoder,算法)