经典c程序(0033) ---迷宫通路求解(单条通路DFS)
2014-07-11 09:22
330 查看
/************************************************************************************** * Function : test * Create Date : 2014/07/11 * Author : NTSK13 * Email : beijiwei@qq.com * Copyright : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。 * 任何单位和个人不经本人允许不得用于商业用途 * Version : V0.1 *************************************************************************************** 经典c程序(0033) ---迷宫通路求解01 题目:较复杂迷宫求解 10*10 有多个分叉口,但只有一条通路 1.从起点到终点, 依次打印出通路坐标 2.输出通路地图,可行处标记为2 0为墙,1为通道 入口: 1 0 0 1 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0 0 0 1 1 1 1 1 1 0 0 0 1 0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1 1 1 1 1 1 1 1 1 0 1 0 0 1 0 0 0 0 1 1 1 0 1 1 1 0 0 0 1 0 1 1 0 0 1 1 出口 **************************************************************************************/ #include<stdio.h> #define M 10 typedef struct{ int x; int y; }Pos; Pos start,end; int data[M][M]; /***********************************************************/ int offset[4][2]={-1,0,0,1,1,0,0,-1}; int mark[M][M]; int check(Pos start,Pos end); Pos store_pos[M*M]; void get_pos(Pos head); int tail=0; int mark_pos[M][M]; /***********************************************************/ int main(void) { int test_case; int T=0,th=0; /* The freopen function below opens input.txt file in read only mode, and afterward, the program will read from input.txt file instead of standard(keyboard) input. To test your program, you may save input data in input.txt file, and use freopen function to read from the file when using scanf function. You may remove the comment symbols(//) in the below statement and use it. But before submission, you must remove the freopen function or rewrite comment symbols(//). */ freopen("input.txt", "r", stdin); /* If you remove the statement below, your program's output may not be rocorded when your program is terminated after the time limit. For safety, please use setbuf(stdout, NULL); statement. */ setbuf(stdout, NULL); scanf("%d", &T); for(test_case = 0; test_case < T; test_case++) { int ret=0,i=0,j=0; /********************************** * Implement your algorithm here. */ scanf("%d", &th); for(i=0;i<M;i++) for(j=0;j<M;j++) { scanf("%d",&data[i][j]); mark[i][j]=0; } for(i=0;i<M*M;i++) { store_pos[i].x=-1; store_pos[i].y=-1; } start.x=0; start.y=0; end.x=9; end.y=9; tail=0; ret=check(start,end); /**********************************/ // Print the answer to standard output(screen). printf("#%d %d\n",th,ret); fflush(stdout);//修复Eclipse printf()不能显示的小bug } return (0);//Your program should return 0 on normal termination. } int check(Pos start,Pos end) { int x=0,y=0,tx=0,ty=0,k=0,ret=0; int i=0,j=0; Pos tmp; x=start.x; y=start.y; data[x][y]=2; mark[x][y]=1; if(x==end.x && y==end.y) { tmp.x=0; tmp.y=0; for(i=0;i<M;i++) for(j=0;j<M;j++) mark_pos[i][j]=0; get_pos(tmp); i=0; while( store_pos[i].x !=-1 ) { printf("(%d,%d) ",store_pos[i].x,store_pos[i].y); fflush(stdout); i++; } printf("\n\n"); fflush(stdout); for(i=0;i<M;i++) { for(j=0;j<M;j++) { printf("%d ",data[i][j]); fflush(stdout); } printf("\n"); fflush(stdout); } printf("\n"); fflush(stdout); return 1; } for(k=0;k<4;k++) { tx=x+offset[k][0]; ty=y+offset[k][1]; if( tx>=0 && tx<M && ty>=0 && ty<M && mark[tx][ty]==0 ) { if(data[tx][ty]==1) { start.x=tx; start.y=ty; ret=check(start,end); if(ret==1) return ret; } } } return ret; } void get_pos(Pos head) { int x=0,y=0,tx=0,ty=0,k=0; x=head.x; y=head.y; store_pos[tail++]=head; mark_pos[x][y]=1; for(k=0;k<4;k++) { tx=x+offset[k][0]; ty=y+offset[k][1]; if( tx>=0 && tx<M && ty>=0 && ty<M && mark_pos[tx][ty]==0) { if(data[tx][ty]==2) { head.x=tx; head.y=ty; get_pos(head); } } } }
相关文章推荐
- 经典c程序(0034) ---迷宫通路求解(多条通路DFS)
- 经典c程序(0035) ---迷宫求解(单条通路 类栈实现)
- 经典c程序(0036) ---迷宫求解(单条通路 栈实现)
- 经典算法<一>迷宫问题 1.单条路径 DFS求解 C++实现
- 【数据结构实验】编制一个程序求解迷宫通路
- 求解迷宫问题的所有路径及最短路径程序
- 迷宫求解非递归 DFS BFS(应用栈和队列)
- 模拟求解迷宫问题(DFS+BFS)
- The problem of maze(经典迷宫问题) DFS版
- 经典算法<一>迷宫问题 2.单条路径 BFS求解 C++实现
- 搜索入门之dfs--经典的迷宫问题解析
- 堆栈的应用--迷宫求解程序
- 【数据结构】迷宫问题求解(链栈,DFS)
- 经典程序--迷宫中的思索
- 迷宫问题(maze problem)——深度优先(DFS)与广度优先搜索(BFS)求解
- 迷宫问题(maze problem)——深度优先(DFS)与广度优先搜索(BFS)求解
- 求解迷宫问题的所有路径及最短路径程序
- 迷宫3--快速求解任意两点之间一条路,并不是最短(dfs)
- 杭电OJ(HDOJ)1010题:Tempter of the Bone(DFS,迷宫求解)
- 回溯算法经典应用:迷宫求解