#include
#include
#include
int map[20][20];
int book[20][20];
int n;
struct node{
int x;
int y;
int step;
struct node*pre;
struct node*next;
};
struct node queue[401];
struct node head=NULL,*tail=NULL;
struct node *create(struct node*head){
struct node p=(struct node*)malloc(sizeof(struct node));
if(p==NULL) return 0;
head=p;
head->pre=NULL;
head->next=NULL;
tail=head;
return head;
}
void hpush(struct node*head,struct node* a){
struct node*tem=(struct node*)malloc(sizeof(struct node));
if(tem==NULL) return ;
tem->x=a->x;
tem->y=a->y;
tem->step=a->step;
if(head->next!=NULL){
head->next->pre=tem;
tem->next=head->next;
tem->pre=head;
head->next=tem;
}
else {
head->next=tem;
tem->next=NULL;
tail=tem;
}
}
void tpush(struct node*head,struct node a){
struct node*tem=(struct node)malloc(sizeof(struct node));
if(tem==NULL) return ;
tail->next=tem;
tem->x=a->x;
tem->y=a->y;
tem->step=a->step;
tem->pre=tail;
tem->next=NULL;
tail=tem;
}
void hpop(struct node*head){
struct node*tem=head->next;
if(tem==NULL) return ;
head->next=tem->next;
if(tem->next!=NULL)tem->next->pre=head;
free(tem);
}
void tpop(struct node*head){
if(tail==head) return ;
struct node*tem2=tail->pre;
free(tail);
tail=tem2;
tail->next=NULL;
}
void visit(struct node*head){
struct node*tem=head->next;
if(tem==NULL) return ;
while(tem!=NULL){
printf("%d ",tem->x);
tem=tem->next;
}
}
void createmap(int n){
for(int i=0;i
for(int j=0;j
map[i][j]=rand()%2;
}
}
map[0][0]=0,map[n-1][n-1]=0;
for(int i=0;i
for(int j=0;j
printf("%d",map[i][j]);
}
printf("\n");
}
}
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int bfs(){
struct node *tem,*next;
tem=(struct node*)malloc(sizeof(struct node));
tem->x=0,tem->y=0;
book[tem->x][tem->y]=1;
tem->step=0;
tpush(head,tem);
while(head!=tail){
tem=head->next;
hpop(head);
if(tem->x==n-1&&tem->y==n-1)
return tem->step;
for(int i=0;i
next=(struct node*)malloc(sizeof(struct node));
next->x=tem->x;
next->y=tem->y;
next->step=tem->step;
next->x=tem->x+dir[i][0];
next->y=tem->y+dir[i][1];
if(next->xyx==n||next->y==n||book[next->x][next->y]==1||map[next->x][next->y]==1)
{
free(next);
continue;
}
book[next->x][next->y]=1;
tpush(head,next);
}
}
return 0;
}
int main(int argc, char* argv[]){
head=create(head);
memset(map,0,sizeof(map));
scanf("%d",&n);
createmap(n);
int out=bfs();
if(out==0)printf("you are trapped!");
else printf("you can escape in %d minutes",out);
free(head);
return 0;
}
做一個迷宮,迷宮自動生成,1為障礙,0為可走的,從左上角走到右下角~~!
你是在做一個迷宮游戲嗎?
關鍵是這份作業的目標,你的代碼注釋,還有你的問題所在你都沒給啊?
大家其實是沒有那麼多耐心自己一一去為你找出來的~
所以,你補全再問一下呗