hiho #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
解题思路:

稍微看下这个题目的数值范围,在200以内,遍历一次许4*10^4计算量,再加上每次遍历需要9*4=36次的检测,总共的时间复杂度约为10^6,在可接受范围内,因此很容易写出代码:

#include <iostream>
#include <vector>
using namespace std;

int n, m;
vector<vector<char> > matrix(201, vector<char>(201, ' '));
vector<vector<char> > position(3, vector<char>(3));

bool match(int row, int col)
{
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            if(position[i][j] != matrix[row+i][col+j])
                return false;
        }
    }
    return true;
}

void rotateMatrix()
{
    int tmp = position[0][0];
    position[0][0] = position[2][0];
    position[2][0] = position[2][2];
    position[2][2] = position[0][2];
    position[0][2] = tmp;
    
    tmp = position[0][1];
    position[0][1] = position[1][0];
    position[1][0] = position[2][1];
    position[2][1] = position[1][2];
    position[1][2] = tmp;
}

//void printPosition()
//{
//    for(int i = 0; i < 3; i++){
//        for(int j = 0; j < 3; j++){
//            cout << position[i][j] << ' ';
//        }
//        cout << endl;
//    }
//}

bool fitInWithPos(int row, int col)
{
    if(match(row, col))
        return true;
    rotateMatrix();
    if(match(row, col))
        return true;
    rotateMatrix();
    if(match(row, col))
        return true;
    rotateMatrix();
    if(match(row, col))
        return true;
    return false;
}

int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin >> matrix[i][j];
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            cin >> position[i][j];

    for(int i = 2; i < n; i++){
        for(int j = 2; j < m; j++){
            if(fitInWithPos(i-1, j-1))
                cout << i << ' ' << j << endl;
        }
    }
    return 0;
}

注意到这里面有个矩阵转置问题,由于只是3*3,所以我简单地手工处理了,但关于矩阵转置算法是个很有趣的问题,以后会慢慢补上来!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值