69,368
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define m 6
#define n 8
int maze[m+2][n+2];
typedef struct {
int x,y;
}item;
item move[8]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
typedef struct{
int x,y,d;
}datatype;
datatype temp;
#define FALSE 0
#define TRUE 1
typedef struct node{
int a,b,c;
struct node *next;
}SeqStack;
SeqStack * Init_SeqStack()
{
SeqStack *top;
top->next=NULL;
return top;
}
int Push_SeqStack(SeqStack *top,datatype temp)
{
SeqStack *p;
p=(SeqStack *)malloc(sizeof(SeqStack));
if(p==NULL)
return FALSE;
p->a=temp.x;
p->b=temp.y;
p->c=temp.d;
p->next=top->next;
top->next=p;
return TRUE;
}
int IsEmpty(SeqStack *top)
{
if(top->next==NULL)
return TRUE;
return FALSE;
}
int Pop_SeqStack(SeqStack *top,datatype *s)
{
SeqStack *p;
if(IsEmpty(top))
return FALSE;
p=top->next;
s->x=p->a;
s->y=p->b;
s->d=p->c;
top->next=p->next;
free(p);
return TRUE;
}
//迷宫
int path(int maze[m][n],item move[8])
{
SeqStack *s;
int x,y,d,i,j;
s=Init_SeqStack();
temp.x=1;
temp.y=1;
temp.d=-1;
Push_SeqStack(s,temp);
while(!Empty_SeqStack(s))
{
Pop_SeqStack(s,&temp);
x=temp.x;
y=temp.y;
d=temp.d+1;
while(d<8)
{
i=x+move[d].x;
j=y+move[d].y;
if(maze[i][j]=0)
{
temp={x,y,d};
Push_SeqStack(s,temp);
x=i;
y=j;
maze[x][y]=-1;
if(x==m && y==n)
return 1;
else
d=0;
}
else
d++;
}
}
return 0;
}
int main()
{
int i,j,f;
for(i=0;i<8;i++)
for(j=0;j<10;j++)
maze[i][j]=1;
maze[1][1]=0;
maze[2][2]=0;
maze[3][3]=0;
maze[3][4]=0;
maze[3][5]=0;
maze[3][6]=0;
maze[3][7]=0;
maze[4][5]=0;
maze[5][6]=0;
maze[5][7]=0;
maze[5][8]=0;
maze[6][8]=0;
f=path(maze[m+2][n+2],move[]);
if(1==f)
printf("迷宫有出路");
else
printf("迷宫没出路");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
//
//实现魔塔地图读取器 ,地图编辑器 编辑好地图后存入txt文件 本程序实现读取
//
bool write_map(char *filename,char (*pos)[14])
{
FILE * fd;
int i, j ;
fd = fopen(filename,"w");
for(i = 0 ; i < 14 ; i++)
for(j = 0; j < 14 ;j++)
fprintf(fd,"%d ",pos[i][j]);//必须有空格 要不读的时候会出问题
fclose(fd);
return true;
}
bool read_map(char *filename,char (*pos)[14])
{
FILE * fd;
int i, j ;
fd = fopen(filename,"r");
for(i = 0 ; i < 14 ; i++)
for(j = 0; j < 14 ;j++){
fscanf(fd,"%d",&pos[i][j]);
}
fclose(fd);
return true;
}
int main()
{
int i ;
int j ;
int8_t pos[14][14] ;
int8_t pos1[14][14] ;
read_map("E:\\write.txt",pos1);
for(i = 0 ; i < 14 ;i++)
for(j = 0 ; j < 14 ;j++){
printf("%d\n",pos1[i][j]);
}
// write_map("E:\\wtite.txt",pos1);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define m 6
#define n 8
int maze[m+2][n+2];
typedef struct {
int x,y;
}item;
item move[8]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
typedef struct{
int x,y,d;
}datatype;
datatype temp;
#define FALSE 0
#define TRUE 1
typedef struct node{
int a,b,c;
struct node *next;
}SeqStack;
SeqStack * Init_SeqStack()
{
SeqStack *top;
top->next=NULL;
return top;
}
int Push_SeqStack(SeqStack *top,datatype temp)
{
SeqStack *p;
p=(SeqStack *)malloc(sizeof(SeqStack));
if(p==NULL)
return FALSE;
p->a=temp.x;
p->b=temp.y;
p->c=temp.d;
p->next=top->next;
top->next=p;
return TRUE;
}
int IsEmpty(SeqStack *top)
{
if(top->next==NULL)
return TRUE;
return FALSE;
}
int Pop_SeqStack(SeqStack *top,datatype *s)
{
SeqStack *p;
if(IsEmpty(top))
return FALSE;
p=top->next;
s->x=p->a;
s->y=p->b;
s->d=p->c;
top->next=p->next;
free(p);
return TRUE;
}
int path(int maze[m][n],item move[8])
{
SeqStack *s;
int x,y,d,i,j;
s=Init_SeqStack();
temp.x=1;
temp.y=1;
temp.d=-1;
Push_SeqStack(s,temp);
while(!IsEmpty(s))
{
Pop_SeqStack(s,&temp);
x=temp.x;
y=temp.y;
d=temp.d+1;
while(d<8)
{
i=x+move[d].x;
j=y+move[d].y;
if(maze[i][j]==0)
{
temp.x=x;
temp.y=y;
temp.d=d;
Push_SeqStack(s,temp);
x=i;
y=j;
maze[x][y]=-1;
if(x==m && y==n)
return 1;
else
d=0;
}
else
d++;
}
}
return 0;
}
int main()
{
int i,j,f;
for(i=0;i<8;i++)
for(j=0;j<10;j++)
maze[i][j]=1;
maze[1][1]=0;
maze[2][2]=0;
maze[3][3]=0;
maze[3][4]=0;
maze[3][5]=0;
maze[3][6]=0;
maze[3][7]=0;
maze[4][5]=0;
maze[5][6]=0;
maze[5][7]=0;
maze[5][8]=0;
maze[6][8]=0;
f=path(maze,move);
if(1==f)
printf("迷宫有出路");
else
printf("迷宫没出路");
return 0;
}