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
import java.util.PriorityQueue; import java.util.Scanner; public class Main { private boolean isHere(char[][] hi, char[][] cityMap, int row, int column) { if (hi[1][1] != cityMap[row][column]) return false; boolean res = true; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (hi[i][j] != cityMap[row + i - 1][column + j - 1]) { return false; } } } return true; } public static void main(String args[]){ class Pair implements Comparable{ int x, y; Pair(int x,int y){ this.x = x; this.y = y; } @Override public int compareTo(Object o) { Pair other = (Pair)o; if(this.x==other.x){ return this.y - other.y; } // TODO Auto-generated method stub return this.x - other.x; } } /* Scanner sc = null; try { sc = new Scanner(new FileInputStream("D:\\Desktop\\test.txt")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } */ Scanner sc = new Scanner(System.in); Main main = new Main(); int row = sc.nextInt(); int column = sc.nextInt(); char [][]cityMap = new char[row][column]; for(int i=0;i<row;i++){ String str = sc.next(); for(int j=0;j<column;j++){ cityMap[i][j]= str.charAt(j); } } char[][] hi1 = new char[3][3]; char[][] hi2 = new char[3][3]; char[][] hi3 = new char[3][3]; char[][] hi4 = new char[3][3]; for(int i=0;i<3;i++){ String str = sc.next(); for(int j=0;j<3;j++){ char c = str.charAt(j); hi1[i][j] = c; hi2[2-i][2-j] =c; hi3[2-j][i] =c; hi4[j][2-i] =c; } } PriorityQueue<Pair> pq = new PriorityQueue<>(); for(int i=1;i<row-1;i++){ for(int j=1;j<column-1;j++){ if(main.isHere(hi1, cityMap, i, j)||main.isHere(hi2, cityMap, i, j) ||main.isHere(hi3, cityMap, i, j)||main.isHere(hi4, cityMap, i, j)){ pq.add(new Pair(i,j)); } } } Pair p = pq.poll(); while(p!=null){ System.out.println((p.x+1)+" "+(p.y+1)); p = pq.poll(); } } }
[HiHoCoder]#1094 : Lost in the City 微软苏州校招笔试 12月27日
原文地址:http://blog.csdn.net/guorudi/article/details/44523289