AibohphobiA
시간 제한3초메모리 제한1024 MB
각 질의 칸마다 왼쪽 위에서 오른쪽 아래로 가는 경로 중 길이 2 또는 3의 회문 부분 문자열이 없는 가장 긴 경로의 길이를 구하고, 무한히 길 수 있으면 -1, 아예 없으면 -2를 출력한다.
문제
You are given a rectangular grid of rows and columns. The rows and columns are indexed from to and from to respectively. In each grid cell , there is a lowercase letter character . This grid represents a maze, and the goal to solve the maze is to find a walk going from to . 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 and the ending cell . 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 is called a palindrome, if it reads the same after reversing the string, i.e., . A substring of a string can be obtained by removing a (possibly empty) prefix and a (possibly empty) suffix.
Now, there are interesting locations that Truckski wishes to visit. For each location , can you help Truckski to find the length of the longest good walk that visits the location grid cell at least once? If there are arbitrarily long good walks please output . If there does not exist any good walk, please output .
입력
The first line contains an integer , indicating the number of test cases. For each test case, there are two integers and in the first line. In each of the following lines there is a string of length , the -th character in the -th line is the character . The next line contains an integer . In each of the following lines there are two integers and 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 if the good walk can be arbitrarily long, or if there does not exist such a good walk.
제한
- For all such that , and .
- For each grid cell , A\[r, c] ∈ \\{
a,b, ... ,z\\} is a lowercase letter.
힌트
This problem is not the easiest problem in this contest.