Advertisement
LarvitarYoung

V2_maze

May 23rd, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1. //*((matrix+y*maxColumn)+x)
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<stdbool.h>
  5. typedef struct stack{
  6.     int y,x,dir;
  7.     struct stack *prev;
  8. }stack;
  9. void push(stack **dest,int x,int y, int dir){
  10.     stack *newStack = malloc(sizeof(stack));
  11.     newStack->x = x;
  12.     newStack->y = y;
  13.     newStack->dir = dir;
  14.     newStack->prev = *dest;
  15.     *dest = newStack;
  16. }
  17. void pop(stack **src){
  18.     stack *temp = *src;
  19.     *src= (*src)->prev;
  20.     free(temp);
  21. }
  22. bool squashIf(int x,int y,int dir,int *matrix,int maxColumn,stack *holder){
  23.     if(*((matrix+y*maxColumn)+x)==0){
  24.         holder->x=x;
  25.         holder->y=y;
  26.         holder->dir=dir;
  27.         *((matrix+y*maxColumn)+x)=2;
  28.         return true;
  29.     }return false;
  30. }
  31. bool pathFind(stack *top,int *matrix,int maxRow,int maxColumn,stack *holder){
  32.     int i,x,y;
  33.     for(i=top->dir,x=top->x,y=top->y;i<8;i++){
  34.         switch((i+0)%8){
  35.             case 0:
  36.                 if(y-1>=0){
  37.                     if(squashIf(x,y-1,i,(int *)matrix,maxColumn,holder)){return true;}
  38.                 }break;
  39.             case 1:
  40.                 if(y-1>=0&&x+1<maxColumn){
  41.                     if(squashIf(x+1,y-1,i,(int *)matrix,maxColumn,holder)){return true;}
  42.                     break;
  43.                 }
  44.             case 2:
  45.                 if(x+1<maxColumn){
  46.                     if(squashIf(x+1,y,i,(int *)matrix,maxColumn,holder)){return true;}
  47.                     break;
  48.                 }
  49.             case 3:
  50.                 if(x+1<maxColumn&&y+1<maxRow){
  51.                     if(squashIf((x+1),(y+1),i,(int *)matrix,maxColumn,holder)){return true;}
  52.                     break;
  53.                 }
  54.             case 4:
  55.                 if(y+1<maxRow){
  56.                     if(squashIf(x,y+1,i,(int *)matrix,maxColumn,holder)){return true;}
  57.                     break;
  58.                 }
  59.             case 5:
  60.                 if(x-1>=0&&y+1<maxRow){
  61.                     if(squashIf(x-1,y+1,i,(int *)matrix,maxColumn,holder)){return true;}
  62.                     break;
  63.                 }
  64.             case 6:
  65.                 if(x-1>=0){
  66.                     if(squashIf(x-1,y,i,(int *)matrix,maxColumn,holder)){return true;}
  67.                     break;
  68.                 }
  69.             case 7:
  70.                 if(x-1>=0&&y-1>=0){
  71.                     if(squashIf(x-1,y-1,i,(int *)matrix,maxColumn,holder)){return true;}
  72.                     break;
  73.                 }
  74.         }
  75.     }
  76.     holder->x=top->x;
  77.     holder->y=top->y;
  78.     return false;
  79. }
  80. int main(){
  81.     int x=0,y=0;
  82.     stack *top = malloc(sizeof(stack));
  83.     top=NULL;
  84.     push(&top,x,y,0);
  85.     top->prev=NULL;
  86.     stack *temp=malloc(sizeof(stack));
  87.     int matrix[8][8]={0,1,1,1,1,0,1,0,
  88.                       1,0,0,1,0,1,1,1,
  89.                       0,1,1,0,1,0,1,1,
  90.                       0,1,0,1,1,1,1,1,
  91.                       1,0,1,0,1,1,1,1,
  92.                       0,1,1,1,0,0,0,0,
  93.                       1,1,1,0,0,1,1,0,
  94.                       1,1,1,1,1,0,1,0};
  95.     int dx=7,dy=7;
  96.     matrix[y][x]=2;
  97.     do{
  98.         if(pathFind(top,(int *)matrix,8,8,temp)){
  99.             top->dir=temp->dir;
  100.             push(&top,temp->x,temp->y,0);
  101.         }
  102.         else{
  103.             pop(&top);
  104.             matrix[temp->y][temp->x]=3;
  105.         }
  106.     }while((temp->x!=dx)||(temp->y!=dy));
  107.     for(y=0;y<8;y++){
  108.         for(x=0;x<8;x++){
  109.             printf("%d ",matrix[y][x]);
  110.         }puts("");
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement