Advertisement
valve2

2D shortest route

May 14th, 2023 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | Software | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #define size 10
  6. #define rows 5
  7. #define cols 5
  8. void route(int arr[rows][cols]) {
  9.         int i=0,j=0;
  10.         while(i<rows && j < cols){
  11.             if(i==rows-1 && j == cols-1)
  12.                 break;
  13.  
  14.             else if(i==rows-1){
  15.                 printf("right ");
  16.                 j++;
  17.             }
  18.             else if(j==cols-1){
  19.                 printf("down ");
  20.                 i++;
  21.             }
  22.             else if(arr[i+1][j]<arr[i][j+1]){
  23.             printf("down ");
  24.             i++;
  25.             }
  26.            else{
  27.             printf("right ");
  28.             j++;
  29.             }
  30.         }
  31. }
  32. int main() {
  33.     int arr[rows][cols];
  34.     srand(time(NULL));
  35.     for (int i = 0; i < rows; i++) {
  36.         for (int j = 0; j < cols; j++) {
  37.             arr[i][j] = (0 + rand() % (9 - 0 + 1));
  38.         }
  39.     }
  40.     puts("arr");
  41.     for (int i = 0; i < rows; i++) {
  42.         for (int j = 0; j < cols; j++) {
  43.             printf("%d ", arr[i][j]);
  44.         }
  45.         puts("");
  46.     }
  47.     route(arr);
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement