su5gsm
|
分享:
▲
#include <stdio.h> #include <stdlib.h> typedef struct { int vert; int horiz; }offsets; offsets move[8] ={ {1,0},{-1,0},{0,-1},{0,1},{1,1},{-1,1},{1,-1},{-1,-1} }; int maze[13][17]={ {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,1,1,0,0,0,1,1,1,1,1,1}, {1,1,0,0,0,1,1,0,1,1,1,0,0,1,1,1,1}, {1,0,1,1,0,0,0,0,1,1,1,1,0,0,1,1,1}, {1,1,1,0,1,1,1,0,0,1,1,0,1,1,0,0,1}, {1,1,1,0,1,0,0,1,0,0,1,1,1,1,1,1,1}, {1,0,0,1,1,0,1,1,1,0,0,0,0,0,0,1,1}, {1,0,1,1,1,1,0,0,1,1,1,1,1,1,0,1,1}, {1,0,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1}, {1,1,1,0,0,0,1,1,0,1,1,0,0,0,0,0,1}, {1,0,0,1,1,1,1,1,0,0,0,1,1,1,1,0,1}, {1,0,1,0,0,1,1,1,1,1,0,1,1,1,1,0,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} }; //迷宮 void print_ans() { int i,j; for(i=0;i<13;i++) { for(j=0;j<17;j++) printf("%d ",maze[j]); printf("\n"); } printf("\n"); return ; } //印答案 void findpath(int row, int col) { int direct,next_row,next_col,k,l; direct = 0; maze[row][col]=2; while(direct<8) { next_row =row + move[direct].vert; next_col =col + move[direct].horiz; if(maze[next_row][next_col]==0) { maze[next_row][next_col] =2; if(next_row==11 && next_col==15) print_ans(); else { findpath(next_row,next_col); maze[next_row][next_col] = 0; } } direct++; } } int main() { findpath(1,1); system("pause"); }
|