社区
C语言
帖子详情
求解迷宫问题
raindream
2003-08-19 06:19:55
那位大侠能不能帮忙写一个求解迷宫的程序,小弟急需,多谢多谢了!!!!
今天编了一天没有编出来,很郁闷,小弟刚开始学,请帮忙大侠们!!!谢了!!谢了!!
...全文
57
10
打赏
收藏
求解迷宫问题
那位大侠能不能帮忙写一个求解迷宫的程序,小弟急需,多谢多谢了!!!! 今天编了一天没有编出来,很郁闷,小弟刚开始学,请帮忙大侠们!!!谢了!!谢了!!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
10 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
raindream
2003-08-26
打赏
举报
回复
感谢大家的帮助,小弟终于用两个栈完成了迷宫的寻找,非常感谢!!!!
alemjann
2003-08-21
打赏
举报
回复
#include <stdio.h>
typedef struct hero {int x,y,face;} HERO;
void set_hero(HERO* h,int x,int y,int face){h->x=x;h->y=y;h->face=face;}
void go(HERO* h){if(h->face%2) h->x+=2-h->face;else h->y+=h->face-1;}
void goleft(HERO* h){if(h->face%2) h->y+=h->face-2;else h->x+=h->face-1;}
void turnleft(HERO* h){h->face=(h->face+3)%4;}
void turnright(HERO* h){h->face=(h->face+1)%4;}
void print_hero(HERO* h, int b)
{
gotoxy(h->x + 1, h->y + 1);
if (b)
{
switch (h->face)
{
case 0: printf("%c", 24); break;
case 1: printf("%c", 16); break;
case 2: printf("%c", 25); break;
case 3: printf("%c", 27); break;
default: break;
}
}
else printf(" ");
}
int maze[10][10] =
{
0, 0, 0, 1, 0, 0, 0, 1, 0, 0,
1, 0, 1, 1, 0, 1, 1, 1, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 1, 0, 1, 1, 1,
0, 0, 1, 0, 1, 1, 0, 0, 0, 1,
1, 0, 1, 0, 1, 1, 0, 1, 0, 1,
0, 0, 1, 0, 1, 1, 0, 1, 0, 1,
0, 1, 1, 0, 0, 0, 0, 1, 0, 1,
0, 0, 0, 0, 1, 0, 1, 1, 0, 1,
0, 1, 1, 1, 1, 0, 0, 0, 0, 0
};
void print_maze()
{
int i, j;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (maze[i][j]) printf("%c", 219);
else printf(" ");
}
printf("\n");
}
}
int gomaze(HERO* h)
{
HERO t = *h; int i;
for (i = 0; i < 2; t = *h)
{
print_hero(h, 1); sleep(1); go(&t);
if (t.x >= 0 && t.x < 10 && t.y >= 0 && t.y < 10 && !maze[t.y][t.x])
{
print_hero(h, 0); go(h);/*前方可走则向前走*/
if (h->x == 9 && h->y == 9) return 1; goleft(&t);
if (h->x == 0 && h->y == 0) i++;
if (t.x >= 0 && t.x < 10 && t.y >= 0 && t.y < 10 && !maze[t.y][t.x]) turnleft(h);/*左方无墙向左转*/
}
else turnright(h);/*前方不可走向右转*/
}
return 0;
}
main()
{
HERO Tom;/*有个英雄叫Tom*/
set_hero(&Tom, 0, 0, 0);/*放在(0,0)面朝北*/
clrscr();
print_maze();
gomaze(&Tom);/*Tom走迷宫*/
}
kinogre
2003-08-21
打赏
举报
回复
classic stack practice
alemjann
2003-08-21
打赏
举报
回复
#include <stdio.h>
typedef struct hero {int x,y,face;} HERO;
void set_hero(HERO* h,int x,int y,int face){h->x=x;h->y=y;h->face=face;}
void go(HERO* h){if(h->face%2) h->x+=2-h->face;else h->y+=h->face-1;}
void goleft(HERO* h){if(h->face%2) h->y+=h->face-2;else h->x+=h->face-1;}
void turnleft(HERO* h){h->face=(h->face+3)%4;}
void turnright(HERO* h){h->face=(h->face+1)%4;}
void print_hero(HERO* h, int b)
{
gotoxy(h->x + 1, h->y + 1);
if (b)
{
switch (h->face)
{
case 0: printf("%c", 24); break;
case 1: printf("%c", 16); break;
case 2: printf("%c", 25); break;
case 3: printf("%c", 27); break;
default: break;
}
}
else printf(" ");
}
int maze[10][10] =
{
0, 0, 0, 1, 0, 0, 0, 1, 0, 0,
1, 0, 1, 1, 0, 1, 1, 1, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 1, 0, 1, 1, 1,
0, 0, 1, 0, 1, 1, 0, 0, 0, 1,
1, 0, 1, 0, 1, 1, 0, 1, 0, 1,
0, 0, 1, 0, 1, 1, 0, 1, 0, 1,
0, 1, 1, 0, 0, 0, 0, 1, 0, 1,
0, 0, 0, 0, 1, 0, 1, 1, 0, 1,
0, 1, 1, 1, 1, 0, 0, 0, 0, 0
};
void print_maze()
{
int i, j;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (maze[i][j]) printf("%c", 219);
else printf(" ");
}
printf("\n");
}
}
int gomaze(HERO* h)
{
HERO t = *h; int i;
for (i = 0; i < 2; t = *h)
{
print_hero(h, 1); sleep(1); go(&t);
if (t.x >= 0 && t.x < 10 && t.y >= 0 && t.y < 10 && !maze[t.y][t.x])
{
print_hero(h, 0); go(h);/*前方可走则向前走*/
if (h->x == 9 && h->y == 9) return 1; goleft(&t);
if (h->x == 0 && h->y == 0) i++;
if (t.x >= 0 && t.x < 10 && t.y >= 0 && t.y < 10 && !maze[t.y][t.x]) turnleft(h);/*左方无墙向左转*/
}
else turnright(h);/*前方不可走向右转*/
}
return 0;
}
main()
{
HERO Tom;/*有个英雄叫Tom*/
set_hero(&Tom, 0, 0, 0);/*放在(0,0)面朝北*/
clrscr();
print_maze();
gomaze(&Tom);/*Tom走迷宫*/
}
alemjann
2003-08-21
打赏
举报
回复
#include <stdio.h>
typedef struct hero {int x,y,face;} HERO;
void set_hero(HERO* h,int x,int y,int face){h->x=x;h->y=y;h->face=face;}
void go(HERO* h){if(h->face%2) h->x+=2-h->face;else h->y+=h->face-1;}
void goleft(HERO* h){if(h->face%2) h->y+=h->face-2;else h->x+=h->face-1;}
void turnleft(HERO* h){h->face=(h->face+3)%4;}
void turnright(HERO* h){h->face=(h->face+1)%4;}
void print_hero(HERO* h, int b)
{
gotoxy(h->x + 1, h->y + 1);
if (b)
{
switch (h->face)
{
case 0: printf("%c", 24); break;
case 1: printf("%c", 16); break;
case 2: printf("%c", 25); break;
case 3: printf("%c", 27); break;
default: break;
}
}
else printf(" ");
}
int maze[10][10] =
{
0, 0, 0, 1, 0, 0, 0, 1, 0, 0,
1, 0, 1, 1, 0, 1, 1, 1, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 1, 0, 1, 1, 1,
0, 0, 1, 0, 1, 1, 0, 0, 0, 1,
1, 0, 1, 0, 1, 1, 0, 1, 0, 1,
0, 0, 1, 0, 1, 1, 0, 1, 0, 1,
0, 1, 1, 0, 0, 0, 0, 1, 0, 1,
0, 0, 0, 0, 1, 0, 1, 1, 0, 1,
0, 1, 1, 1, 1, 0, 0, 0, 0, 0
};
void print_maze()
{
int i, j;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (maze[i][j]) printf("%c", 219);
else printf(" ");
}
printf("\n");
}
}
int gomaze(HERO* h)
{
HERO t = *h; int i;
for (i = 0; i < 2; t = *h)
{
print_hero(h, 1); sleep(1); go(&t);
if (t.x >= 0 && t.x < 10 && t.y >= 0 && t.y < 10 && !maze[t.y][t.x])
{
print_hero(h, 0); go(h);/*前方可走则向前走*/
if (h->x == 9 && h->y == 9) return 1; goleft(&t);
if (h->x == 0 && h->y == 0) i++;
if (t.x >= 0 && t.x < 10 && t.y >= 0 && t.y < 10 && !maze[t.y][t.x]) turnleft(h);/*左方无墙向左转*/
}
else turnright(h);/*前方不可走向右转*/
}
return 0;
}
main()
{
HERO Tom;/*有个英雄叫Tom*/
set_hero(&Tom, 0, 0, 0);/*放在(0,0)面朝北*/
clrscr();
print_maze();
gomaze(&Tom);/*Tom走迷宫*/
}
flybird66
2003-08-21
打赏
举报
回复
用栈的方法比较好做,走过的路设个标识
98440622
2003-08-21
打赏
举报
回复
每个位置八个方向,用栈回溯,你会找到出口的:-)
Use your head, then you will find a way
idontlikenickname
2003-08-20
打赏
举报
回复
To:made_in_(潦倒)
一直右拐或左拐?如果要时遇到一个环形的地图怎么办?那就一直转下去永远也出不来了~
应该设置一个状态机,90%的几率右转,10%的几率左转,这样才"有可能"走出来~~
raindream
2003-08-19
打赏
举报
回复
补充一句,用从c++编.
made_in_
2003-08-19
打赏
举报
回复
思想就是:当碰到拐弯口的时候,记住一直右拐或者一直左拐,这样就能找到出口了.
用邻接表
求解
迷宫
问题
数据结构中用邻接表
求解
迷宫
问题
,该算法运用了简单的原理,但是非常实用的解决了
迷宫
问题
队列
求解
迷宫
问题
ACM、数据结构练习经典
问题
——队列
求解
迷宫
问题
。VC6.0调试通过。
matlab常微分方程
求解
matlab常微分方程
求解
,通过编程实战掌握具体应用。包括matlab常微分方程
求解
前、matlab常微分方程
求解
中、matlab常微分方程
求解
后。
数据结构课程设计--
求解
迷宫
问题
数据结构课程设计--
求解
迷宫
问题
AStar算法
求解
迷宫
问题
根据最优路径算法Astar算法编写的C++
求解
迷宫
问题
C语言
69,382
社区成员
243,073
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章