hihocoder 1094 : Lost in the City

来源:互联网 发布:淘宝一元起拍在哪里 编辑:程序博客网 时间:2024/05/22 11:54
时间限制: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.PPGHSPPTPPSSSSSS..MMSHHH..MMSH..SSSSHGSH.
样例输出

5 4

import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class Main {public static void main(String[] args) {Main main=new Main();Scanner scan=new Scanner(System.in);int x=scan.nextInt();int y=scan.nextInt();char[][] map=new char[x][y];for(int i=0;i<x;i++){String str=scan.next();for(int j=0;j<y;j++){map[i][j]=str.charAt(j);}}char[][] arr=new char[3][3];for(int i=0;i<3;i++){String str=scan.next();for(int j=0;j<3;j++){arr[i][j]=str.charAt(j);}}main.solve(map,arr);}private void solve(char[][] map, char[][] arr) {List<char[][]> template=getTemplate(arr);for(int i=0;i<=map.length-3;i++){for(int j=0;j<=map[0].length-3;j++){for(char[][] temp:template){if(isSame(map,i,j,temp)){System.out.println((i+2)+" "+(j+2));break;}}}}}private boolean isSame(char[][] map, int i, int j, char[][] arr) {for(int k=0;k<arr.length;k++){for(int l=0;l<arr[0].length;l++){if(map[i+k][j+l]!=arr[k][l]) return false;}}return true;}public List<char[][]> getTemplate(char[][] arr){List<char[][]> re=new ArrayList<char[][]>();re.add(arr);for(int i=0;i<3;i++){arr=rotateRight(arr);re.add(arr);}return re;}public char[][] rotateRight(char[][] arr){char[][] arr2=new char[3][3];for(int i=0;i<3;i++){for(int j=0;j<3;j++){arr2[j][2-i]=arr[i][j];}}return arr2;}}

原创粉丝点击