微软苏州校招笔试 12月27日 By Java

注意考虑排序和相同项剔除

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.

import java.lang.reflect.Array;
import java.util.*;

/**
 * Created by 95112 on 2/26/2018.
 */
public class LostInTheCity {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int N,M;
        char[][] map;
        String input;
        N = scanner.nextInt();
        M = scanner.nextInt();
        scanner.nextLine();
        map = new char[N][M];
        for (int i = 0 ; i < N; i++) {
            input = scanner.nextLine();
            map[i] =  input.toCharArray();
        }
        char[][] location = new char[3][3];
        for (int i = 0 ; i < 3; i++){
            input = scanner.nextLine();
            location[i] = input.toCharArray();
        }
        char[][] l2,l3,l4;
        l2 = new char[3][3];
        l3 = new char[3][3];
        l4 = new char[3][3];
        for (int i = 0; i< 3; i++)
            for(int j = 0 ; j < 3 ; j++)
            {
                char tmp = location[i][j];
                l2[2-j][i] = tmp;
                l3[2-i][2-j] = tmp;
                l4[j][2-i] = tmp;
            }
        ArrayList<int[]> answers = new ArrayList<>();
        answers.addAll(find(map,location));
        answers.addAll(find(map,l2));
        answers.addAll(find(map,l3));
        answers.addAll(find(map,l4));
        Comparator<int[]> comparator = new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1==null)
                    return 1;
                else if (o2 == null)
                    return -1;
                else {
                    if (o1[0] < o2[0])
                        return -1;
                    else if (o1[0] > o2[0])
                        return 1;
                    else {
                        if (o1[1] < o2[1])
                            return -1;
                        else
                            return 1;
                    }
                }
            }
        };
        answers.sort(comparator);
        HashMap<Integer,ArrayList<Integer>> hashmap = new HashMap<>();
        for (int[] answer:answers){
            if (!hashmap.containsKey(answer[0])) {
                System.out.println(answer[0] + " " + answer[1]);
                ArrayList<Integer> arrayList = new ArrayList<>();
                arrayList.add(answer[1]);
                hashmap.put(answer[0],arrayList);
            }
            else{
                ArrayList<Integer> arrayList = hashmap.get(answer[0]);
                if (!arrayList.contains(answer[1])){
                    System.out.println(answer[0] + " " + answer[1]);
                    arrayList.add(answer[1]);
                    hashmap.put(answer[0],arrayList);
                }
            }
        }


    }
    private static ArrayList<int[]> find(char[][] map, char[][] around){
        int N,M;
        N = map.length;
        M = map[0].length;
        ArrayList<int[]> answers  = new ArrayList<>();
        for (int i = 0 ; i < N-2;i++)
            for (int j = 0 ; j < M-2; j++)
            {
                if (map[i][j] == around[0][0]){
                    boolean isError = false;
                    for (int x = 0;x <3 ;x++) {
                        if (isError)
                            break;
                        for (int y = 0; y < 3; y++) {
                            if (map[i + x][j + y] != around[x][y]) {
                                isError = true;
                            }
                        }
                    }
                    if (isError == false) {
                        int[] answer = new int[2];
                        answer[0] = i+2;
                        answer[1] = j+2;
                        answers.add(answer);
                    }
                }
            }

        return answers;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值