#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> /* printf, scanf, NULL */
#include <stdlib.h> /* malloc, free, rand, system */
#include <stdbool.h>
//坐标点类
struct Point {
int x;
int y;
int c;
};
//自定义队列 建议自己输入大小
struct que
{
int x, y, s, f;//分别存储横、纵、已走过的路程、上一步坐标
}node[250];
//构造坐标点
struct Point buildPoint(int x, int y,int c)/////////
{
struct Point p;
p.x = x;
p.y = y;
p.c = c;
return p;
}
//节点
struct Node {
struct Point data;
struct Node* next;
};
//自定义堆栈
struct stack {
struct Node* head;//头节点,之后采用头插法插入节点
void (*pop)(struct stack* s);//出栈
void (*push)(struct stack* s, struct Point x);//入栈
struct Point(*top)(struct stack* s);//返回栈顶元素
int (*empty)(struct stack* s);//栈是否为空
};
void pop(struct stack* s)//出栈
{
if (!s->head->next)
return;
struct Node* t = s->head->next;//
s->head->next = s->head->next->next;
free(t);
}
void push(struct stack* s, struct Point x)//入栈
{
struct Node* t = (struct Node*)malloc(1);
t->data = x;
t->next = s->head->next;//
s->head->next = t;
}
struct Point top(struct stack* s)//返回栈顶元素
{
return s->head->next->data;
}
int empty(struct stack* s)//栈是否为空
{
return s->head->next ? 0 : 1;
}
//构造堆栈
struct stack buildStack()
{
struct Node* head = (struct Node*)malloc(1);
head->next = NULL;
struct stack s = { head,pop,push,top,empty };
return s;
}
void testStack()
{
struct Point p1 = buildPoint(1, 1,0);
struct Point p2 = buildPoint(1, 1, 0);
struct Point p3 = buildPoint(1, 1, 0);
struct stack s = buildStack();
s.push(&s, p1);
s.push(&s, p2);
s.push(&s, p3);
while (!s.empty(&s))
{
struct Point p = s.top(&s);
s.pop(&s);//
printf("(%d,%d)\n", p.x, p.y);
}
}
int main(void)
{
struct stack s = buildStack();
struct Point p1;
FILE* source;
char ch[250];
int head = 1;
int tail = 1;
int final_x, final_y;
int start_x, start_y;
int tx, ty;//下一步位置
bool flag = false;//用于结束双层循环
printf("Enter the name of file:\n");
scanf("%s", &ch);
if ((source = fopen("ch", "r")) == NULL)//检测文件打开
{
fprintf(stderr, "Can't open file %s", ch);

exit(1);
}
printf("Enter the start of file:\n");
fseek(source, 0L, SEEK_SET);//定位到文件开始位
//读取文件至链表
int n;
int i = 0;
int j = 0;
n = getc(source);
while (n != '\0')
{
if (n == '\n')
{
i++;
j = 0;
}
p1 = buildPoint(i, j,n);
j++;
s.push(&s, p1);
n = getc(source);
}
//得到大小
int size_x, size_y;
size_y = i + 1;
size_x = j + 1;
printf("\tsize_x:%d \tsize_y:%d\n", size_x, size_y);
//得到出口和入口
while (!s.empty(&s))
{
struct Point p = s.top(&s);
s.pop(&s);
if (p.c == 0)
{
if (p.x == 0 || p.y == 0||p.x == size_y || p.y == size_x)
{
final_x = p.x;
final_y = p.y;
break;
}
}
}
printf("Enter the start of file:\n");
scanf("%d %d", &start_x, &start_y);
//释放内存
while (!s.empty(&s))
{
struct Point p = s.top(&s);
s.pop(&s);
}
//存储地图(变长数组)建议自行修改代码输入
int fill[255][255];
//存储使用状态 建议自行修改代码输入
int book[255][255];
//将数据读入二维数组
while (!s.empty(&s))
{
struct Point p = s.top(&s);
s.pop(&s);//
fill[p.x][p.y] == p.c;
}
//初始化使用状态
for (i = 0; i < size_x; i++)
{
for (j = 0; j < size_y; j++)
{
book[i][j] == 0;
}
}
//初始化迷宫入口
book[start_x][start_y] = 1;
node[tail].y = start_y;
node[tail].x = start_x;
node[tail].f = 0;
node[tail].s = 0;
tail++;
while (head < tail)
{
//控制下一步行走方向
for (int ctrl = 0; ctrl < 4; ctrl++)
{
switch (ctrl)
{
case 0:
tx = node[head].x + 1;
ty = node[head].y;
break;
case 1:
tx = node[head].x + 1;
ty = node[head].y;
break;
case 2:
tx = node[head].x + 1;
ty = node[head].y;
break;
case 3:
tx = node[head].x + 1;
ty = node[head].y - 1;
break;
}
//方格可走条件;0、未走过、非终点
if (tx < 0 || tx > size_x - 1 || ty < 0 || ty > size_y - 1)
continue;
if (fill[ty][tx] == 0 && book[ty][tx] == 0)
{
node[tail].s = node[head].s + 1;
node[tail].x = tx;
node[tail].y = tx;
node[tail].f = tx;
book[ty][tx] = 1;
tail++;
}
if (tx == final_x && ty == final_y)
{
flag = true;
break;
}
if (flag)
break;
head++;
}
}
//打印路程和位置坐标
printf("%d\n", node[tail - 1].s);
printf("(%d , %d)\n", final_x,final_y);
for (i = 0; i < node[tail - 1].s; i++)
{
printf("%d", node[tail - 1].s);
}
int temp = node[tail - 1].f;
while (temp != 0)
{
printf("(%d , %d)\n", node[temp].x, node[temp].y);
fill[node[temp].x][node[temp].y] = 2;
temp = node[temp].f;
}
//打印小地图
fill[final_x][final_y] = 2;
int x, y;
for ( x = 0; x < size_x; x++)
{
for (y = 0; y < size_y; y++)
printf("%d\n", fill[x][y]);
}
//关闭文件
fseek(source, 0L, SEEK_SET);
if (fclose(source) != 0)
{
fprintf(stderr, "Can't close file %s", source);
exit(1);
}
return 0;
}
尝试过移动文件的位置,但还是这样