📄 simple_migong.cpp
字号:
#include <stdio.h>
#include <conio.h>
int maze[8][7] = {
1, 1,1,1,1,1, 1,
1, 0,0,0,0,1, 1,
1, 1,0,1,0,0, 1,
1, 1,0,1,0,0, 1,
1, 1,0,0,0,1, 1,
1, 0,0,1,0,1, 1,
1, 1,0,0,0,0, 1,
1, 1,1,1,1,1, 1 };
void print_result()
{ int i,j;
printf("--------------------\n");
for(i = 0; i<=7; i++)
{
for(j = 0; j<=6; j++)
{ if (maze[i][j] == 2) textcolor(15); else textcolor(1);
cprintf("%d ",maze[i][j]);
}
printf("\n");
}
/* reset the old path that can not reach the final point. */
for(i = 0; i<=7; i++)
for(j = 0; j<=6; j++)
if (maze[i][j] == 3 ) maze[i][j] = 0;
maze[6][5] = 0;
getch();
}
int way(int x,int y)
{ int d1,d2,d3,d4 ;
if (maze[6][5] == 2 )
{ print_result() ; return 1;}
else
if (maze[x][y] == 0) /* if there have a path we can walk... */
{
maze[x][y] = 2;
d1 = way(x,y-1);
d2 = way(x,y+1);
d3 = way(x-1,y);
d4 = way(x+1,y);
if (d1 || d2 || d3 || d4 )
{ maze[x][y] = 0; return 1; }
else
{ maze[x][y] = 3; return 0; }
}
else return 0;
}
void main()
{ int i,j;
clrscr();
printf("search path from 1,1 to 6,5:\n");
for(i = 0; i<=7; i++)
{ for(j = 0; j<=6; j++)
{ if (maze[i][j] == 0 ) textcolor(15); else textcolor(7);
cprintf("%d ",maze[i][j]);
}
printf("\n");
}
way(1,1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -