pietrod21

formica di langton

Dec 25th, 2011
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #define L 300
  3. #define t_max 11000
  4.  
  5. int main(){
  6.     int c[L][L];
  7.     int ant[L][L];
  8.     int ant1[L][L];
  9.     int i, j, n, somm, t;
  10.     FILE *fp, *fd;
  11.     fp=fopen("ant.dat","w");
  12.     fd=fopen("ant.gnu","w");
  13.     fprintf(fd, "splot 'ant.dat' \n");
  14.     fclose(fd);
  15.    
  16.     for(i=0;i<L;i++){       //azzero array ant e ant1 e metto tutta bianca la scacchiera
  17.         for(j=0;j<L;j++){
  18.             c[i][j]=0;
  19.             ant[i][j]=0;
  20.             ant1[i][j]=0;
  21.         }
  22.     }
  23.    
  24.     ant1[L/2][L/2]=1; //creo una ant al centro della scacchiera
  25.     ant[L/2][L/2-1]=1;
  26.    
  27.     for(t=0;t<t_max;t++){
  28.        
  29.         for(i=1;i<L;i++){      
  30.             for(j=1;j<L;j++){
  31.                 if(ant1[i][j]==1){
  32.                     if(c[i][j]==1){
  33.                         if(ant[i-1][j]==1){ant1[i][j+1]=1;ant1[i][j]=0;ant[i][j]=1;ant[i-1][j]=0;c[i][j]=0;}
  34.                         if(ant[i][j-1]==1){ant1[i-1][j]=1;ant1[i][j]=0;ant[i][j]=1;ant[i][j-1]=0;c[i][j]=0;}
  35.                         if(ant[i+1][j]==1){ant1[i][j-1]=1;ant1[i][j]=0;ant[i][j]=1;ant[i+1][j]=0;c[i][j]=0;}
  36.                         if(ant[i][j+1]==1){ant1[i+1][j]=1;ant1[i][j]=0;ant[i][j]=1;ant[i][j+1]=0;c[i][j]=0;}
  37.                         fprintf(fp,"%d %d %d\n",i,j,t);
  38.                     }
  39.                    
  40.                     else if(c[i][j]==0){
  41.                         if(ant[i-1][j]==1){ant1[i][j-1]=1;ant1[i][j]=0;ant[i][j]=1;ant[i-1][j]=0;c[i][j]=1;}
  42.                         if(ant[i][j-1]==1){ant1[i+1][j]=1;ant1[i][j]=0;ant[i][j]=1;ant[i][j-1]=0;c[i][j]=1;}
  43.                         if(ant[i+1][j]==1){ant1[i][j+1]=1;ant1[i][j]=0;ant[i][j]=1;ant[i+1][j]=0;c[i][j]=1;}
  44.                         if(ant[i][j+1]==1){ant1[i-1][j]=1;ant1[i][j]=0;ant[i][j]=1;ant[i][j+1]=0;c[i][j]=1;}
  45.                         fprintf(fp,"%d %d %d\n",i,j,t);
  46.                     }
  47.                 }                      
  48.             }
  49.         }
  50.     }
  51.     fclose(fp);
  52.     system("gnuplot ant.gnu");
  53.    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment