刚开始学习C语言,准备在做hiho的题目的过程中来学习,在此进行记录,如果代码中有错误或者不当的地方还请指正。
描述
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
解决思路
从(2,2)开始到(n-1,m-1)的方块中的每一个元素进行分析,看其是否满足。分析元素过程:首先顺时针
分析离得最近的四个如果满足,再顺时针分析四个角上的是否满足(注意一旦四个最近的顺序确定了
那么四个角上的顺序也就定下来了)。最后输出所有满足条件的结果。
#include<stdio.h> int JudgeThePos(char **Map,int i,int j); char HPos[][];
char HPosDir[],HPosOblDir[],HPosPos;
int n,m; int main()
{
char **Map,EKey;
int i,j;
scanf("%d%d",&n,&m);
//初始化地图
Map=(char **)malloc(n*sizeof(char*));
for(i=;i<n;i++)
Map[i]=(char *)malloc(m*sizeof(char*));
scanf("%c",&EKey);
for(i=;i<n;i++)
{
for(j=;j<m;j++)
scanf("%c",&Map[i][j]);
scanf("%c",&EKey);
}
//读取位置
for(i=;i<;i++)
{
for(j=;j<;j++)
scanf("%c",&HPos[i][j]);
scanf("%c",&EKey);
}
//初始化位置
HPosDir[]=HPos[][]; HPosDir[]=HPos[][];
HPosDir[]=HPos[][]; HPosDir[]=HPos[][];
HPosOblDir[]=HPos[][]; HPosOblDir[]=HPos[][];
HPosOblDir[]=HPos[][]; HPosOblDir[]=HPos[][];
HPosPos=HPos[][];
//对(2,2)到(n-1,m-1)的方块内的每一个位置进行判断
for(i=;i<n-;i++)
{
for(j=;j<m-;j++)
{
if(JudgeThePos(Map,i,j)==)
printf("%d %d\n",i+,j+);
}
}
free(Map);
return ;
} int JudgeThePos(char **Map,int i,int j)
{
int k,l,p,q;
char PosDir[],PosOblDir[],PosPos;
PosDir[]=Map[i-][j];
PosDir[]=Map[i][j+];
PosDir[]=Map[i+][j];
PosDir[]=Map[i][j-];
PosOblDir[]=Map[i-][j+];
PosOblDir[]=Map[i+][j+];
PosOblDir[]=Map[i+][j-];
PosOblDir[]=Map[i-][j-];
PosPos=Map[i][j];
//判断位置
if(PosPos!=HPosPos)
return ;
//判断正方向
for(k=;k<;k++)
{
p=;q=;
for(l=;l<;l++)
{
if((k+p)==)
p=-k;
if(HPosDir[l]==PosDir[k+p])
{
p++;
q++;
}
else
break;
}
if(q==)
break;
}
//判断斜方向
if(q==)
{
p=;q=;
for(l=;l<;l++)
{
if((k+p)==)
p=-k;
if(HPosOblDir[l]==PosOblDir[k+p])
{
p++;
q++;
}
else
break;
}
if(q==)
return ;
else
return ;
}
else
return ;
}