Advertisement
Mr_kindle

rottentomatoes.c

Nov 15th, 2022 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.56 KB | None | 0 0
  1. /*Given a grid of dimension nxm where each cell in the grid can have values 0, 1 or 2 which has the
  2.  following meaning:
  3. 0 : Empty cell
  4. 1 : Cells have fresh oranges
  5. 2 : Cells have rotten oranges
  6.  
  7. We have to determine what is the minimum time required to rot all oranges.
  8.  A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1]
  9.   (up, down, left and right) in unit time.*/
  10.  
  11. /*Input: grid = {{0,1,2},{0,1,2},{2,1,1}}
  12. Output: 1
  13. Explanation: The grid is-
  14. 0 1 2
  15. 0 1 2
  16. 2 1 1
  17. Oranges at positions (0,2), (1,2), (2,0)
  18. will rot oranges at (0,1), (1,1), (2,2) and
  19. (2,1) in unit time*/
  20.  
  21. #include<stdio.h>
  22. #include<stdlib.h>
  23. #include<malloc.h>
  24. #define MAX 50
  25. #define initial 1
  26. #define visited 2
  27. int adj[MAX][MAX];
  28. int queue[MAX],front = -1, rear = -1;
  29. int state[MAX],n;
  30. void insert_queue(int vertex);
  31. int isempty_queue();
  32. int delete_queue();
  33. void printarray(int * ptr);
  34. void  make_adjacency_matrix(int * array);
  35. int BFS(int * array);
  36.  
  37. int main()
  38. {   int *array,i,j,pop,time;
  39.     int rotten = 0;
  40.     printf("\nEnter n for n*n matrics: ");
  41.     scanf("%d", &n);
  42.     array = calloc(n*n,sizeof(int));
  43.     printf("\nPlease enter elment of matrix: \n");
  44.     for(i=0;i<n*n;i++)
  45.         {
  46.             printf("Enter element no %d: ",i+1);
  47.             scanf("%d",&array[i]);
  48.         }
  49.     printf("\nThe tomatoes in beginning is following: \n");
  50.     printarray(array);
  51.  
  52.     make_adjacency_matrix(array);
  53.  
  54.    /* printf("\nThe adjacency matrix is: \n");
  55.     for(i=0;i<n*n;i++)
  56.     {
  57.            for(j=0;j<4;j++)
  58.                 printf("%d ",adj[i][j]);
  59.             printf("\n");
  60.     }*/
  61.     /*change state of all elements to initial*/
  62.     for(i=0;i<n*n;i++)
  63.         state[i] = 1;
  64.        
  65.        
  66.        
  67.     /*Search for all rotten tomatoes in array initially*/
  68.     for(i=0;i<n;i++)
  69.         for(j=0;j<n;j++)
  70.            if(array[i*n + j]==2)
  71.             {   /*since rotten found put its place-value inside queue and change its state from initial to visited*/
  72.                 /*place value tells us which particular tomatoes is rotten*/
  73.                 insert_queue(i*n + j);
  74.                 state[i*n + j] = 2;
  75.             }
  76.             /*Now after putting all rotten tomatoes in queue put an delimiter (-1) */
  77.             /*delimiter differentiate between the tomatoes rotten first day and the next day and so on...*/
  78.             /*tomatoes before delimiter is previous day rotten and after the delimiter are next day rotten*/
  79.             insert_queue(-1);
  80.            
  81.            time = BFS(array);
  82.            
  83.            /*Now check whether all tomatoes are rotten or not*/
  84.            /*If all rotten then tell the number of days in which all tomatoes are rotten*/
  85.    
  86.     for(i=0;i<n;i++)
  87.         {  
  88.             for(j=0;j<n;j++)
  89.                 {
  90.                     if(array[i*n + j] == 1)
  91.                         {    rotten++;
  92.                             break;
  93.                         }
  94.                 }
  95.             if(rotten)
  96.                 break;
  97.         }
  98.     if(rotten)
  99.         printf("\nAll tomato will not be rotten:  ");
  100.     else
  101.         printf("\n All tomatoes will be rotten in %d days: ",time);
  102.    
  103.            
  104.  free(array);  
  105. return 0;
  106. }//main
  107.  
  108. /*Main ends here and below is the definition of all the function being used*/
  109.  
  110. void  make_adjacency_matrix(int * array)
  111.     {
  112.         int i,j;
  113.             for(i=0;i<n;i++)
  114.         {  
  115.             for(j=0;j<n;j++)
  116.         /*check adjacency only when there is tomato. (means array[i][j] != 0)*/
  117.                 {   if(array[i*n + j]!= 0)
  118.                    
  119.                     {    //check left adjacency
  120.             /*if j=0 then it will be first column and no element in left possible*/
  121.             /*if it is not first column then check the left element it must be non-zero*/
  122.                          if(j!=0 && array[i*n + (j-1)] != 0)
  123.                             adj[i*n + j][0] = 1;
  124.                        
  125.                         //check right adjacency
  126.             /*if j=n-1 then it will be last column and no element in right side possible*/
  127.             /*if it is not last column then check the right element and it must be non-zero*/
  128.                         if(j!=n-1 && array[i*n + (j+1)]!=0)
  129.                             adj[i*n + j][1] = 1;
  130.                        
  131.                         //check up adjacency
  132.             /*if i=0 then it will be first row and no element in upside possible*/
  133.             /*if it is not the first row then check the upside element it must be non-zero*/
  134.                         if(i!=0 && array[(i-1)*n + j]!=0)
  135.                             adj[i*n + j][2] = 1;
  136.                        
  137.                         //check down adjacency
  138.             /*if i=n-1 then it will be last row and no element in downside possible*/
  139.             /*if it is not the last row then chech the downside element it must be non-zero*/
  140.                         if(i!= n-1 && array[(i+1)*n + j]!= 0)
  141.                             adj[i*n + j][3] = 1;
  142.                        
  143.                      }
  144.                 }
  145.         }//end of for loop
  146.     }//end of make adjacency matrix
  147.    
  148. int BFS(int * array)
  149.     {
  150.          int time=0,pop,i,j;
  151.          while(!isempty_queue())
  152.         {  
  153.            pop = delete_queue();
  154.            if(pop == -1)
  155.            { /*if queue is  empty do nothing*/  
  156.             if(!isempty_queue())//after popping delimiter if there is some element still in queue then..
  157.                 {   time++;
  158.                     insert_queue(-1);
  159.                     printf("\nTomatoes after %d day: \n",time);
  160.                     printarray(array);
  161.                 }
  162.            
  163.            }
  164.            /*check all adjacency (left,right,up,down)*/
  165.            /*Since only 4 adjacency possible for any element hence loop operate for 4 time only*/
  166.            for(j=0;j<4;j++)
  167.             {
  168.                 if(adj[pop][j] == 1)//if adjacency found then..
  169.                 /*check whether it is left,right,up or downside adjacency*/
  170.                
  171.                     {   if(j==0 && state[pop-1] == initial)//for left adjacency, j=0 indicate left adjacency
  172.                             {   array[pop - 1] = 2;
  173.                                 state[pop - 1] = visited;
  174.                                 insert_queue(pop - 1);
  175.                             }
  176.                         if(j==1 && state[pop+1] == initial)//for right adjacency
  177.                             {   array[pop + 1] = 2;
  178.                                 state[pop + 1] = visited;
  179.                                 insert_queue(pop + 1);
  180.                             }
  181.                         if(j==2 && state[pop-n] == initial)//for up adjacency
  182.                             {   array[pop - n] = 2;
  183.                                 state[pop - n] = visited;
  184.                                 insert_queue(pop - n);
  185.                             }
  186.                         if(j==3 && state[pop+n] == initial)//for down adjacency
  187.                             {   array[pop + n] = 2;
  188.                                 state[pop + n] = visited;
  189.                                 insert_queue(pop + n);
  190.                             }
  191.                              
  192.      
  193.              }//j loop printf("\n*****\n");
  194.      
  195.              
  196.         }
  197.     }
  198.     return time;
  199.     }//end of BFS
  200.  
  201. void printarray(int * ptr)
  202.     {   int i ,j;
  203.          for(i=0;i<n;i++)
  204.         {  
  205.             for(j=0;j<n;j++)
  206.                 printf("%2d ",ptr[i*n + j]);
  207.                 printf("\n");
  208.         }
  209.     }
  210.  
  211. void insert_queue(int vertex)
  212.     {  
  213.         if(rear == MAX-1)
  214.             printf("queue overflown: \n");
  215.         else
  216.             {  
  217.                 if(front == -1)//check if queue is empty
  218.                     front++;//or front = 0;
  219.                 rear++;
  220.                 queue[rear] = vertex;
  221.             }
  222.     }//end of insert_queue
  223.    
  224. int isempty_queue()
  225.     {
  226.         if(front == -1 || front>rear)
  227.             return 1;
  228.         else
  229.             return 0;
  230.     }//end of isempty_queue
  231.  
  232. int delete_queue()
  233.     {  
  234.         int deleted_item;
  235.         /*front = -1 means (virgin queue) no value has been inserted yet in queue hence it is empty */
  236.         /*front>rear means some value had been inserted in queue but now all had been taken outside hence queue is empty*/
  237.         if(front == -1 || front>rear)
  238.             {
  239.                 printf("\nQueue is underflown: ");
  240.                 exit(1);
  241.             }
  242.         deleted_item = queue[front];
  243.         front++;
  244.         return deleted_item;
  245.        
  246.     }//end of delete_queue
  247.  
  248.  
  249.  
  250.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement