题目地址: http://hihocoder.com/problemset/problem/1094
描述
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
Java 代码如下:
import java.util.Scanner;public class Main {public static void main(String[] args) {int t,n,m;int[][] me=new int[3][3];Scanner in =new Scanner(System.in);n=in.nextInt();m=in.nextInt();int[][] map=new int[m][n] ;for(int i=0;i<n;i++){String temp=in.next();for(int j=0;j<m;j++){map[i][j]=temp.charAt(j);}}for(int i=0;i<3;i++){String temp=in.next();for(int j=0;j<3;j++){me[i][j]=temp.charAt(j);}}for(int i=1;i<n-1;i++){for(int j=1;j<m-1;j++){if(map[i][j]!=me[1][1]) continue;if(map[i-1][j]==me[0][1]&&map[i-1][j-1]==me[0][0]&&map[i-1][j+1]==me[0][2]){ //正if(map[i][j-1]==me[1][0]&&map[i][j+1]==me[1][2]){if(map[i+1][j-1]==me[2][0]&&map[i+1][j]==me[2][1]&&map[i+1][j+1]==me[2][2]){System.out.println((i+1)+" "+(j+1)); continue;}}}if(map[i-1][j]==me[1][0]&&map[i-1][j-1]==me[2][0]&&map[i-1][j+1]==me[0][0]){if(map[i][j-1]==me[2][1]&&map[i][j+1]==me[0][1]){if(map[i+1][j-1]==me[2][2]&&map[i+1][j]==me[1][2]&&map[i+1][j+1]==me[0][2]){System.out.println((i+1)+" "+(j+1)); continue;}}}if(map[i-1][j]==me[2][1]&&map[i-1][j-1]==me[2][2]&&map[i-1][j+1]==me[2][0]){if(map[i][j-1]==me[1][2]&&map[i][j+1]==me[1][0]){if(map[i+1][j-1]==me[0][2]&&map[i+1][j]==me[0][1]&&map[i+1][j+1]==me[0][0]){System.out.println((i+1)+" "+(j+1)); continue;}}}if(map[i-1][j]==me[1][2]&&map[i-1][j-1]==me[0][2]&&map[i-1][j+1]==me[2][2]){if(map[i][j-1]==me[0][1]&&map[i][j+1]==me[2][1]){if(map[i+1][j-1]==me[0][0]&&map[i+1][j]==me[1][0]&&map[i+1][j+1]==me[2][0]){System.out.println((i+1)+" "+(j+1)); continue;}}}}}}}