Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6.   int matrix[3][3];
  7.   int distance_depth;
  8.   struct node *next;
  9. }node ;
  10.  
  11. node *glob;
  12.  
  13. int CountDist(int matrix[3][3]){
  14. int distance,i,j,help,a,b;
  15.  
  16. distance=0;
  17. for (i=0;i<3;i++)
  18.     for (j=0;j<3;j++){
  19.             if (matrix[i][j] != 9){
  20.         help = matrix[i][j];
  21.         b = ((help-1) % 3);
  22.         a = ((help-1) / 3);
  23.         distance += abs(a-i) + abs(b-j);
  24.        // printf ("I: %d, J: %d, A: %d, B: %d, distance: %d \n",i,j,a,b,distance);
  25.     }
  26.     }
  27. return distance;
  28. }
  29.  
  30. void add_to_sorted(int matrix[3][3],int depth){
  31. int i,j,value=0;
  32.     node *curr=NULL,*next,*newnode=(node*)malloc(sizeof(node));
  33.  
  34.     for (i=0;i<3;i++)
  35.         for (j=0;j<3;j++)
  36.             newnode -> matrix[i][j] = matrix[i][j];
  37.  
  38.     newnode->distance_depth = CountDist(matrix)+depth;
  39.  
  40.     newnode->next= NULL;
  41.     if (glob == NULL){
  42.         glob = newnode;
  43.     }
  44.     else{
  45.             curr=glob;
  46.             next = glob->next;
  47.       if (curr->distance_depth >= newnode->distance_depth){
  48.             newnode->next = curr;
  49.             glob = newnode;
  50.             return;
  51.       }/*
  52.         while (curr -> next != NULL && value==0){
  53.             if ((curr->distance_depth<= newnode->distance_depth) && (next->distance_depth >= newnode->distance_depth)){
  54.                 curr->next = newnode;
  55.                 newnode->next=next;
  56.                 value = 1;
  57.                 break;
  58.             }
  59.  
  60.             next=next->next;
  61.             curr = curr->next;
  62.         }
  63.             if (value==0){
  64.             curr->next=newnode;
  65.             newnode->next=next;
  66.             }*/
  67.         }
  68. }
  69.  
  70. }
  71. int main(){
  72.  
  73. node *curr,*next;
  74. glob = NULL;
  75. glob_history = NULL;
  76. int newarray[3][3],value=0,i,j,array[3][3] = {{1,2,3},
  77.                                               {4,9,5},
  78.                                               {7,8,6}};//4+0
  79. int goal[3][3] = {{1,2,3},
  80.                   {4,5,6},
  81.                   {7,8,9}};//0+1
  82. int array2[3][3] = {{1,2,4},
  83.                                               {4,9,5},
  84.                                               {7,8,6}};//7+0
  85.  
  86. int array3[3][3] = {{1,2,8},
  87.                                               {4,9,5},
  88.                                               {7,8,6}};//7+1
  89. curr = glob;
  90.   add_to_sorted(goal,1); //1
  91.   add_to_sorted(array3,1);//6
  92.   //add_to_sorted(array2,0);
  93.  // add_to_sorted(array2,4);
  94.  
  95.     while(curr != NULL){
  96.         for (i=0;i<3;i++){
  97.                 printf("\n");
  98.             for (j=0;j<3;j++)
  99.                 printf("%d ", curr->matrix[i][j]);
  100.         }
  101.         printf(" x ");
  102.     curr = curr->next;
  103.  
  104.     }
  105.  
  106. return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement