经典c程序(0035) ---迷宫求解(单条通路 类栈实现)

<pre name="code" class="cpp">/************************************************************************************** 
* Function     : test
* Create Date  : 2014/07/13
* Author       : NTSK13 
* Email        : beijiwei@qq.com 
* Copyright    : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。 
*                任何单位和个人不经本人允许不得用于商业用途 
* Version      : V0.1                    
***************************************************************************************
经典c程序(0035) ---迷宫求解(单条通路stack)

题目:较复杂迷宫求解   有多个分叉口,但只有一条路径  .

0为墙,1为通道

start (0,0)  end (9,9)
入口:

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
int data[M][M];

/***********************************************************/
typedef struct {
	int x;
	int y;
	int len;
}Pos;

Pos start,end;

Pos cross_store[M*M];
int cross_increase;

Pos path_store[M*M];
int path_increase;

int len;
/***********************************************************/
int offset[4][2]={-1,0,0,1,1,0,0,-1};
int mark[M][M];

int find(Pos start, Pos end);
/***********************************************************/
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++)
		{
			cross_store[i].x=-1;
			cross_store[i].y=-1;

			path_store[i].x=-1;
			path_store[i].y=-1;
		}
		cross_increase=0;
		path_increase=0;
		len=0;
		start.x=0;
		start.y=0;
		end.x=9;
		end.y=9;

		ret=find(start,end);
		/*************************************/
		// Print the answer to standard output(screen).
		printf("#%d %d\n",th,ret);
	    fflush(stdout);//修复Eclipse printf()不能显示的小bug  
	    i=0;
	    while(path_store[i].x !=-1)
	    {
	    	printf("The %dth stp is (%d,%d) \n",i+1,path_store[i].x,path_store[i].y);
	    	fflush(stdout);//修复Eclipse printf()不能显示的小bug
	    	i++;
	    }

	}
	return (0);//Your program should return 0 on normal termination.
}

int find(Pos start, Pos end)
{
	int x=0,y=0,tx=0,ty=0,ret=0,k=0,flag=0;

	x=start.x;
	y=start.y;

	path_store[path_increase].x=x;
	path_store[path_increase].y=y;
	path_store[path_increase].len=len++;
	path_increase++;

	mark[x][y]=1;

	if(x==end.x && y==end.y)//找到
	{
		return (1);
	}

	for(k=0;k<4;k++)// check cross
	{
		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)
				flag++;
		}
	}
	if(flag==0)//no path; return last cross
	{
		x=cross_store[cross_increase-1].x;
		y=cross_store[cross_increase-1].y;
		len=cross_store[cross_increase-1].len;
		path_increase=len;

	}
	if(flag>1)// encounter cross ,store
	{
		cross_store[cross_increase].x=x;
		cross_store[cross_increase].y=y;
		cross_store[cross_increase].len= len;
		cross_increase++;
	}

	for(k=0;k<4;k++)// go
	{
		tx=x+offset[k][0];
		ty=y+offset[k][1];
		if( tx>=0 && tx<M && ty>=0 && ty<M && mark[tx][ty]==0 )
		{
			start.x=tx;
			start.y=ty;
			if(data[tx][ty]==1)
				ret=find(start,end);
			if(ret==1)
				return ret;
		}
	}
	return ret;
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值