qq_31951747 2015-11-17 15:08 采纳率: 100%
浏览 1522
已采纳

数据结构作业!求大神帮忙看看啊!

#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为可走的,从左上角走到右下角~~!

  • 写回答

1条回答 默认 最新

  • Meditator_hkx 2015-11-17 15:40
    关注

    你是在做一个迷宫游戏吗?
    关键是这份作业的目标,你的代码注释,还有你的问题所在你都没给啊?
    大家其实是没有那么多耐心自己一一去为你找出来的~
    所以,你补全再问一下呗

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组