您的位置:首页 > 其它

hiho #1094 : Lost in the City

2016-12-29 12:11 483 查看
题目描述

时间限制: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,所以我简单地手工处理了,但关于矩阵转置算法是个很有趣的问题,以后会慢慢补上来!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hiho