hihocoder#1094 : Lost in the City

Posted wuezs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    

以上是关于hihocoder#1094 : Lost in the City的主要内容,如果未能解决你的问题,请参考以下文章

lightoj-1094 Farthest Nodes in a Tree(求树的直径)

light oj 1094 Farthest Nodes in a Tree(树的直径模板)

Farthest Nodes in a Tree LightOJ - 1094

方程式EQGRP_Lost_in_Translation工具之fb.py

After change SessionID data in Session variables is lost

[bfs,深度记录] East Central North America Regional Contest 2016 (ECNA 2016) D Lost in Translation