CCC '12 S5 - Mouse Journey

View as PDF

Submit solution

Points: 10
Time limit: 1.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
Canadian Computing Competition: 2012 Stage 1, Senior #5

You are a mouse that lives in a cage in a large laboratory. The laboratory is composed of one rectangular grid of square cages, with a total of R rows and C columns of cages (1 \le R, C \le 25).

To get your exercise, the laboratory owners allow you to move between cages. You can move between cages either by moving right between two adjacent cages in the same row, or by moving down between two adjacent cages in the same column. You cannot move diagonally, left or up.

Your cage is in one corner of the laboratory, which has the label (1, 1) (to indicate top-most row, left-most column). You would like to visit your brother who lives in the cage labelled (R, C) (bottom-most row, right-most column), which is in the other corner diagonally. However, there are some cages which you cannot pass through, since they contain cats.

Your brother, who loves numbers, would like to know how many different paths there are between your cage and his that do not pass through any cat cage. Write a program to compute this number of cat-free paths.

Input Specification

The first line of input contains two integers R and C, separated by one space representing the number of rows and columns (respectively). On the second line of input is the integer K, the number of cages that contain cats. The next K lines each contain the row and column positions (in that order) for a cage that contains a cat. None of the K cat cages are repeated, and all cages are valid positions. Note also that (1, 1) and (R, C) will not be cat cages.

Output Specification

Output the non-negative integer value representing the number of paths between your cage at position (1, 1) and your brother's cage at position (R, C). You can assume the output will be strictly less than 1\,000\,000\,000.

Sample Input 1

2 3
2 1

Output for Sample Input 1


Sample Input 2

3 4
2 3
2 1
1 4

Output for Sample Input 2



  • 2
    georgezhang006  commented on July 5, 2020, 9:52 p.m.

    Um, can someone help me? I'm getting TLE in case #5 and #7.


  • 1
    nabil_boudraa38  commented on Aug. 27, 2019, 5:26 p.m.

    why am I getting WC in 3 cases?

    • 5
      hxxr  commented on Aug. 28, 2019, 12:41 a.m.

      Be careful! Your code is checking negative array indices in one of its loops. Be aware that because of how C/C++ arrays work some odd things happen when you index element -1 in a 2-dimensional array:

      #include <bits/stdc++.h>
      using namespace std;
      int main(){
          int a[100][100] = {0};
          a[0][99] = 1234;
          cout << a[1][-1];
          return 0;

      You get 1234 as output because a[0][99] and a[1][-1] are technically accessing the same position in memory.

      In summary, avoid negative index values. Make your array longer by 1 in both dimensions and adjust the variables in your loops.

      • 3
        nabil_boudraa38  commented on Aug. 29, 2019, 7:30 a.m.

        Thank you so much. Your comment was very helpful.

  • 2
    Suraj_IOI  commented on June 2, 2019, 12:06 p.m.

    Classic DP problem.

  • 0
    Suraj_IOI  commented on June 2, 2019, 11:29 a.m.

    What is IR during system test ?

  • 0
    31501357  commented on April 14, 2019, 12:15 p.m.

    What if R,C=1,1?

    • -9
      oofisoofoofoof  commented on April 25, 2020, 3:16 p.m. edited

      This comment is hidden due to too much negative feedback. Click here to view it.

    • 1
      Roronoa_Zoro1540  commented on April 14, 2019, 12:29 p.m.

      i thnk then u have to output 0, because you cant get from (0, 0) to (1, 1)...

  • 5
    bloxblox  commented on Jan. 18, 2019, 7:24 a.m.

    I submitted literally the exact same code onto wcipeg and got AC. Anyone know why this happened?

    • 8
      kingW3  commented on Jan. 18, 2019, 9:57 a.m.

      You're checking if if (arr[i][j] != -1) but arr[i][j] might not be initialized and hence might equal -1, this migth work on wcipeg because the "random values" aren't -1, or because the compiler initialized the array to 0, or perhaps something entirely different the compiler pretty much has freedom to do whatever it wants if it gets undefined behaviour.

      • 4
        bloxblox  commented on Jan. 22, 2019, 8:52 p.m.

        got it now, thanks!

    • -13
      magicalsoup  commented on Jan. 18, 2019, 9:54 a.m.

      This comment is hidden due to too much negative feedback. Click here to view it.