Facebook
From Corrupt Flamingo, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 192
  1. #include<stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int count_routes(int** board, int N, int from_x, int from_y, int to_x, int to_y) {
  5.     int routes = 0;
  6.     if(from_x==to_x && from_y == to_y) {
  7.         routes++;
  8.     }
  9.     else {
  10.         if(board[from_x+1][from_y] != 0 && from_x+1<N && from_y+1<=N){
  11.     routes+=count_routes(board,N,from_x+1,from_y, to_x, to_y);  
  12.         }
  13.         if(board[from_x][from_y+1] != 0 && from_x+1<=N && from_y+1<N ){
  14.     routes+=count_routes(board,N,from_x,from_y+1, to_x, to_y);  
  15.         }
  16.         if(board[from_x+1][from_y+1] != 0 && from_x+1<N && from_y+1<N){
  17.     routes+=count_routes(board,N,from_x+1,from_y+1, to_x, to_y);
  18.         }
  19.     }
  20.     /*********************
  21.     * put your code here *
  22.     *********************/
  23.     return routes;
  24. }
  25.  
  26. int main() {
  27.     int N;
  28.     scanf("%d", &N);
  29.     int** board = malloc(N*sizeof(int*));
  30.     for (int i=0; i<N; i++) {
  31.         board[i] = malloc(N*sizeof(int));
  32.         for (int j=0; j<N; j++) scanf("%d", &board[i][j]);
  33.     }
  34.  
  35.     int from_x, from_y, to_x, to_y;
  36.     scanf("%d", &from_x);
  37.     scanf("%d", &from_y);
  38.     scanf("%d", &to_x);
  39.     scanf("%d", &to_y);
  40.     printf("%d\n", count_routes(board, N, from_x, from_y, to_x, to_y));
  41.  
  42.     for(int i=0; i<N; i++) free(board[i]);
  43.     free(board);
  44. }