留下空白 注释 日后加
#include<stdio.h>
#include<stdlib.h>
int a[10][10]={
{0,0,1,0},
{1,1,1,1},
{0,0,1,0},
{0,1,0,0},
{0,0,0,1}
};
int book[10][10];
typedef struct Node
{
int col;
int row;
int step;
struct Node* next;
}Node;
typedef struct Node Element;
typedef struct Queue
{
Node *front;
Node *tail;
} LinkQueue;//Node *front是因为front tail只是指示队列头尾的指针而已 而非实际节点
LinkQueue * init()
{
LinkQueue* Q=(LinkQueue*)malloc(sizeof(Q));
Q->front=Q->tail=NULL;
return Q;
}
Node *Dequeue(LinkQueue *Q)
{
Node *p =Q->front;
if(p==NULL)
{
printf("nothing\n");
return NULL;
}
if(Q->front!=Q->tail)
{
Q->front=p->next;
}
else
{
Q->front=Q->tail=NULL;
}
return p;
}
void Enqueue(LinkQueue *Q,Element *e)
{
if(Q->tail==NULL)
{
Q->front = Q->tail=e;
}
else
{
Q->tail->next=e;
Q->tail=e;
//printf("second.... in\n");
}
}
int main()
{
int start_x,start_y;
int tx,ty;
int k;
int next[4][2]={{0,1},
{1,0},
{0,-1},
{-1,0}};
int flag=0;
Node *p;
LinkQueue* Q=init();
scanf("%d%d",&start_x,&start_y);//0 0
//printf("%d %d",start_x,start_y);
int endX=3;
int endY=2;
Node* e=(Node*)malloc(sizeof(Node));
e->col=start_x;
e->row=start_y;
e->step=0;
book[start_x][start_y]=1;
Enqueue(Q,e);
while(Q->tail!=NULL)//队列非空
{
for(k=0;k<4;k++)//寻找下一个点
{
tx=Q->front->col+next[k][0];
ty=Q->front->row+next[k][1];
if(tx>=5||tx<0||ty>=4||ty<0)
continue;
if(a[tx][ty]==0&&book[tx][ty]==0)
{
Node* node=(Node*)malloc(sizeof(Node));
node->col=tx;
node->row=ty;
node->step=Q->front->step+1;
book[tx][ty]=1;
Enqueue(Q,node);
}
if(tx==endX&&ty==endY)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("success\n");
break;
}
else
{
p=Dequeue(Q);
printf("[************%d\n",p->step);
}
}
if(Q->tail==NULL&&flag==0)
{
printf("不能去哪里");
}
else
printf("%d step",Q->tail->step);
return 0;
}