#1094 : Lost in the City(测试用例正确,但提交显示WA,求发现我程序错误)

1094 : Lost in the City(测试用例正确,但提交显示WA,求发现我程序错误)

描述

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.Scanner;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int M = sc.nextInt();

        char arr[][] = new char[N][M];
        char a[][] = new char[3][3];

        String str = null;

        sc.nextLine();
        for(int i = 0; i < arr.length; i++) {
            str = sc.nextLine();
            char chrArr[] = str.toCharArray();
            for(int j = 0; j < N; j++) arr[i][j] = chrArr[j];
        }

    //  sc.nextLine();

        for(int i = 0; i < 3; i++) {
            str = sc.nextLine();
            char chrArr[] = str.toCharArray();

            for(int j = 0; j < 3; j++) a[i][j] = chrArr[j];
        }







        //north
        new Main().compareMatrix(arr, a, N, M);

        //south
        a = new Main().rotateMatrix(a);
        a = new Main().rotateMatrix(a);
        new Main().compareMatrix(arr, a, N, M);

        //west
        a = new Main().rotateMatrix(a);
        new Main().compareMatrix(arr, a, N, M);

        //east
        a = new Main().rotateMatrix(a);
        a = new Main().rotateMatrix(a);
        new Main().compareMatrix(arr, a, N, M);






    }




    //针对N*N类型矩阵旋转90度
    char[][] rotateMatrix(char a[][]) {

        int row = a.length;


        char arr[][] = new char[row][row];

        int col = row - 1;
        int count = 0;

        for(int i = 0; i < row; i++) {

            int num = 0;

            for(int j = 0; j < row; j++) {
                arr[num++][col - count] = a[i][j];
            }

            count++;
        }

        return arr;

    }



    void compareMatrix(char arr[][], char a[][], int N, int M) {

        for(int i = 0; i + 2 < N; i++) {
            for(int j = 0; j + 2 < M; j++) {



                boolean flag = true;

                for(int ik = 0; ik < 3; ik++) {
                    for(int jk = 0; jk < 3; jk++) {

                        if(a[ik][jk] != arr[i + ik][j + jk]) {
                            flag = false;
                            break;
                        }
                    }
                }

                if(flag == false) continue;
                else {
                    int tempI = i + 2;
                    int tempJ = j + 2;

                    System.out.println(tempI + "" + tempJ);

                    //return;
                }

            }
        }
    }



}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值