[C/C++ 演算法]-老鼠走迷官(二)
[C/C++ 演算法]-老鼠走迷官(二)
剛才找資料時發現一個C/C++的教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。
拷貝來源:
http://openhome.cc/Gossip/AlgorithmGossip/
http://openhome.cc/Gossip/AlgorithmGossip/MouseGoMaze2.htm
#include <stdio.h>
#include <stdlib.h>
#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");
}
}
|