// maze.cpp : 定義控制台應用程式的入口點。
//#include "stdafx.h"
#include #define stack_init_size 100
#define stack_add_size 10
#define row_number 3
#define col_number 3
typedef struct
cell;
typedef struct
pathstack;
int initstack(pathstack &stack)
stack.top = stack.base;
stack.stacksize = stack_init_size;
return 0;
}int push(pathstack &stack, cell element)
*stack.top = element;
stack.top++;
return 0;
}cell pop(pathstack &stack)
return *(--stack.top);
}cell gettop(pathstack stack)
return *(--stack.top);
}int findallpath(int mazearray[row_number][col_number])
else
cellarray[i][j].instack = false;
} }pathstack stack;
initstack(stack);
if (!push(stack, cellarray[0][0]))
else
int row, col; // 當前位置的座標
row = col = 0;
while (stack.base != stack.top)
if (mazearray[i][j])
else
cellarray[i][j].direction = 0;}}
} if (celltop.row == row_number - 1 && celltop.col == col_number - 1)
else
pbase++;
}pop(stack);
cellarray[row_number - 1][col_number - 1].instack = false;
continue;
} int east, south, west, north;
east = col + 1;
south = row + 1;
west = col - 1;
north = row - 1;
if (east < col_number && !cellarray[row][col].direction) // 探索東方位
cellarray[row][east - 1].direction = 1;
continue;
} if (south < row_number && cellarray[row][col].direction <= 1)
cellarray[south - 1][col].direction = 2;
continue;
} if (west >= 0 && cellarray[row][col].direction <= 2) // 探索東方位
cellarray[row][west + 1].direction = 3;
continue;
} if (north >= 0 && cellarray[row][col].direction <= 3)
cellarray[north + 1][col].direction = 4;
continue;
} cellarray[row][col].canpass = false;
cellarray[row][col].instack = false;
cell cellpop = pop(stack);
cellarray[cellpop.row][cellpop.col].direction = 0;
if (mazearray[cellpop.row][cellpop.col])
else
}return 0;
}int _tmain(int argc, _tchar* argv)
} findallpath(mazearray);
return 0;
}
用遞迴函式求出迷宮所有解
牆元素值為0,可通過路徑為 1,通過路徑為足跡。輸入格式為x,y,中間有逗號。依次試探東南西北四個方向 include using namespace std struct postype 迷宮座標位置型別 define maxlength 25 設迷宮的最大行列為25 typedef int ma...
程式設計訓練 輸出迷宮所有解(不是迷宮最短路)
之前寫過乙個用bfs解決迷宮最短路的問題,這篇文章則是另外乙個問題,那就是輸出迷宮的所有可能路徑。直接看樣例輸入輸出就明白題意了 樣例輸入13 s.e3代表迷宮是3 times 3的,s代表起點,e代表終點,代表路,代表牆壁。樣例輸出1 0,1 1,1 2,1 2,2 顯然這個迷宮只有一條路徑,上述...
求N皇后問題所有解(分支限界法)
問題描述 略 注意事項 這裡用的是fifo佇列 細節說明 裡面有詳細的註解 及執行截圖 include include include include include using namespace std 定義乙個結點類 struct node 定義乙個queen的類 class queen 判斷...