The pointer didn't point at the right place in the matrix. I would like to ask why. The following is the description of the question I've been working on, which is from JudgeGirl.
Description Write a program to compute the number of ways to go from the lower left corner of a matrix to upper right corner. The matrix has r rows and c columns. You can only move one cell to another cell, and only to the right or to the top. Also there are obstacles in the matrix and you cannot go into any cell that is obstacle . Please compute the number of ways you can go. Note that you are strongly suggested to think in "recursion" to solve this problem.
Limits Both r and c are no more than 12 .
Input Format There are r+1 lines in the input. The first line has r and c The next r lines have the status of the matrix. A 1 indicates a cell you can go through, and a 0 is an obstacle. Both the lower left and upper right corners are 1's.
Here's my code, and it turns out that the pointer doesn't point at the right address:
#include <stdio.h>
#include <assert.h>
void count(int x, int y, int r, int c, int *ptr, int *ways){
if((*ptr==0)||(x>(c-1))||(y<0)){
return;
}else{
//printf("(%d, %d) %d", x, y, *matrix);
}
if((x==(c-1))||(y==0)){
*ways += 1;
return;
}
count(x+1, y, r, c, &(*(ptr+1)), &(*ways));
count(x, y-1, r, c, &(*(ptr-c)), &(*ways));
return;
}
int main(){
int r, c; //y:r x:c
scanf("%d %d", &r, &c);
assert((r<=12)&&(c<=12));
int matrix[c][r];
for(int j=0; j<r; j++){
for(int i=0; i<c; i++){
scanf("%d", &matrix[i][j]);
//printf("%d ", matrix[i][j]);
}
//printf("\n");
}
int ways = 0;
count(0, r-1, r, c, &matrix[0][r-1], &ways); //from(0,r-1) to (c-1,0)
printf("%d", ways);
return 0;
}