hihoCoder-Lost in the City
描述
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
这个题归类为枚举,因为题目的问题规模比较小(N,M最大是200),所以暴力求解就可以。maps二维数组存储city的数据,location二维数组存储Little Hi周围的数据。(这个题目中有一个比较重要的地方就是, 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.)
思路如下:1.在maps二维数组中找到location的中心的元素,也就是Little Hi的位置(x,y);
2.暴力求解时没有用网上一些同学用的旋转矩阵的方法来做,而是将location中除了中心元素外的其他8个元素按照顺时针拼接成一个字符串s1,因为在maps矩阵中(x,y)周围的元素顺序不确定,所以s1 += s1,将字符串首尾相连,组成一个备用比较的长字符串;将maps中与location中心元素相等的元素的周围8个字符也拼串为s2。因为已经比较过了中心位置的元素,其实要找到准确的Little Hi的位置就是比较s2是否在s1中出现过(利用在s1中查找子串的方法),然后取此时的(x,y)就可以了。
代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int N = input.nextInt();
int M = input.nextInt();
char[][] maps = new char[N][M];
for (int i = 0; i < N; i++){
String string = input.next().trim();
maps[i] = string.toCharArray();
}
String s1 = new String();
char[][] location = new char[3][3];
for (int i = 0; i < 3; i++){
String string = input.next().trim();
location[i] = string.toCharArray();
}
String temp1 = "" + location[0][0] + location[0][1] + location[0][2];
String temp2 = "" + location[1][2] + location[2][2] + location[2][1];
String temp3 = "" + location[2][0] + location[1][0];
s1 = temp1 + temp2 + temp3;
s1 += s1;
//System.out.println(s1);
char center = location[1][1];
for (int i = 1; i < N - 1; i++){
for (int j = 1; j < M - 1; j++){
String s2 = new String();
if (maps[i][j] == center){
temp1 = "" + maps[i - 1][j - 1] + maps[i - 1][j] + maps[i - 1][j + 1];
temp2 = "" + maps[i][j + 1] + maps[i + 1][j + 1] + maps[i + 1][j];
temp3 = "" + maps[i + 1][j - 1] + maps[i][j - 1];
s2 = temp1 + temp2 + temp3;
//System.out.println(s2);
for (int k = 0; k < s1.length() / 2; k++){
String r1 = s1.substring(k, k + 8);
if (r1.equals(s2)){
System.out.println((i + 1) + " " + (j + 1));
break;
}
}
}
}
}
}
}