Skip to content

Instantly share code, notes, and snippets.

@Heliosmaster
Created November 25, 2012 19:11
Show Gist options
  • Save Heliosmaster/4144833 to your computer and use it in GitHub Desktop.
Save Heliosmaster/4144833 to your computer and use it in GitHub Desktop.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
extern double drand48();
int Z;
int M;
int sizeLattice;
int N;
double SumR2;
int main(int argc, char* argv[])
{
M = (argc >1) ? atoi(argv[1]) : 2;
int seed;
seed=time(NULL);
srand48(seed);
for(N=1;N<=M;N++){
walk();
}
}
void printlattice(int* lattice){
int i,j;
for(i=0;i<2*M+1;i++){
for(j=0;j<2*M+1;j++) printf("%d ",lattice[i*(2*M+1)+j]);
printf("\n");
}
}
walk(){
Z = 0.0;
SumR2 = 0.0;
int start, end;
FILE *file1;
start = time(NULL);
int StepX[N];
int StepY[N];
int i;
sizeLattice = (2*M+1)*(2*M+1);
int* lattice = malloc(sizeLattice*sizeof(int));
for(i=0;i<sizeLattice;i++) lattice[i] = 0;
lattice[M*(2*M+1)+M]=1;
printlattice(lattice);
StepX[0] = 0;
StepY[0] = 0;
//printlattice();
go(1, 0, 1, StepX, StepY,lattice);
end = time(NULL);
file1 = fopen("polymers.txt","a+");
printf("%i\t%u\t\t%f\t%i\n",N, 4*Z, SumR2/Z, end-start);
fprintf(file1,"%i\t%u\t\t%f\t%i\n",N, 4*Z, SumR2/Z, end-start);
fclose(file1);
}
go(int x, int y, int n, int *PrevX, int *PrevY, int* lattice)
{
int posX = M+x;
int posY = M+y;
printf("%d,%d\n",posX,posY);
lattice[posX*(2*M+1)+posY] = 1;
printlattice(lattice);
int StepX[N];
int StepY[N];
int i;
for(i=0;i<n;i++){
StepX[i] = PrevX[i];
StepY[i] = PrevY[i];
}
/*
for(i=n-1;i>=0;i--){
if(StepX[i]==x &&StepY[i]==y){
return;
}
}
*/
StepX[n] = x;
StepY[n] = y;
for(i=0;i<n;i++) printf("(%d,%d)-",StepX[i],StepY[i]);
printf("\n");
if(n==N){
Z++;
SumR2 += x*x + y*y;
// lattice[(M+x)*(2*M+1)+M+y]=0;
/*
int i;
//printf("\n step: ");
//for(i=0;i<=N;i++)
for(i=0;i<n-1;i++) {
//printf("(%d,%d) - ",StepX[N-i],StepY[N-i]);
lattice[M+StepX[N-i]][M+StepY[N-i]]=0;
}
printf("\n");
*/
}
else{
int before = 0;
for(i=n-1;i>=0;i--){
if(StepX[i]==x &&StepY[i]==y+1){
before = 1;
break;
}
}
if (before == 0) go(x, y+1, n+1, StepX, StepY,lattice);
before = 0;
for(i=n-1;i>=0;i--){
if(StepX[i]==x &&StepY[i]==y-1){
before = 1;
break;
}
}
if (before == 0) go(x, y-1, n+1, StepX, StepY,lattice);
before = 0;
for(i=n-1;i>=0;i--){
if(StepX[i]==x+1 &&StepY[i]==y){
before = 1;
break;
}
}
if (before == 0) go(x+1, y, n+1, StepX, StepY,lattice);
before = 0;
for(i=n-1;i>=0;i--){
if(StepX[i]==x-1 &&StepY[i]==y){
before = 1;
break;
}
}
if (before == 0) go(x-1, y, n+1, StepX, StepY,lattice);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment