hihocoder-#1094 : Lost in the City

#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
解答
首先初始化先将数据读入:
 string[] mn = (Console.ReadLine()).Split(' ');
            int n = Convert.ToInt32(mn[0]);
            int m = Convert.ToInt32(mn[1]);
            string[,] map = new string[n,m];
            string[,] pos = new string[3, 3];
            //init
            for (int i = 0; i < n; i++)
            {
                string temp = Console.ReadLine();
                for (int j = 0; j < m; j++)
                {
                    map[i,j] = temp[j].ToString();
                }
            }
            for (int i = 0; i < 3; i++)
            {
                string temp = Console.ReadLine();
                for (int j = 0; j < 3; j++)
                {
                    pos[i,j] = temp[j].ToString();
                }
            }
接下来的方法用了特别的字符串比较技巧
首先要明确我们的目标其实是比较pos(3*3)矩阵跟我们遍历的每一个地图上的3*3矩阵是不是旋转关系。如果把“地图”旋转之后能与所在位置pos吻合就ok
核心部分:
string source = pos[0, 0] + pos[0, 1] + pos[0, 2] + pos[1, 2] + pos[2, 2] + pos[2, 1] + pos[2, 0] + pos[1, 0];
            for (int i = 1; i < n - 1; i++)
            {
                for (int j = 1; j < m - 1; j++)
                {
                    if (map[i,j] == pos[1,1])
                    {
                        string part = map[i - 1, j - 1] + map[i - 1, j] + map[i - 1, j + 1] + map[i, j + 1] + map[i + 1, j + 1] + map[i + 1, j] + map[i + 1, j - 1] + map[i, j - 1];
                        part += part;
                        if (part.Contains(source))
                            Console.WriteLine(string.Format("{0} {1}", i + 1, j + 1));
                    }
                }
            }

————————
这是昨天想到的办法,居然通过了test。但是今天细想一下发现用的技巧有bug,所以是怎么回事呢?hihocoder平台错了?

你可能感兴趣的:(算法)