看到迷宫算法的题目,自己先写了个代码,感觉肯定是有错误的。先看一下我的代码。接下来分析一下错误。其实也不算是错误,就是不完备吧。
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">#include <iostream></span>
#include <stdio.h>
#include <conio.h>
int maze[9][9]={
{2,2,2,0,2,2,2,0,0},
{2,0,0,0,0,0,2,0,0},
{2,0,2,2,2,2,2,2,2},
{0,0,0,0,0,0,0,0,0},
{2,0,2,2,2,2,0,2,2},
{2,0,2,2,0,0,0,2,2},
{2,0,2,2,0,2,2,0,2},
{2,2,2,0,0,0,0,0,0},
{2,0,2,2,2,2,2,2,2}
};
void show(){
for(int i=0; i< 9; i++){
for(int j = 0; j< 9 ; j++){
printf("%d, " ,maze[i][j]);
}
printf("\n\r");
}
}
bool find_next_way(int x, int y ){
bool flag1 = true;
bool flag2 = true;
bool flag;
show();
printf("%d, %d\n\r", x, y);
if(x==0 || x == 9 || y == 0 || y== 9){
printf("over\n");
return true;
}
if(maze[x][y] == 2){
printf("wall\n\r");
return false;
}else if( maze[x][y] == 0){
maze[x][y] = 1;
flag1 = find_next_way(x+1, y);
flag2 = find_next_way(x, y+1);
maze[x][y] = 0;
}else if (maze[x][y] == 1){
return true;
}else{
}
flag = (flag1 || flag2);
if(flag == true){
maze[x][y] = 5;
}
return flag;
}
int main( void )
{
find_next_way(1,1);
show();
}
其实,刚开始想不通如何将结果保存显示出来,后来想通了直接通过边界出判断时候,将所有标志位1的地方记录下来,就是一条通路了。不过仍然有个bug,就是用户不进去,从入口出就出去了。
#include <iostream>
#include <stdio.h>
#include <conio.h>
int maze[9][9]={
{2,2,2,0,2,2,2,0,0},
{2,0,0,0,0,0,2,0,0},
{2,0,2,2,2,2,2,2,2},
{0,0,0,0,0,0,0,0,0},
{2,0,2,2,2,2,0,2,2},
{2,0,2,2,0,0,0,2,2},
{2,0,2,2,0,2,2,0,2},
{2,2,2,0,0,0,0,0,0},
{2,0,2,2,2,2,2,2,2}
};
void show(){
for(int i=0; i< 9; i++){
for(int j = 0; j< 9 ; j++){
printf("%d, " ,maze[i][j]);
}
printf("\n\r");
}
}
bool find_next_way(int x, int y ){
bool flag1 = true;
bool flag2 = true;
bool flag3 = true;
bool flag4 = true;
bool flag;
//printf("%d, %d\n\r", x, y);
if(x==-1 || x == 9 || y == -1 || y== 9){
printf("over\n");
show();
return true;
}
if(maze[x][y] == 2){
// printf("wall\n\r");
return false;
}else if( maze[x][y] == 0){
maze[x][y] = 1;
flag1 = find_next_way(x+1, y);
flag2 = find_next_way(x, y+1);
flag3 = find_next_way(x-1, y);
flag4 = find_next_way(x, y-1);
maze[x][y] = 0;
}else if (maze[x][y] == 1){
return true;
}else{
}
flag = (flag1 || flag2);
if(flag == true){
// maze[x][y] = 5;
}
return flag;
}
int main( void )
{
find_next_way(0,3);
// show();
}
其实这样的话,标志变量也不需要了。
#include <iostream>
#include <stdio.h>
#include <conio.h>
int maze[9][9]={
{2,2,2,0,2,2,2,0,0},
{2,0,0,0,0,0,2,0,0},
{2,0,2,2,2,2,2,2,2},
{0,0,0,0,0,0,0,0,0},
{2,0,2,2,2,2,0,2,2},
{2,0,2,2,0,0,0,2,2},
{2,0,2,2,0,2,2,0,2},
{2,2,2,0,0,0,0,0,0},
{2,0,2,2,2,2,2,2,2}
};
void show(){
for(int i=0; i< 9; i++){
for(int j = 0; j< 9 ; j++){
printf("%d, " ,maze[i][j]);
}
printf("\n\r");
}
}
bool find_next_way(int x, int y ){
//printf("%d, %d\n\r", x, y);
if(x==-1 || x == 9 || y == -1 || y== 9){
printf("over\n");
show();
return true;
}
if(maze[x][y] == 2){
// printf("wall\n\r");
return false;
}else if( maze[x][y] == 0){
maze[x][y] = 1;
find_next_way(x+1, y);
find_next_way(x, y+1);
find_next_way(x-1, y);
find_next_way(x, y-1);
maze[x][y] = 0;
}else if (maze[x][y] == 1){
return true;
}else{
}
return true;
}
int main( void )
{
find_next_way(0,3);
// show();
}