|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
最后是通过函数打印出来的 那我想打印在一个txt文件上 fprintf格式的第三个地方写什么进去?
fprintf(fp,"%d","这里写什么 ")
#include<assert.h>
#include<stdio.h>
#define MaxSize 20
#define Row 6
#define Col 6
typedef struct Position
{
int _x;
int _y;
}Position;
typedef Position SDataType;
typedef struct Stack
{
int top;
SDataType _arry[MaxSize];
}Stack;
typedef struct Maze
{
int _map[Row][Col];
}Maze;
void PassMaze(Maze* m, Position enter);
void InitMaze(Maze* m, int map[Row][Col]);
void PrintMaze(Maze* m);
int IsValidEnter(Maze* m, Position enter);//是否为入口
void StackPush(Stack* ps, SDataType data);
void StackInit(Stack* ps);
void StackPop(Stack* ps);
int IsExit(Position pos, Position enter);//是否为出口
int IsPass(Maze* m, Position pos);
int StackEmpty(Stack* ps);
SDataType StackTop(Stack* ps);//返回栈顶元素
void InitMaze(Maze* m, int map[Row][Col])
{
assert(m != NULL);
int i = 0;
int j = 0;
for (i = 0; i < Row; i++)
{
for (j = 0; j < Col; j++)
{
m->_map[i][j] = map[i][j];
}
}
}
void PrintMaze(Maze* m)
{
assert(m != NULL);
int i = 0;
int j = 0;
for (i = 0; i < Row; i++)
{
for (j = 0; j < Col; j++)
{
printf("%d ", m->_map[i][j]);
}
printf("\n");
}
printf("\n");
}
int IsValidEnter(Maze* m, Position enter)
{
assert(m != NULL);
if ((enter._x >= 0 || enter._x < Row) && (enter._y >= 0 || enter._y < Col))
{
return 1;
}
return 0;
}
void StackPush(Stack* ps, SDataType data)
{
assert(ps != NULL);
if (ps->top == MaxSize)
return;
else
{
ps->_arry[ps->top] = data;
ps->top++;
}
}
void StackInit(Stack* ps)
{
assert(ps != NULL);
ps->top = 0;
}
void StackPop(Stack* ps)
{
assert(ps != NULL);
if (ps->top)
ps->top--;
}
int IsExit(Position pos, Position enter)
{
if ((pos._x == 0 || pos._y == Row - 1) || ((pos._y == 0 || pos._y == Col - 1) && (pos._x !=enter._x || pos._y !=enter._y)))
{
return 1;
}
return 0;
}
int IsPass(Maze* m, Position pos)
{
assert(m != NULL);
if (1 == m->_map[pos._x][pos._y])
{
return 1;
}
return 0;
}
int StackEmpty(Stack* ps)
{
assert(ps != NULL);
if (0 == ps->top)
return 1;//栈空
return 0;
}
SDataType StackTop(Stack* ps)
{
assert(ps != NULL);
return ps->_arry[ps->top - 1];
}
void PassMaze(Maze* m, Position enter)
{
assert(m != NULL);
if (!IsValidEnter(m, enter))//如果入口不合法
{
printf("入口不合法!!!\n");
return;
}
else
{
Stack s;
StackInit(&s);
StackPush(&s, enter);
while (!StackEmpty(&s))//当栈不空时
{
Position pos;
Position next;
pos = StackTop(&s);
m->_map[pos._x][pos._y] = 8;//将走过的路标记为2
if (IsExit(pos, enter))//判断是不是出口
return;
else
{
//上
next = pos;
next._x -= 1;
if (IsPass(m, next))//判断是否可以走通,只有为1 时才可以走通
{
StackPush(&s, next);
continue;
}
//左
next = pos;
next._y -= 1;
if (IsPass(m, next))
{
StackPush(&s, next);
continue;
}
//右
next = pos;
next._y += 1;
if (IsPass(m, next))
{
StackPush(&s, next);
continue;
}
//下
next = pos;
next._x += 1;
if (IsPass(m, next))
{
StackPush(&s, next);
continue;
}
m->_map[pos._x][pos._y] = 3;
StackPop(&s);
}
}
}
}
void test()
{
Maze m;
Position enter;
enter._x = 5;
enter._y = 2;
int map[Row][Col] =
{ { 0, 1, 0, 0, 1, 0 },
{ 0, 0, 1, 0, 0, 0 },
{ 0, 0, 1, 1, 1, 0 },
{ 0, 0, 1, 1, 1, 0 },
{ 0, 1, 1, 0, 1, 1 },
{ 0, 0, 1, 0, 0, 0 } };
InitMaze(&m, map);
PrintMaze(&m);
PassMaze(&m, enter);
PrintMaze(&m);
}
int main()
{
test();
FILE*fp;
fp=fopen("maze.txt","w");
fprintf(fp,"%d", );
fclose(fp);
return 0;
}
所以我说你要用循环的方式,上次的代码不是有一个 print(int arr[][M]) 的吗?用同样的方式就好~
|
|