Picnic
2014-06-25 23:38:23

//#define N 2

//#define K 2

//#define C 1

//int friends[C*2]={0,1};

//#define N 4

//#define K 4

//#define C 6

//int friends[C*2]={0,1,1,2,2,3,3,0,0,2,1,3};

#define N 6

#define K 6

#define C 10

int friends[C*2]={0,1,0,2,1,2,1,3,1,4,2,3,2,4,3,4,3,5,4,5};

int parent[K];

int pN=0;

bool isFriendlySelected(){

    for(int i=0;i<K;i+=2){

        bool isFriend=false;

        for(int j=0;j<C*2;j+=2){

            if(friends[j]==parent[i] && friends[j+1]==parent[i+1] ){

                if(i==0 || (i>0&& parent[i-2] <parent[i])){

                    isFriend = true;

                }

                break;

            }

        }

        if(!isFriend){

            return false;

        }

    }

    return true;

}

void permutation(int depth){

    if(depth==K-1){

        if(isFriendlySelected()){

            pN++;

        }

        return;

    }

    for(int i=0;i<N;i++){

        bool goFurther=true;

        for(int j=0;j<=depth;j++){

            if(parent[j]==i){

                goFurther=false;

                break;

            }

        }

        if(goFurther){

            parent[depth+1]=i;

            permutation(depth+1);

        }

    }

}

int main(){

    for(int i=0;i<N;i++){

        parent[0] = i;

        permutation(0);

    }

    printf("%d

",pN);

}

▼ more
Permutation
2014-06-25 22:44:26

#define N 4

#define K 4

int parent[K];

int pN=0;

void permutation(int depth){

    if(depth==K-1){

        for(int i=0;i<K;i++){

            printf("%d ",parent[i]);

        }

        printf("

");

        pN++;

        return;

    }

    for(int i=0;i<N;i++){

        bool goFurther=true;

        for(int j=0;j<=depth;j++){

            if(parent[j]==i){

                goFurther=false;

                break;

            }

        }

        if(goFurther){

            parent[depth+1]=i;

            permutation(depth+1);

        }

    }

}

int main(){

    for(int i=0;i<N;i++){

        parent[0] = i;

        permutation(0);

    }

    printf("%d

",pN);

}

▼ more
Fibonacci memoization
2014-06-25 21:54:45

long a[100];

//fibonacci(i)

long fibonacci(int i){

    if(a[i]!=-1){

        return a[i];

    }

    //a[i] = fibonacci(i-2) + fibonacci(i-1);

    return fibonacci(i-2) + fibonacci(i-1);

//    return a[i];

}

int main(int argc, char** argv){

    //if(argc!=2){

    //    printf("!!USAGE: fibo limit

");

    //    return 1;

    //}

printf("%d

",sizeof(int));

printf("%d

",sizeof(long long));

    

a[0]=1;

    a[1]=1;

    

for(int i=2;i<100;i++){

        a[i]=-1;

    }

    int limit = 100;

    for(int i=0;i<limit;i++){

        if(i>0 && i%10==0){

            printf("

");

        }

        printf("%015ld ",fibonacci(i));

    }

    

printf("

");

}

▼ more
Boggle
2014-06-25 21:50:01

#define N 5

char boggle[N][N+1]={

    {"URLPM"},

    {"XPRET"},

    {"GIAET"},

    {"XTNZY"},

    {"XOQRS"}

};

int wordN = 6;

bool hasWord(int x, int y, int depth, const char* word){

    if(x<0 || x>=N || y <0 || y >= N){

        return false;

    }

    

    bool hasWordValue = boggle[x][y]==word[depth];

    if(!hasWordValue) return hasWordValue;

    if(depth==wordN-1) return hasWordValue;

    if(hasWord(x+1,y+1,depth+1,word)){return true;}

    if(hasWord(x+1,y-1,depth+1,word)){return true;}

    if(hasWord(x-1,y-1,depth+1,word)){return true;}

    if(hasWord(x-1,y+1,depth+1,word)){return true;}

    if(hasWord(x+1,y,depth+1,word)){return true;}

    if(hasWord(x-1,y,depth+1,word)){return true;}

    if(hasWord(x,y+1,depth+1,word)){return true;}

    if(hasWord(x,y-1,depth+1,word)){return true;}

    return false;

}

int main(){

    char word[10]="PRETTY";

    if(hasWord(1,1,0,word)){

        printf("%s exists

",word);

    }

    return 0;

}

▼ more