Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*((matrix+y*maxColumn)+x)
- #include<stdio.h>
- #include<stdlib.h>
- #include<stdbool.h>
- typedef struct stack{
- int y,x,dir;
- struct stack *prev;
- }stack;
- void push(stack **dest,int x,int y, int dir){
- stack *newStack = malloc(sizeof(stack));
- newStack->x = x;
- newStack->y = y;
- newStack->dir = dir;
- newStack->prev = *dest;
- *dest = newStack;
- }
- void pop(stack **src){
- stack *temp = *src;
- *src= (*src)->prev;
- free(temp);
- }
- bool squashIf(int x,int y,int dir,int *matrix,int maxColumn,stack *holder){
- if(*((matrix+y*maxColumn)+x)==0){
- holder->x=x;
- holder->y=y;
- holder->dir=dir;
- *((matrix+y*maxColumn)+x)=2;
- return true;
- }return false;
- }
- bool pathFind(stack *top,int *matrix,int maxRow,int maxColumn,stack *holder){
- int i,x,y;
- for(i=top->dir,x=top->x,y=top->y;i<8;i++){
- switch((i+0)%8){
- case 0:
- if(y-1>=0){
- if(squashIf(x,y-1,i,(int *)matrix,maxColumn,holder)){return true;}
- }break;
- case 1:
- if(y-1>=0&&x+1<maxColumn){
- if(squashIf(x+1,y-1,i,(int *)matrix,maxColumn,holder)){return true;}
- break;
- }
- case 2:
- if(x+1<maxColumn){
- if(squashIf(x+1,y,i,(int *)matrix,maxColumn,holder)){return true;}
- break;
- }
- case 3:
- if(x+1<maxColumn&&y+1<maxRow){
- if(squashIf((x+1),(y+1),i,(int *)matrix,maxColumn,holder)){return true;}
- break;
- }
- case 4:
- if(y+1<maxRow){
- if(squashIf(x,y+1,i,(int *)matrix,maxColumn,holder)){return true;}
- break;
- }
- case 5:
- if(x-1>=0&&y+1<maxRow){
- if(squashIf(x-1,y+1,i,(int *)matrix,maxColumn,holder)){return true;}
- break;
- }
- case 6:
- if(x-1>=0){
- if(squashIf(x-1,y,i,(int *)matrix,maxColumn,holder)){return true;}
- break;
- }
- case 7:
- if(x-1>=0&&y-1>=0){
- if(squashIf(x-1,y-1,i,(int *)matrix,maxColumn,holder)){return true;}
- break;
- }
- }
- }
- holder->x=top->x;
- holder->y=top->y;
- return false;
- }
- int main(){
- int x=0,y=0;
- stack *top = malloc(sizeof(stack));
- top=NULL;
- push(&top,x,y,0);
- top->prev=NULL;
- stack *temp=malloc(sizeof(stack));
- int matrix[8][8]={0,1,1,1,1,0,1,0,
- 1,0,0,1,0,1,1,1,
- 0,1,1,0,1,0,1,1,
- 0,1,0,1,1,1,1,1,
- 1,0,1,0,1,1,1,1,
- 0,1,1,1,0,0,0,0,
- 1,1,1,0,0,1,1,0,
- 1,1,1,1,1,0,1,0};
- int dx=7,dy=7;
- matrix[y][x]=2;
- do{
- if(pathFind(top,(int *)matrix,8,8,temp)){
- top->dir=temp->dir;
- push(&top,temp->x,temp->y,0);
- }
- else{
- pop(&top);
- matrix[temp->y][temp->x]=3;
- }
- }while((temp->x!=dx)||(temp->y!=dy));
- for(y=0;y<8;y++){
- for(x=0;x<8;x++){
- printf("%d ",matrix[y][x]);
- }puts("");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement