请求C语言编程高手帮助

[复制链接]
查看11 | 回复0 | 2009-9-17 16:21:28 | 显示全部楼层 |阅读模式
//给你修改了一下,可以了,主要修改了一下findpath的逻辑:#include#define ROW 6#define COL 6//*************变量及函数声明*******************typedef struct{ int row; int col;}TRACK;TRACK trk[36];int trkind = 0;typedef struct { int x; int y;}direction;direction move[4]={ {1,0}, {-1,0}, {0,1}, {0,-1}};int findpath(int row,int col);void print_path();//4×4迷宫图:int pic[ROW][COL]={ {1,1,1,1,1,1}, {1,0,0,1,1,1}, {1,1,0,0,0,1}, {1,0,0,1,1,1}, {1,1,0,0,0,1}, {1,1,1,1,1,1}};//****************主函数************************int main(void){ if(findpath(1,1)==1)print_path(); return 0;}//*****************子函数***********************int findpath(int row,int col){ int i=0,j=0,next_row,next_col; trk[trkind].row = row; trk[trkind].col = col; trkind++; while(i<4) {next_col=col+move.x;next_row=row+move.y;for (j=0; j<trkind-1; ++j){ if ((trk[j].row == next_row)&&(trk[j].col == next_col)) break;}if (j < trkind-1){ i++; continue;}if(pic[next_row][next_col]==0){ if(next_row==ROW-2&&next_col==COL-2) {
pic[next_row][next_col]=3;
pic[row][col]=3;
return 1; } if(findpath(next_row,next_col)==1) {
pic[row][col]=3;
return 1; }}/*else{ if(findpath(next_row,next_col)==1) {
pic[next_row-move.y][next_col-move.x]=3;
return 1; }}*/i++; }return 0;}void print_path(){ int i,j; for(i=0;i<6;i++,printf("\n"))for(j=0;j<6;j++) printf("%d",pic[j]); printf("0代表通路,1代表墙壁,3代表正确路径\n");}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行