【hihocoder】#1094 : Lost in the City

题目链接: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.

思路:
在一个二维数组中匹配一个3*3的正方形区域,该区域是可以90 180 270度旋转的。

算法:

import java.util.ArrayList;
import java.util.Scanner;
/**
 * http://hihocoder.com/problemset/problem/1094
 */
public class Main {

    public static void main(String[] args) {
        Main m = new Main();
        m.handleInput();
    }

    public void handleInput() {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        char map[][] = new char[n][m];

        for (int i = 0; i < n; i++) {
            char s[] = sc.next().trim().toCharArray();
            for (int j = 0; j < m; j++) {
                map[i][j] = s[j];
            }
        }

        char pos[][] = new char[3][3];
        for (int i = 0; i < 3; i++) {
            char s[] = sc.next().trim().toCharArray();
            for (int j = 0; j < 3; j++) {
                pos[i][j] = s[j];
            }
        }

        ArrayList<int[]> res = search(map,pos);
        for(int arr[]:res){
            System.out.println((1+arr[0])+" "+(1+arr[1]));
        }
    }

    public ArrayList<int[]> search(char[][] map, char pos[][]) {
        ArrayList<int[]> res = new ArrayList<>();
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[0].length; j++) {
                if (i + 2 >= map.length || j + 2 >= map[0].length)
                    continue;

                boolean flag = false;
                for (int ii = 0,r = i; ii < 3; ii++, r++) {
                    for (int jj = 0, c = j; jj <3; jj++, c++) {
                        if (pos[ii][jj] != map[r][c]) {
                            flag = true;
                            break;
                        }
                    }
                }
                if (!flag) {
                    res.add(new int[] { i + 1, j + 1 });
                    continue;
                } else {
                    flag = false;
                }
                // 顺时针 90
                for (int ii = 0,r = i; ii < 3; ii++, r++) {
                    for (int jj = 2, c = j; jj >= 0; jj--, c++) {
                        if (pos[jj][ii] != map[r][c]) {
                            flag = true;
                            break;
                        }
                    }
                }
                if (!flag) {
                    res.add(new int[] { i + 1, j + 1 });
                    continue;
                } else {
                    flag = false;
                }
                // 逆时针 90
                for (int ii = 2,r = i; ii >= 0; ii--, r++) {
                    for (int jj = 0, c = j; jj < 3; jj++, c++) {
                        if (pos[jj][ii] != map[r][c]) {
                            flag = true;
                            break;
                        }
                    }
                }
                if (!flag) {
                    res.add(new int[] { i + 1, j + 1 });
                    continue;
                } else {
                    flag = false;
                }
                // 180
                for (int ii = 2,r = i; ii >= 0; ii--, r++) {
                    for (int jj = 2, c = j; jj >= 0; jj--, c++) {
                        if (pos[ii][jj] != map[r][c]) {
                            flag = true;
                            break;
                        }
                    }
                }
                if (!flag) {
                    res.add(new int[] { i + 1, j + 1 });
                    continue;
                }
            }
        }
        return res;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值