/*
* 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");
}
網頁
BloggerAds 廣告
標籤
- Java (96)
- Android (27)
- 演算法 (21)
- c++ (19)
- JavaScript (7)
- OpenMp (6)
- Design Pattern (4)
- 日文歌曲 (4)
- 資料結構 (4)
- Foundation Knowledge Of Programming (3)
- QUT (2)
- CodingHomeWork (1)
- Database (1)
- 英文歌詞 (1)
搜尋此網誌
2012年5月6日 星期日
Solvaing Maze Traversal problems with a Brute-Force algorithm
訂閱:
張貼留言 (Atom)
我的網誌清單
標籤
日文歌曲
(4)
股市
(7)
股票
(9)
英文歌詞
(1)
時事
(1)
硬體(hardware)
(1)
資料結構
(4)
演算法
(21)
數學(Math)
(4)
ACM
(3)
ajax
(7)
algorithms
(1)
Android
(27)
Blog Notes(部落格記事)
(6)
C
(9)
c++
(19)
CodingHomeWork
(1)
Database
(1)
Design Pattern
(4)
Foundation Knowledge Of Programming
(3)
GWT
(1)
How
(2)
J2EE
(1)
Java
(96)
Java語言
(4)
JavaScript
(7)
Leetcode
(4)
LOL
(1)
OpenMp
(6)
QUT
(2)
Uva
(2)
Yahoo知識問答
(11)
沒有留言:
張貼留言