经典c程序(0034) ---迷宫通路求解(多条通路DFS)
来源:互联网 发布:swmm中文版软件下载 编辑:程序博客网 时间:2024/05/22 06:33
/************************************************************************************** * Function : test* Create Date : 2014/07/11* Author : NTSK13 * Email : beijiwei@qq.com * Copyright : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。 * 任何单位和个人不经本人允许不得用于商业用途 * Version : V0.1 ***************************************************************************************经典c程序(0034) ---迷宫通路求解02题目:较复杂迷宫求解 10*10 有多个分叉口,多条通路1.求通路个数2.分别输出通路地图,可行处标记为20为墙,1为通道入口:1 0 0 1 0 1 0 0 0 01 1 0 1 0 1 0 0 0 00 1 1 1 1 1 1 0 0 01 0 0 0 0 1 0 0 0 01 1 1 1 1 1 1 0 0 00 0 1 0 0 0 0 0 0 01 0 1 0 1 1 1 0 0 11 1 1 0 1 0 0 1 1 10 0 1 1 1 1 1 1 0 10 0 1 0 1 1 0 1 1 1 出口 **************************************************************************************/ #include<stdio.h>#define M 10typedef 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];void check(Pos start,Pos end);Pos store_pos[M*M];void get_pos(Pos head);int tail=0;int mark_pos[M][M];int count=0;int flag=0;/***********************************************************/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 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;}start.x=0;start.y=0;end.x=9;end.y=9;count=0;check(start,end);/**********************************/// Print the answer to standard output(screen).printf("#%d %d\n",th,count);//个数同路 fflush(stdout);//修复Eclipse printf()不能显示的小bug }return (0);//Your program should return 0 on normal termination.}void check(Pos start,Pos end){int x=0,y=0,tx=0,ty=0,k=0;int i=0,j=0,value=0;Pos tmp;x=start.x;y=start.y;value=data[x][y];//保存当前位置值data[x][y]=2;mark[x][y]=1;if(x==end.x && y==end.y){count++;tmp.x=0;tmp.y=0;for(i=0;i<M;i++)for(j=0;j<M;j++)mark_pos[i][j]=0;tail=0;for(i=0;i<M*M;i++){store_pos[i].x=-1;store_pos[i].y=-1;}flag=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);/****************************************************/data[x][y]=value; //到达终点后 回溯mark[x][y]=0;return;/****************************************************/}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;check(start,end);}}}data[x][y]=value;mark[x][y]=0;}void get_pos(Pos head){int x=0,y=0,tx=0,ty=0,k=0;x=head.x;y=head.y;if(flag==1)//这个必须加,否则 输出(9,9) 之后,有可能输出其他点 ( 9,5 9,4)return;store_pos[tail++]=head;mark_pos[x][y]=1;if(x==9 && y==9){flag=1;return;}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);}}}}
0 0
- 经典c程序(0034) ---迷宫通路求解(多条通路DFS)
- 经典c程序(0033) ---迷宫通路求解(单条通路DFS)
- 经典c程序(0035) ---迷宫求解(单条通路 类栈实现)
- 经典c程序(0036) ---迷宫求解(单条通路 栈实现)
- 【数据结构实验】编制一个程序求解迷宫通路
- 求迷宫通路问题
- 欧拉通路的求解
- 迷宫系列(三)利用BFS/DFS的数据得到最短路/通路
- 经典算法<一>迷宫问题 1.单条路径 DFS求解 C++实现
- lightoj 1316 状态压缩求解汉密尔顿通路
- 电路中最优数据通路截取求解
- BFS、DFS——求最短通路值
- UVALive 6590 Digraphs (DFS找环找通路)
- 通路搜索
- 经典算法<一>迷宫问题 4.多条路径 求路径条数求解 C++实现
- 用动态规划实现规则迷宫最短通路
- 寻找迷宫的一条出路 (o:通路,X障碍)
- 用递归实现求一个迷宫是否有通路
- 重要的cocos2d的在iPhone应用程序开发
- 这是三个最热门的梵文纹身设计...
- tesseract-ocr识别英文和中文图片文字以及扫描图片实例讲解
- 兼容性或不高 三星将推出虚拟显示设备
- openacs javascript脚本
- 经典c程序(0034) ---迷宫通路求解(多条通路DFS)
- 这帮助改善皮肤弹性在皮肤老化的食物
- XML 概述
- Android中软解码和硬解码的优先级
- RenderScript控制并行化线程数量
- 和小白一起学ABAP – 22 SAP程序设计之 商用文档设计
- git签出远程分支问题解决
- 解决eclipse中egit中的cannot open git-upload-pack问题
- JAVA System.arraycopy方法的使用