c 迷宫程序

时间 : 14-06-14 栏目 : linux编程 作者 : 老薛 评论 : 0 点击 : 1,003 次

#include <stdio.h>

struct node{
int x;
int y;
};

struct stack{
struct node arr[100];
int top;
int base;
};

int pop( struct stack *psk, struct node *pitem){
if(psk->top==psk->base)
return -1;
*pitem=psk->arr[--psk->top];
return 0;

}

int push(struct stack *psk,struct node item){
if(psk->top==99)
return -1;
psk->arr[psk->top]=item;
psk->top++;
return 0;
}

int main()
{
//map start
int map[10][10]={{1,1,1,1,1,1,1,1,1,1},
{1,0,0,2,0,0,0,2,0,1},
{1,2,0,2,0,0,0,2,0,1},
{1,0,0,0,0,2,2,0,0,1},
{1,0,2,2,2,0,0,0,0,1},
{1,0,0,0,2,0,0,0,0,1},
{1,0,2,0,0,0,2,0,0,1},
{1,0,2,2,2,0,2,2,0,1},
{1,2,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
};
//print 1
int p1,p2;
for(p1=0;p1<10;p1++)
{
for(p2=0;p2<10;p2++)
{
printf("%2d",map[p1][p2]);
}
printf("\n");
}
//
int i=1,j=1;
struct stack sk;
struct node item;
sk.top=0;
sk.base=0;
//start
while(1)
{
if(map[i][j]==0)
{
if(map[i][j+1]==0)
{
item.x=i;
item.y=j;
if(0>push(&sk,item))
return -1;
map[i][j]=3;
j++;

}else if(map[i+1][j]==0)
{
item.x=i;
item.y=j;
if(0>push(&sk,item))
return -1;
map[i][j]=3;
i++;
}else if(map[i][j-1]==0)
{
item.x=i;
item.y=j;
if(0>push(&sk,item))
return -1;
map[i][j]=3;
j--;
}else if(map[i-1][j]==0)
{
item.x=i;
item.y=j;
if(0>push(&sk,item))
return -1;
map[i][j]=3;
i--;
}else{
map[i][j]=3;
}
}else
{
if(map[i][j+1]==0)
{
item.x=i;
item.y=j;
if(0>push(&sk,item))
return -1;
map[i][j]=3;
j++;
}else if(map[i+1][j]==0)
{
item.x=i;
item.y=j;
if(0>push(&sk,item))
return -1;
map[i][j]=3;
i++;
}else if(map[i][j-1]==0)
{
item.x=i;
item.y=j;
if(0>push(&sk,item))
return -1;
map[i][j]=3;
j--;
}else if(map[i-1][j]==0)
{
item.x=i;
item.y=j;
if(0>push(&sk,item))
return -1;
map[i][j]=3;
i--;
}else
{
if(0>pop(&sk, &item))
{
printf("Reach base!\n");
return -1;
}
i=item.x;
j=item.y;
}
}
if(i==8 && j==8)
{
printf("map[%d][%d]\n",i,j);
int n;
for(n=0;n<100;n++)
{
if(0>pop(&sk, &item))
{
printf("END----Reach base!\n");
return -1;
}
printf("map[%d][%d]\n", item.x,item.y);
}
}
continue;
}

//end
return 0;
}

除非注明,文章均为( 老薛 )原创,转载请保留链接: http://www.bdkyr.com/xtyw003/47.html

c 迷宫程序:等您坐沙发呢!

发表评论

1 + 6 = ?


博主微信号,很高兴为您提供帮助

随便看看

0