PanamaCanal C

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

#include <stdio.

h> /*gives the number of buckets being used, since the hash function is perfect als o gives the number of elements*/ int hash_table_size=0; int queue_front = 0; int queue_rear = 0; int queue_size =0; int steps=0; /*STATE STRUCT*/ typedef struct State{ char alphabets[2][6]; struct State *PREV; /*this points to the previous state it came from*/ struct State *NEXT; /*this points to the next state in the linked list*/ int cost; /*Number of moves done to get to this position*/ int zero_index;/*this holds the index to the empty postion*/ char *move[2];/*this holds the move that was done to get to this state*/ } State; /*Allocates memory to struct pointers*/ State * memAllocator(){ struct State *p = (State*) malloc(2*sizeof(State)); if (p==NULL){ printf("Malloc for a new position failed"); exit(1); } return p; } /*STATE AND HASH TABLE Memory Allocation*/ State *queue_array[50000]={NULL}; State *hash_table[100003]={NULL}; State *unique_moves[2]={NULL}; /*initializes the start and end pointer*/ void initializeStartEnd(State *start_state, State *end_state){ start_state->zero_index=15; start_state->PREV = NULL; start_state->alphabets[0][0]='C'; start_state->alphabets[0][1]='A'; start_state->alphabets[0][2]='N'; start_state->alphabets[0][3]='A'; start_state->alphabets[0][4]='M'; start_state->alphabets[0][5]='A'; start_state->alphabets[1][0]='P'; start_state->alphabets[1][1]='A'; start_state->alphabets[1][2]='N'; start_state->alphabets[1][3]='A'; start_state->alphabets[1][4]='L'; start_state->alphabets[1][5]='_'; start_state->move[1]="Beginning State"; start_state->move[2]="_"; end_state->zero_index=15; end_state->alphabets[0][0]='P'; end_state->alphabets[0][1]='A'; end_state->alphabets[0][2]='N'; end_state->alphabets[0][3]='A'; end_state->alphabets[0][4]='M'; end_state->alphabets[0][5]='A';

end_state->alphabets[1][0]='C'; end_state->alphabets[1][1]='A'; end_state->alphabets[1][2]='N'; end_state->alphabets[1][3]='A'; end_state->alphabets[1][4]='L'; end_state->alphabets[1][5]='_'; } /*This method prints the alphabets of the State*/ void printState(State *ptr){ int i=0; for (i=0;i<2;i++){ int j=0; for (j=0;j<6;j++){ /*printf("LOOKING AT %p\n",&(ptr->alphabets[i][j]));*/ printf("%c",ptr->alphabets[i][j]); /*printf("Printing %c\n",ptr->alphabets[i][j]);*/ } printf("\n"); } printf("%3s\n","|"); } /*this method takes two state pointers as input and then returns true or false*/ int checkState(State *ptr1, State *ptr2){ int points=0; int i; for (i=0;i<2;i++){ int j=0; for (j=0;j<6;j++){ if ((ptr1->alphabets[i][j])==(ptr2->alphabets[i][j])){ points++; } } } if (points==12){ return 1; }else { return 0; } } /*This is the hash function*/ int getKey(State *input_state){ int sum=0; int count=0; int i,j,ascii; for(i=0;i<2;i++){ for(j=0;j<6;j++){ ascii = input_state->alphabets[i][j]; sum = sum + ascii * pow(3,count); count=count+1; } } return sum%100003; } /*check if the state is already in the hash_table, this returns either -1, 0 or the index where to insert the new hash_element*/

int hash_contains(State *input_state){ int key; key=0; key = getKey(input_state); /*check if the space at key is Null*/ if (hash_table[key]==NULL){ return 0; }else{ /*now we know that the hash table at key has some data, iterate through it and check the states*/ State *current = hash_table[key]; while (current != NULL){ /*check if the states match and if they do then we know that the state already exists*/ if (checkState(input_state,current)==1){ return 1; } current = current->NEXT; } return key;/*meaning the state is not in the linked list but it already hash a storage space*/ } } /*checks if the new_state is already in the hash and depending on the response 1 ,0 or key does the inserting*/ void hash_insert(State *input_state){ if (hash_table_size==100003){ printf("Hash Table Is Full. No resize implemented. Sorry!"); }else{ /*checking if the state is already in the hash_table*/ int index=hash_contains(input_state); int key=0; key = getKey(input_state); printf("%d\n",key); /*printf("Key: %d Index: %d\n", key,index);*/ if (index==0){ /*meaning that there is null in the hash_table(ke y) and we need to create one*/ hash_table[key]= input_state; hash_table_size++; } else if (index == 1){ printf("This state already is in the hash table, so cann ot be inserted again\n"); } else{ /*I have tested the get_key function and it seems to be a perfect hash*/ printf("Attempting to add state into the linked list\n") ; /*case where key exists but we need to input into the li nked list by manupulating pointers*/ input_state->NEXT = hash_table[key]->NEXT; hash_table[key]->NEXT = input_state; } } } /*This method swaps the empty space in the second line with the character above it to get the new state and returns it if its not cont ained in the hash_table*/

State* swap(int alphabet_first, int alphabet_last, State *current_state, State * new_state){ printf("workin\n"); int empty_index = current_state->zero_index; int empty_first=empty_index/10; int empty_last= empty_index%10; printState(current_state); /*copying all the data in current to new_state */ *new_state=*current_state; /*memcpy(new_state, current_state, sizeof(State));*/ printf("workin1\n"); new_state->PREV=current_state; /*swapping the empty spot with the character to move*/ char temp=NULL; temp = new_state->alphabets[alphabet_first][alphabet_last]; new_state->alphabets[alphabet_first][alphabet_last]='_'; new_state->alphabets[empty_first][empty_last]=temp; new_state->zero_index= alphabet_first*10+alphabet_last; printf("Working2\n"); /*checking if our new state is already in the hash table 1 meaning that it is*/ if (hash_contains(new_state)!= 1){ steps++; return new_state; } return NULL; } /*helps with the randomize method with figuring out east or west moves and then inserts the moves into the to return array*/ void checkOthers(int empty_index, State *current_state){ State *new_state1=memAllocator(); printf("Memory allocation succesfful\n"); if ((empty_index%10)==5){ /*Go West*/ /*printf("Step %d, move %c east\n",current_state->alphabets[empt y_index/10][(empty_index%10)-1]);*/ printf("If exception below then problem in swap1\n"); new_state1=swap(empty_index/10, (empty_index%10)-1,current_state , new_state1); printf("If this prints problem not in swap\n"); if (new_state1 != NULL){ printf("trying east\n"); new_state1->move[1]="East"; new_state1->move[2]=current_state->alphabets[empty_index /10][(empty_index%10)-1]; printf("Attempting to insert 1a:\n"); printState(new_state1); unique_moves[1]=new_state1; } /*Go East*/ } else if (empty_index%10==0){ /*printf("Step %d, move %c west\n",current_state->alphabets[empt y_index/10][(empty_index%10)+1]);*/ printf("If exception below then problem in swap2\n"); new_state1=swap(empty_index/10, (empty_index%10+1),current_state , new_state1);

printf("If this prints problem not in swap\n"); if (new_state1 != NULL){ printf("trying west\n"); new_state1->move[1]="West"; new_state1->move[2]=current_state->alphabets[empty_index /10][(empty_index%10)+1]; unique_moves[1]=new_state1; } } else { /* Do both east and west*/ /*Go West*/ /*printf("Step %d, move %c east\n",current_state->alphabets[empt y_index/10][(empty_index%10)-1]);*/ printf("If exception below then problem in swap3\n"); new_state1 = swap(empty_index/10, (empty_index%10)-1,current_sta te, new_state1); printf("If this prints problem not in swap\n"); if (new_state1 != NULL){ printf("Moving east working\n"); new_state1->move[1]="East"; new_state1->move[2]=current_state->alphabets[empty_index /10][(empty_index%10)-1]; unique_moves[1]=new_state1; return; } /*Go East*/ /*printf("Step %d, move %c west\n",current_state->alphabets[empt y_index/10][(empty_index%10)+1]);*/ printf("If exception below then problem in swap4\n"); new_state1 = swap(empty_index/10, (empty_index%10+1),current_sta te, new_state1); printState(new_state1); printf("If this prints problem not in swap\n"); if (new_state1 != NULL){ printf("moving west working\n"); new_state1->move[1]="West"; new_state1->move[2]=current_state->alphabets[empty_index /10][(empty_index%10)+1]; unique_moves[1]=new_state1; } } } /*This method is going to insert the two unique random moves into the moves arra y*/ void randomizeState(State *current_state){ int empty_index= current_state->zero_index; State *new_state=memAllocator(); if ((empty_index/10)==1){ /*printf("Step %d, move %c south\n",current_state->alphabets[0][ (empty_index%10)]);*/ /*Moving the character south*/ new_state = swap((empty_index/10 -1), empty_index%10,current_sta te,new_state); if (new_state != NULL){ printf("Moving south working\n"); new_state->move[1]="South"; new_state->move[2]=current_state->alphabets[0][empty_ind ex%10]; unique_moves[0]=new_state;

} checkOthers(empty_index, current_state); } else if ((empty_index/10)==0){ /*printf("Step %d, move %c north\n",current_state->alphabets[1][ (empty_index%10)]);*/ /*Moving the character north*/ new_state = swap((empty_index/10 +1), empty_index%10,current_sta te,new_state); if (new_state != NULL){ printf("Moving north working\n"); new_state->move[1]="North"; new_state->move[2]=current_state->alphabets[1][(empty_in dex%10)]; unique_moves[0]=new_state; } checkOthers(empty_index, current_state); } } /*inserts a pointer to the queue*/ void enqueue(State *input_state){ if (queue_size >= 500000){ printf("Queue Overflow\n"); }else{ queue_array[queue_rear]= input_state; queue_rear++; if (queue_rear==500000){ queue_rear = 0; } queue_size++; } } /*prints the queue*/ void printQueue(){ if (queue_size==0){ printf("Queue is empty\n"); }else{ printf("This is the current queue\n"); int i=0; for (i =queue_front; i<=queue_rear-1; i++){ printState(queue_array[i]); } printf("\n"); } } /*this deletes the oldest state in the queue*/ State * dequeue(){ if (queue_size==0){ printf("The queue is empty\n"); }else{ State *temp; temp = queue_array[queue_front]; queue_front++; if (queue_front == 50000){ queue_front=0; } queue_size--; return temp;

} } void printStatistics(){ printf("Number of items in the hash table when the goal position was fou nd: %d\n", hash_table_size); printf("Number of empty hash table buckets at the end: %d\n", 100003-has h_table_size); printf("Number of hash table buckets with only one item in them at the e nd: %d\n",hash_table_size); printf("Maximum number of items in a largest hash table bucket at the en d: 1"); } int main(){ State *start_state=memAllocator(); State *end_state=memAllocator(); State *dummy_check =memAllocator(); initializeStartEnd(start_state,end_state); *dummy_check=*start_state; State *current_state; current_state=start_state; /*hash_insert(start_state); hash_insert(end_state); printf("This is the end state: \n"); printState(end_state); randomizeState(end_state); printf("These are the randomized states: \n"); printState(unique_moves[0]); printState(unique_moves[1]);*/ hash_insert(start_state); enqueue(start_state); while (checkState(current_state,end_state)!= 1){ current_state=dequeue(); printf("This is the current state thats being looked over now: \ n"); printState(current_state); randomizeState(current_state); printf("Randomize State is working: \n"); int i; for (i=0;i<2;i++){ if (unique_moves[i] != NULL){ printf("Unique Move at %d :\n",i); printState(unique_moves[i]); if (checkState(unique_moves[i],dummy_check)==1){ exit(1); } hash_insert(unique_moves[i]); enqueue(unique_moves[i]); printf("The current queue\n"); printQueue(); } } unique_moves[0]=NULL; unique_moves[1]=NULL; printf("unique moves have been nulled and now its time for deque ueing other states Current queue size: \n"); printf("%d\n",queue_size); } printf("%d\n",steps);

printf("This line is executed again\n"); printQueue(); return 0; }

You might also like