搜尋此網誌

2012年5月6日 星期日

Solvaing Maze Traversal problems with a Brute-Force algorithm

/*
 * Hello.c
 *
 *  Created on: 2012/5/5
 *      Author: Administrator
 */
#include <stdio.h>
#include <stdlib.h>
#define startRow 2
#define startCol 0
#define endRow 5
#define endCol 7

void mazeTraverse(char [][8], int , int , char);
void printMaze(char[][8]);
#include <stdlib.h>
#include <stdio.h>
int main(void){
    char maze [][8]={
            {'#','#','#','#','#','#','#','#'},
            {'#','.','.','#','#','#','#','#'},
            {'.','.','#','#','.','#','#','#'},
            {'#','.','#','#','.','#','#','#'},
            {'#','.','#','#','.','#','#','#'},
            {'#','.','.','.','.','.','.','.'},
            {'#','.','#','.','#','#','.','#'},
            {'#','#','#','#','#','#','#','#'},

    };
    mazeTraverse(maze, startRow, startCol, 's');
    printMaze(maze);
    return 0;
}
void mazeTraverse(char maze[][8], int r, int c, char d) {
    maze[r][c] = 'X';
    if(r == endRow && c == endCol){
            return;
    }else{
            if ('n' == d) {
                if (maze[r][c + 1] != '#') {
                    mazeTraverse(maze, r, c+1, 'w');
                } else if (maze[r - 1][c] != '#') {
                    mazeTraverse(maze, r-1, c, 'n');
                } else if (maze[r][c + 1] != '#') {
                    mazeTraverse(maze, r, c+1, 'e');
                } else {
                    mazeTraverse(maze, r+1, c, 's');
                }
            }

            if ('e' == d) {
                if (maze[r - 1][c] != '#') {
                    mazeTraverse(maze, r-1, c, 'n');
                } else if (maze[r][c + 1] != '#') {
                    mazeTraverse(maze, r, c+1, 'e');
                } else if (maze[r + 1][c] != '#') {
                    mazeTraverse(maze, r+1, c, 's');
                } else {
                    mazeTraverse(maze, r, c-1, 'w');
                }
            }

            if ('s' == d) {
                if (maze[r][c + 1] != '#') {
                    mazeTraverse(maze, r, c+1, 'e');
                } else if (maze[r + 1][c] != '#') {
                    mazeTraverse(maze, r+1, c, 's');
                } else if (maze[r][c - 1] != '#') {
                    mazeTraverse(maze, r, c-1, 'w');
                } else {
                    mazeTraverse(maze, r-1, c, 'n');
                }
            }

            if ('w' == d) {
                if (maze[r + 1][c] != '#') {
                    mazeTraverse(maze, r+1, c, 's');
                } else if (maze[r][c - 1] != '#') {
                    mazeTraverse(maze, r, c-1, 'w');
                } else if (maze[r - 1][c] != '#') {
                    mazeTraverse(maze, r-1, c, 'n');
                } else {
                    mazeTraverse(maze, r, c+1, 'e');
                }
            }
    }

}

void printMaze(char maze[][8]) {
    int i, j;
    for (i = 0; i < 8; i++) {
        for (j = 0; j < 8; j++) {
            printf("%c", maze[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

沒有留言:

張貼留言