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 rows and
columns of cages (
).
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 (to indicate top-most row, left-most column). You would like to visit your brother who lives in the cage labelled
(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 and
, separated by one space representing the number of rows and columns (respectively). On the second line of input is the integer
, the number of cages that contain cats. The next
lines each contain the row and column positions (in that order) for a cage that contains a cat. None of the
cat cages are repeated, and all cages are valid positions. Note also that
and
will not be cat cages.
Output Specification
Output the non-negative integer value representing the number of paths between your cage at position and your brother's cage at position
. You can assume the output will be strictly less than
.
Sample Input 1
2 3
1
2 1
Output for Sample Input 1
2
Sample Input 2
3 4
3
2 3
2 1
1 4
Output for Sample Input 2
1
Comments
it seems you can solve it using bfs
This comment is hidden due to too much negative feedback. Show it anyway.
Where is your combinatorics solution then?
This comment is hidden due to too much negative feedback. Show it anyway.
You would have to look at the number of ways to get to R,C, then subtract ways to get to catr, caty * ways to get from catr, catc to r,c. But if there are more cats, you would have to use PIE, which is very complicated.
I highly recommend https://dmoj.ca/problem/halfgrid to anyone who can find a combinatorics solution.
spittin' facts
Um, can someone help me? I'm getting TLE in case #5 and #7.
Thanks!
Join the DMOJ Discord for help.
why am I getting WC in 3 cases?
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:
You get
1234
as output becausea[0][99]
anda[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.
Thank you so much. Your comment was very helpful.
Classic DP problem.
This comment is hidden due to too much negative feedback. Show it anyway.
i.e. a dp question
What if R,C=1,1?
I submitted literally the exact same code onto wcipeg and got AC. Anyone know why this happened?
You're checking if
, or because the compiler initialized the array to
, or perhaps something entirely different the compiler pretty much has freedom to do whatever it wants if it gets undefined behaviour.
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'tgot it now, thanks!