DMOPC '17 Contest 1 P1 - Fujō Neko

View as PDF

Submit solution

Points: 3 (partial)
Time limit: 3.0s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Saki is walking around the school fields when she notices that something might be stalking her. As such, she stops at Q locations on the field, with the i^\text{th} being (x_i,y_i) and takes a look directly in all 4 cardinal directions (north, south, east, and west) to see if she can locate these mysterious beings. Saki's vision is quite good, so even if the being is far away, as long as she's looking in the right direction, she is able to see it. (The top left corner is (1, 1) and the bottom right corner is (C, R).) Can you help Saki figure out if she's being stalked?


For all subtasks,
1 \le x_i \le C
1 \le y_i \le R

Subtask 1 [20%]

1 \le R, C \le 1\,000
1 \le Q \le 1\,000

Subtask 2 [80%]

1 \le R, C \le 1\,000
1 \le Q \le 10^6

Input Specification

The first line will contain 2 space-separated integers, R and C, the number of rows and columns her school field has.
The next R lines will each have C characters, with a X representing a mysterious being, and an . representing flat land.
The next line will have an integer, Q.
The next Q lines will have two integers, x_i and y_i, her position on the field for the i^{\text{th}} location.

Output Specification

The output should have Q lines, either Y if she can see one of the mysterious beings, or N otherwise.

Note that fast input/output may be necessary.

Sample Input

4 4
2 3
4 4
1 1

Sample Output


Explanation for Sample Output

Let S denote Saki's position. The first query looks like this:


For the second query, the grid looks like the following:


For the third query, the grid looks like this:


Note that even though Saki is on the same square as a mysterious being, she can still see it.


  • 0
    Dan13llljws  commented on Jan. 10, 2020, 11:08 p.m.

    How come when I use fast I/O I TLE.

    • 3
      richardzhang  commented on Jan. 10, 2020, 11:14 p.m.

      It TLEs because you're trying to mix your scan function with cin when you didn't sync cin with stdio.

      As a rule of thumb, usually just including cin.tie(0), ios::sync_with_stdio(0) is fast enough for most applications :).

  • -2
    tiger2018  commented on Nov. 4, 2017, 11:22 a.m.

    Another question about anime by Kirito. No surprise there.

  • -1
    mhamedkchikech  commented on Nov. 3, 2017, 7:49 p.m.

    Please what's wrong in my code ? All my tests are correct but the autocorrection say that is wrong

    • 0
      injust  commented on Nov. 3, 2017, 8:21 p.m.

      Take another look at the input specification:

      The next R lines will each have C characters