C++ 2D array/dynamic programming question

Hello,

I have trouble solving this programming question.
Here is the question:

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 ≤ R, C ≤ 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


By incorporating some example answers, my codes are like this:
#include <iostream>

int count = 0;
int x;
int y;
int cats;
int catX = 0;
int catY = 0;
int num;
int xFirst; int yFirst;

int numberOfPaths (int m, int n, int *catXX, int *catYY)
{
// for (int i = 0; i < num; i++)
// {
// if (m != catXX[i] && m != catYY[i])
// {
//
// }
// else
// {
// return;
// }
//
// if (i == num-1)
// {
// int x;
// int y;
// if (m < catXX[i] < xFirst)
// {
// x = catXX[i] - m;
// }
// if (n < catYY[i] < yFirst)
// {
// y = catYY[i] - n;
// }
// count = count*x*y;
// return;
// }
// }

int isIt[m][n];


int count[m][n];

for (int heyyy = 0; heyyy < num; heyyy++)
{
int ho = catXX[heyyy];
int hoho = catYY[heyyy];
std::cout << ho << hoho;
isIt[ho][hoho] = 5;


}


for (int i = 0; i < m; i++)
{
count[i][0] = 1;
}

for (int j = 0; j < n; j++)
{
count [0][j] = 1;
}


for (int i = 1; i < m; i++)
{
for (int j = 1; j < n; j++)
{

count[i][j] = count[i-1][j] + count[i][j-1];

if (isIt[i+1][j] == 5)
{
count[i][j]--;
}
if (isIt[i][j+1] == 5)
{
count[i][j]--;
}


}
}
return count[m-1][n-1];
}
int main(int argc, const char * argv[])
{

// insert code here...

std::cin >> xFirst >> yFirst;
std::cin >> num;
int catX[num];
int catY[num];

for (int i = 0; i < num; i++)
{
std::cin >> catX[i] >> catY[i];


}
// for (int i = 1; i < xFirst; i++)
// {
// for (int j = 1; j < yFirst; j++)
// {
// numberOfPaths(i, j, catX, catY);
// }
// }
std::cout << count;
std::cout << numberOfPaths(xFirst, yFirst, catX, catY);

return 0;
}

When the input is:
3 4 (the location of the brother)
3 (how many cats there are)
2 3 (location of the first cat)
2 1 (location of the second cat)
1 4, (location of the third cat)

it is supposed to be 1.
However, I get 4.

I'm not sure what I did wrong.

Thanks in advance.
Last edited on
Bump. Sorry.
Last edited on
This is an algorithm question.
Forget C++ for the moment, think about the algorithm. And then work it out on the example by hand on paper. And see where the first error, if any, occurs.
Topic archived. No new replies allowed.