아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

AibohphobiA

시간 제한3초메모리 제한1024 MB

요약
각 질의 칸마다 왼쪽 위에서 오른쪽 아래로 가는 경로 중 길이 2 또는 3의 회문 부분 문자열이 없는 가장 긴 경로의 길이를 구하고, 무한히 길 수 있으면 -1, 아예 없으면 -2를 출력한다.
난이도

보통10점 중 7점

유형
그래프, DFS, 완전 탐색, 구현
정답자
아직 제출이 없습니다

문제

You are given a rectangular grid of MM rows and NN columns. The rows and columns are indexed from 00 to M−1M - 1 and from 00 to N−1N - 1 respectively. In each grid cell (i,j)(i, j), there is a lowercase letter character A\[i,j]A\[i, j]. This grid represents a maze, and the goal to solve the maze is to find a walk going from (0,0)(0, 0) to (M−1,N−1)(M - 1, N - 1). The walk consists of several steps. In each step you can choose one of the four directions (going from a grid cell to a neighboring cell that shares an edge.) Notice that it is okay to revisit a cell multiple times during the walk, including the starting cell (0,0)(0, 0) and the ending cell (M−1,N−1)(M - 1, N - 1). If you record all characters along the walk, you’ll get a string that represents this walk.

Truckski is not a fan of palindromes, so he would like to find a walk that does not contain any palindromic substrings of length at least two, which he called a good walk. A string s_1s_2⋯s_ks\_1s\_2 \cdots s\_k is called a palindrome, if it reads the same after reversing the string, i.e., s_1s_2⋯s_k=s_ks_k−1⋯s_1s\_1s\_2 \cdots s\_k = s\_ks\_{k-1} \cdots s\_1. A substring of a string can be obtained by removing a (possibly empty) prefix and a (possibly empty) suffix.

Now, there are QQ interesting locations (r_i,c_i)Q_i=1\\{(r\_i , c\_i)\\}^Q\_{i=1} that Truckski wishes to visit. For each location (r_i,c_i)(r\_i , c\_i), can you help Truckski to find the length of the longest good walk that visits the location grid cell (r_i,c_i)(r\_i , c\_i) at least once? If there are arbitrarily long good walks please output −1-1. If there does not exist any good walk, please output −2-2.

입력

The first line contains an integer TT, indicating the number of test cases. For each test case, there are two integers MM and NN in the first line. In each of the following MM lines there is a string of length NN, the cc-th character in the rr-th line is the character A\[r,c]A\[r, c]. The next line contains an integer QQ. In each of the following QQ lines there are two integers r_ir\_i and c_ic\_i indicating the location of interest.

출력

For each interesting location, output the length of the longest good walk that visits this location at least once, or −1-1 if the good walk can be arbitrarily long, or −2-2 if there does not exist such a good walk.

제한

  • T≤20T ≤ 20
  • 2≤M≤1002 ≤ M ≤ 100
  • 2≤N≤1002 ≤ N ≤ 100
  • 1≤Q≤1001 ≤ Q ≤ 100
  • For all ii such that 1≤i≤Q1 ≤ i ≤ Q, 0≤r_i<M0 ≤ r\_i < M and 0≤c_i<N0 ≤ c\_i < N.
  • For each grid cell (r,c)(r, c), A\[r, c] ∈ \\{a, b, ... , z\\} is a lowercase letter.

힌트

This problem is not the easiest problem in this contest.

예제1

  1. 예제 1

    입력
    3
    3 5
    abbba
    bccab
    cabcc
    2
    0 1
    1 0
    3 4
    aaba
    bbaa
    abab
    1
    1 1
    4 4
    abca
    cxxb
    bxxc
    acba
    1
    0 1
    
    예상 출력
    9
    9
    -2
    -1