Babel

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

요약
언어 지역으로 이루어진 격자에서 두 칸을 같은 언어 지역만 지나 연결할 수 있는지, 있다면 어떤 언어인지 답하는 문제입니다.
난이도

보통10점 중 4점

유형
그래프, DFS, 유니온 파인드, 행렬
정답자
아직 제출이 없습니다

문제

The Tower of Babel is a biblical story in Genesis in which the entire world had one language and a common speech. Of course, the moment the entire world could easily communicate they decided to build a tower that would reach to the heavens so they would not be "scattered over the face of the earth." God, however, did not allow the people to build this tower, and He stopped this by "confusing their language" so that they no longer had one common language.

After this scattering, a person only knew a single language and cross-cultural communication was impossible. Thus, it became vital for the people to know if they could travel from place to place without moving through an area where an unknown language was spoken. One day, when God was feeling particularly gracious, he published a map with the locations where each language was spoken. For example:

EEEE
FFFG
FGGG
FFAA
AAFA

Each letter represents a common language (in the example above, E = English, F = French, A = Arabic, G = Greek). Language areas are considered connected if any of the four adjacent areas (north, south, east, or west) speak the same language.

Given the map and qq pairs of coordinates, can you determine if an individual can move between the two coordinates without passing through an area that speaks another language?

입력

The first line of input contains two integers, 1≤R≤1,0001 \leq R \leq 1\\,000, denoting the number of rows in the map, and 1≤C≤1,0001 \leq C \leq 1\\,000, denoting the number of columns in the map. The following RR lines contain exactly CC characters, all of which will be uppercase letters of the alphabet A-Z not including N. The next line contains an integer 0≤Q≤1,0000 \leq Q \leq 1\\,000, indicating the number of queries to follow. The following QQ lines each contain one query, given as four space separated integers r_1,c_1,r_2,c_2r\_1, c\_1, r\_2, c\_2 such that 0≤r_1,r_2<R,0≤c_1,c_2<C0 \leq r\_1, r\_2 < R, 0 \leq c\_1, c\_2 < C. r_1r\_1 and c_1c\_1 are the row and column on the map from where the person is trying to travel, and r_2r\_2 and c_2c\_2 are the row and column on the map where the person is trying to travel to.

출력

For each query, output the character of the language that connects the two points without passing through an area that speaks another language. If it is not possible for any language to do this, output the letter "N".

예제2

  1. 예제 1

    입력
    5 4
    EEEE
    FFFG
    FGGG
    FFAA
    AAFA
    5
    0 0 0 1
    4 0 4 3
    1 0 1 2
    1 0 2 0
    3 1 4 2
    
    예상 출력
    E
    N
    F
    F
    N
    
  2. 예제 2

    입력
    10 10
    ABABABABAB
    BABABABABA
    AAAAAAAABA
    BBBBBBBBBA
    AAAAAAAAAA
    CCCCCCCAAA
    CDDDDDDDDA
    CDDDDDDDAA
    AAAAAAAAAA
    ADDDDDDDDD
    10
    0 0 0 1
    0 0 0 2
    0 1 0 3
    0 0 1 1
    1 9 9 0
    1 9 2 0
    1 8 3 0
    6 8 7 7
    9 9 9 1
    0 9 1 8
    
    예상 출력
    N
    N
    N
    N
    A
    N
    B
    D
    D
    N