3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
[C/C++ 演算法]-老鼠走迷官(二) 剛才找資料時發現一個C/C++的教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。 拷貝來源:http://openhome.cc/Gossip/AlgorithmGossip/http://openhome.cc/Gossip/AlgorithmGossip/MouseGoMaze2.htm #include #include #define SIZE 9 typedef struct { int x; int y; } Point; Point pt(int, int); void visit(int[][SIZE], Point, Point, void (*)(int[][SIZE])); void print(int[][SIZE]); int main(void) { int maze[SIZE][SIZE] = {{2, 2, 2, 2, 2, 2, 2, 2, 2}, {2, 0, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 2, 2, 0, 2, 2, 0, 2}, {2, 0, 2, 0, 0, 2, 0, 0, 2}, {2, 0, 2, 0, 2, 0, 2, 0, 2}, {2, 0, 0, 0, 0, 0, 2, 0, 2}, {2, 2, 0, 2, 2, 0, 2, 2, 2}, {2, 0, 0, 0, 0, 0, 0, 0, 2}, {2, 2, 2, 2, 2, 2, 2, 2, 2}}; visit(maze, pt(1, 1), pt(7, 7), print); return 0; } Point pt(int x, int y) { Point p = {x, y}; return p; } void visit(int maze[][SIZE], Point start, Point end, void (*take)(int[][SIZE])) { if(!maze[start.x][start.y]) { maze[start.x][start.y] = 1; if(maze[end.x][end.y]) { take(maze); } else { visit(maze, pt(start.x, start.y + 1), end, take); visit(maze, pt(start.x + 1, start.y), end, take); visit(maze, pt(start.x, start.y - 1), end, take); visit(maze, pt(start.x - 1, start.y), end, take); } maze[start.x][start.y] = 0; } } void print(int maze[][SIZE]) { int i, j; for(i = 0; i < SIZE; i++) { for(j = 0; j < SIZE; j++) switch(maze[i][j]) { case 0 : printf(" "); break; case 1 : printf("◇"); break; case 2 : printf("█"); } printf("\n"); } }    

本文由jashliaoeuwordpress提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦