c语言迷宫问题 急求

2024-11-06 21:34:40
推荐回答(2个)
回答1:

1是路径还是0是路径?
#include
#include
enum se{
path,
block,
end,
mark
};
struct way{
int x;
int y;
}record[1000];
int maze[14][28]={
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,
1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,1,0,0,1,
1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,1,1,1,0,1,1,1,1,0,0,0,1,1,0,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,1,0,0,1,
1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
void set_end(int ex, int ey){ //设置结束
maze[ex][ey] = end;
}
void printMaze(){
for(int i=0;i<14;i++) //迷宫图形化输出
{
for(int j=0;j<28;j++)
{
if(maze[i][j] == block)
printf("■");
else printf("□");
}
printf("\n");
}
}
void printResult(int length){
for(int i = 0; i < 14; i++) //迷宫路径图形化输出
{
for(int j = 0; j < 28; j++)
{
if(maze[i][j] == mark)
printf(">>");
else if(maze[i][j] == block)
printf("■");
else if(maze[i][j] == end)
printf("<<");
else if(maze[i][j] == path)
printf("□");
}
printf("\n");
}
for(int x = 0; x < length - 1; x++){
printf("<%d,%d> ->", record[x].x, record[x].y);
}
printf("<%d,%d> ", record[x].x, record[x].y);
printf("\n");
}
void search_path(int x, int y, int length){ // 递归寻找路径
if(maze[x][y] == end){
printResult(length);
system("pause");
exit(0);
}
else{
if(maze[x + 1][y] != block && maze[x + 1][y] != mark){
maze[x][y] = mark;
record[length].x = x;
record[length].y = y;
length++;
search_path(x + 1, y, length);
maze[x][y] = path;
length --;
}
if(maze[x - 1][y] != block && maze[x - 1][y] != mark){
maze[x][y] = mark;
record[length].x = x;
record[length].y = y;
length++;
search_path(x - 1, y, length);
maze[x][y] = path;
length --;
}
if(maze[x][y + 1] != block && maze[x][y + 1] != mark){
maze[x][y] = mark;
record[length].x = x;
record[length].y = y;
length++;
search_path(x, y + 1, length);
maze[x][y] = path;
length --;
}
if(maze[x][y - 1] != block && maze[x][y - 1] != mark){
maze[x][y] = mark;
record[length].x = x;
record[length].y = y;
length++;
search_path(x, y - 1, length);
maze[x][y] = path;
length --;
}
return;
}
}

int main(void)
{
set_end(12,26);
printMaze();
search_path(1,5,0);
printf("no path!"); //没有路径
return 0;
}

回答2:

#include
using namespace std;
int main(){
int maze[14][28]= //迷宫举例:校园地图
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,
1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,1,0,0,1,
1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,1,1,1,0,1,1,1,1,0,0,0,1,1,0,1,1,1,1,0,1,
1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,1,0,0,1,
1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int i,j;

for(i=0;i<14;i++) //迷宫图形化输出
{
for(j=0;j<28;j++)
{
if(maze[i][j]==1)
cout<<"■";
else cout<<"□";
}
cout< }
return 0;
}