STACKMAT.C
资源名称:STACKMAT.rar [点击查看]
上传用户:amwwyq
上传日期:2021-05-31
资源大小:1k
文件大小:2k
源码类别:
手机WAP编程
开发平台:
C/C++
- #include<stdio.h>
- #include<conio.h>
- #include<alloc.h>
- #include<math.h>
- void stackpush(int ,int);
- void stackpop(struct node **);
- struct node
- {
- int x1,y1,xd,yd;
- struct node *link;
- };
- struct node *p=NULL,*r;
- int x,y;
- void main()
- {
- int k=0,c=0,counter=0,i,j,xk,yk;
- clrscr();
- printf("enter co-ordinates of Pawn=");
- scanf("%d %d",&x,&y);
- printf("enter co-ordinates of Knight=");
- scanf("%d %d",&xk,&yk);
- while(y<=8)
- { y++;
- printf("%d ",y);
- stackpush(xk+2,yk+1);
- stackpush(xk+2,yk-1);
- stackpush(xk+1,yk+2);
- stackpush(xk+1,yk-2);
- stackpush(xk-2,yk+1);
- stackpush(xk-2,yk-1);
- stackpush(xk-1,yk+2);
- stackpush(xk-1,yk-2);
- r=p;
- while(r!=NULL)
- { printf("%d %dn",r->x1,r->y1);
- if(r->x1==x && r->y1==y)
- {k=1; }
- r=r->link;
- }
- if(k==1)
- {printf("knight won");
- getch();
- exit(); }
- r=p; c=0; counter=0;
- while(r!=NULL)
- { counter++;
- if(c > r->xd)
- c=r->xd;
- r=r->link;}
- r=p;
- for(i=1;i<=counter;i++)
- {
- if(r->xd!=c)
- stackpop(&r);
- r=r->link;
- }
- p=r; counter=0;
- while(r!=NULL)
- { counter++;
- r=r->link; }
- if(counter==1)
- yk=p->y1;
- if(counter==2)
- {
- if(p->yd > p->link->yd)
- yk=p->y1;
- }
- y++;
- }
- getch();
- }
- void stackpush(int a,int b)
- {
- if(a>=0 && a<=8 && b>=0 && b<=8 )
- {
- r=(struct node *)malloc(sizeof(struct node));
- r->x1=a; r->y1=b; r->xd=abs(x-a); r->yd=abs(y-b);
- if(p==NULL)
- {p=r; p->link=NULL;}
- else
- {r->link=p;
- p=r;} }
- }
- void stackpop(struct node **q)
- {
- struct node *temp;
- temp=*q;
- if(temp==p)
- *q=temp->link;
- else
- {
- while(p->link!=temp)
- p=p->link;
- p->link=temp->link;
- }
- free(temp);
- getch();
- }