home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / CataniaB / teach-act / esempi / Comp-Sep / lab.c < prev    next >
C/C++ Source or Header  |  1997-04-19  |  1KB  |  49 lines

  1. #include <stdio.h>
  2. #include "stack.h"
  3. #include "input.h"
  4.  
  5. stack S;    /* utilizzo pop, push, is_empty_stack,  print_stack */ 
  6. extern int laby[DIM][DIM];  /* definita in input,h */
  7.  
  8. int move(int i,int j,int *newi,int *newj)
  9.   /* muove i e j alla prox. casella  libera (newi,newj) 
  10.      fallisce se non ci sono caselle libere */
  11.  {  
  12.    if (j+1<DIM && laby[i][j+1])      {*newj=j+1; return 1;}
  13.    else if (i+1<DIM && laby[i+1][j]) {*newi=i+1; return 1;}
  14.    else if (i>0 && laby[i-1][j])     {*newi=i-1; return 1;}
  15.    else if (j>0 && laby[i][j-1])     {*newj=j-1; return 1;}
  16.    return 0;
  17.  }
  18.  
  19. main()
  20. {
  21.  int i,j,continua;
  22.  
  23.  init_stack(&S);
  24.  
  25.  continua=true;
  26.  i=InX;
  27.  j=InY;
  28.  
  29.  while (continua)
  30.   {
  31.    if ( i==OutX && j==OutY ) break;
  32.    if (laby[i][j]) 
  33.        { 
  34.         laby[i][j]=0;
  35.         push(i,j,&S);
  36.         print_stack(&S);
  37.        }
  38.    if (!move(i,j,&i,&j))  /* move ha side effect su i e j */
  39.      if (continua=is_empty_stack(&S)) 
  40.         {
  41.           pop(&S);
  42.           top(&i,&j,&S); 
  43.         }
  44.   }
  45.   if (i==OutX && j==OutY) print_stack(&S);
  46.   else printf("\n No way out!\n"); 
  47. }
  48.  
  49.