hihocoder #1094 : Lost in the City微软苏州校招笔试 12月27日 (建图不大【暴力枚举】 子图的4种形态 1Y )

#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

算法分析:在大地图中找子地图,注意:子地图的方向不确定,也就是说,子地图可以旋转4次90度,产生4中形态。
只要大地图中,有其中任意一种形态,那个位置就是合法的。找出所有这样的状态。
一开始我想把子地图存储成二维数组,后来发现要把它变换成共4中形态,变换的语句描述很是麻烦,修饰语法就得写一大堆。
为了一下xu建哥有什么好的策略:可以把子图存储成一维数组。然后按照四中形态的遍历顺序去大地图中进行遍历。看看在
当前的i,j,能不能遍历出这样的序列,如果能就说明这个位置合法,直接输出。否则继续枚举寻找。
代码:
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <ctype.h>

using namespace std;

char ch[202][202];
char c[11];

bool test_ok(int dd, int ff)
{
    int i, j;
    int k=0;
    int ok=1;
    for(i=dd; i<=dd+2; i++)
    {
        for(j=ff; j<=ff+2; j++)
        {
            if(ch[i][j]==c[k])
            {
                k++;
            }
            else
            {
                ok=0; break;
            }
        }
        if(ok==0)
            break;
    }
    if(ok==1)
    {
        return true;
    }
    ok=1; k=0;
    for(j=ff; j<=ff+2; j++)
    {
        for(i=dd+2; i>=dd; i--)
        {
            if(ch[i][j]==c[k])
                k++;
            else
            {
                ok=0; break;
            }
        }
        if(ok==0)
            break;
    }
    if(ok==1)
        return true;

    ok=1; k=0;
    for(i=dd+2; i>=dd; i--)
    {
        for(j=ff+2; j>=ff; j--)
        {
            if(ch[i][j]==c[k])
                k++;
            else
            {
                ok=0; break;
            }
        }
        if(ok==0)
            break;
    }
    if(ok==1)
        return true;

    ok=1; k=0;
    for(j=ff+2; j>=ff; j--)
    {
        for(i=dd; i<=dd+2; i++)
        {
            if(ch[i][j]==c[k])
                k++;
            else
            {
                ok=0; break;
            }
        }
        if(ok==0)
            break;
    }
    if(ok==1)
        return true;

    return false;
}

int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    int i, j;
    char cc;
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            cc=getchar();
            while(!isalpha(cc)&&cc!='.' ) //不是字母且不是.
            {
                cc=getchar();
            }
            ch[i][j]=cc;
        }
    } //建立地图
    int e=0;
    for(i=0; i<3; i++)
        for(j=0; j<3; j++)
        {
            cc=getchar();
            while(!isalpha(cc)&&cc!='.')
                cc=getchar();
            c[e++]=cc;
        }
    c[e]='';
    //printf("%s", c);
    //建立小地图
    for(i=0; i<=n-3; i++)
    {
        for(j=0; j<=m-3; j++)
        {
            if(test_ok(i, j))
            {
                printf("%d %d
", i+1+1, j+1+1);
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yspworld/p/4261635.html