遞迴_From One Corner with Obstacles_錯了

 avatar
user_3763047219
c_cpp
2 years ago
936 B
0
Indexable
Never
#include <stdio.h>

int step(int r, int c,int arr[100][100],int i0,int j0) {
	while (r >= 0 && c >= 0) {
		if (i0 == 1) {
			if (r == 0 && c != 0) {
				return 0;
			}
		}
		if (j0 == 1) {
			if (r != 0 && c == 0) {
				return 0;
			}
		}
		
		if(i0!=1 &&j0!=1){
			if (r != 0 && c == 0) {
				return 1;
			}
			else if (r == 0 && c != 0) {
				return 1;
			}
		}

		if (r == 0 && c == 0) {
			return 0;
		}
		else if (arr[r][c] == 0) {
			return 0;
		}
		else {
			return step(r - 1, c,arr,i0,j0) + step(r, c - 1,arr,i0,j0);
		}
	}
}
int main() {
	int r = 0, c = 0;
	scanf("%d %d", &r, &c);
	int arr[100][100] = {};
	int i0 = 0;
	int j0 = 0;
	for (int i = r-1; i >=0; i--) {
		for (int j =0; j <c; j++) {
			scanf("%d", &arr[i][j]);
			if (arr[i][j] == 0 && i == 0) {
				i0=1;
			}
			if (arr[i][j] == 0 && j == 0) {
				j0=1;
			}
		}
	}
	printf("%d", step(r-1, c-1,arr,i0,j0));
}