hihoCoder#1094
剛開始學習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[3][3]; char HPosDir[4],HPosOblDir[4],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=0;i<n;i++) Map[i]=(char *)malloc(m*sizeof(char*)); scanf("%c",&EKey); for(i=0;i<n;i++) { for(j=0;j<m;j++) scanf("%c",&Map[i][j]); scanf("%c",&EKey); } //讀取位置 for(i=0;i<3;i++) { for(j=0;j<3;j++) scanf("%c",&HPos[i][j]); scanf("%c",&EKey); } //初始化位置 HPosDir[0]=HPos[0][1]; HPosDir[1]=HPos[1][2]; HPosDir[2]=HPos[2][1]; HPosDir[3]=HPos[1][0]; HPosOblDir[0]=HPos[0][2]; HPosOblDir[1]=HPos[2][2]; HPosOblDir[2]=HPos[2][0]; HPosOblDir[3]=HPos[0][0]; HPosPos=HPos[1][1]; //對(2,2)到(n-1,m-1)的方塊內的每一個位置進行判斷 for(i=1;i<n-1;i++) { for(j=1;j<m-1;j++) { if(JudgeThePos(Map,i,j)==1) printf("%d %d\n",i+1,j+1); } } free(Map); return 0; } int JudgeThePos(char **Map,int i,int j) { int k,l,p,q; char PosDir[4],PosOblDir[4],PosPos; PosDir[0]=Map[i-1][j]; PosDir[1]=Map[i][j+1]; PosDir[2]=Map[i+1][j]; PosDir[3]=Map[i][j-1]; PosOblDir[0]=Map[i-1][j+1]; PosOblDir[1]=Map[i+1][j+1]; PosOblDir[2]=Map[i+1][j-1]; PosOblDir[3]=Map[i-1][j-1]; PosPos=Map[i][j]; //判斷位置 if(PosPos!=HPosPos) return 0; //判斷正方向 for(k=0;k<4;k++) { p=0;q=0; for(l=0;l<4;l++) { if((k+p)==4) p=-k; if(HPosDir[l]==PosDir[k+p]) { p++; q++; } else break; } if(q==4) break; } //判斷斜方向 if(q==4) { p=0;q=0; for(l=0;l<4;l++) { if((k+p)==4) p=-k; if(HPosOblDir[l]==PosOblDir[k+p]) { p++; q++; } else break; } if(q==4) return 1; else return 0; } else return 0; }
浙公網安備 33010602011771號