您的位置:首页 > 其它

#1094 : Lost in the City

2015-01-30 16:19 393 查看
时间限制: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

就是在一个二维数组里面找到另一个小一点的二维数组相匹配,用枚举法即可。

值得注意的是小的那个数组不知道方向,那么只需要把四种方向的情况列出来分别寻找即可,找到了把坐标存进结果结构体,最后再排序输出(先比较横坐标 如相同再比较纵坐标)以满足从北到南,从西到东的要求。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 500
char arr[MAX][MAX];
char str[3][MAX],strr[3][MAX];
typedef struct {
        int x;
        int y;
}result;
result res[MAX*MAX];
int num;
void search(int M,int N)
{
     int i,j;
     int k,m,flag;
     if(M>=3&&N>=3)
     {
         for(i=0;i<M-2;i++)
         {
            for(j=0;j<N-2;j++)
            {
              flag=0;
              for(k=0;k<3;k++)
              {
                for(m=0;m<3;m++)
                {
                  if(arr[i+m][j+k]!=strr[m][k])
                  {    flag=1;
                       break;
                  }
                }
                if(flag==1)
                    break;
              }
              if(flag==0)
              {
                ((res[num]).x)=i+2;
                ((res[num]).y)=j+2;
                num++;
              }
            }
         }
     }
}
int cmp(const void *a,const void *b)
{
     result *f1=(result *)a;
     result *f2=(result *)b;
    if(f1->x!=f2->x)
        return f1->x-f2->x;
    else
    {
        return f1->y-f2->y;
    }
}
int main(void)
{
     int M,N,i,j,k,m;
     int temp1,temp2;
     while(scanf("%d%d",&M,&N)!=EOF)
     {
         memset(res,0,sizeof(res));
         num=0;
         temp1=0,temp2=0;
         for(i=0;i<M;i++)
         {
         scanf("%s",&arr[i][0]);
         }
          for(i=0;i<3;i++)
         { 
         scanf("%s",&str[i][0]);
         }
         for(i=2,k=0; i>=0; i--,k++)    //right to left down to up
         {
             for (j=2,m=0; j>=0; j--,m++)
             {
                    strr[k][m]=str[k][m];
             }
         }
         search(M,N);            //left to right up to down
         for(i=2,k=0; i>=0; i--,k++)    //right to left down to up
         {
             for (j=2,m=0; j>=0; j--,m++)
             {
                 strr[k][m]=str[i][j];
             }
         }
         search(M,N);
         for(i=2,k=0; i>=0; i--,k++)    //UP TO DOWM AND LEFT TO RIGHT
         {
             for (j=2,m=0; j>=0; j--,m++)
             {
                strr[k][m]=str[j][k];
             }
         }
         search(M,N);
         for(i=2,k=0; i>=0; i--,k++)    //UP TO DOWM AND LEFT TO RIGHT
         {
             for (j=2,m=0; j>=0; j--,m++)
             {
                strr[k][m]=str[m][i];
             }
         }
         search(M,N);
         qsort(res,num,sizeof(res[0]),cmp);
         for(i=0;i<num;i++)
         {
          if(temp1!=res[i].x||temp2!=res[i].y)
           {
           printf("%d %d\n",res[i].x,res[i].y);
           temp1=res[i].x;temp2=res[i].y;
           }

         }
     }
     return 0;
}


NormalVim
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hiho coder 枚举