定义一个二维数组: 
int maze[5][5] = {

	0, 1, 0, 0, 0,

	0, 1, 0, 1, 0,

	0, 0, 0, 0, 0,

	0, 1, 1, 1, 0,

	0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
Output
左上角到右下角的最短路径,格式如样例所示。
Sample Input
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

因为不会搜索 , 所以慢慢来~~~

可是这题数据很水啊,直接输出样例就可以过了。也不知道自己写的对不对。

难受~~~

代码:

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <cstring>
using namespace std;
int a[5][5] , book[5][5] , b[5][5];
int q[30] , w[30];
int minn = 9999999;
struct note
{
	int a ;
	int b;
}s[100];
int top = 0;
int ret;
void dfs(int x , int y ,int step)
{
	
	book[x][y] = 1;
	 int next[4][2] = {{0 , 1} , {1 , 0} , {0 , -1} , {-1 , 0}};
	 int tx , ty;
	 if(x == 4 && y == 4)
	 {
	 	if(step < minn)
	 	{
	 		minn = step ;
	 		
		 	for(int i = 1 ; i <= top; i++)
			 {
			 	ret = top;
				 q[i] = s[i].a ;
				 w[i] = s[i].b ;	 
			 }
		 }
//		 	for(int i = 0 ; i <= top; i++)
//			 {
//				printf("(%d, %d)\n" , q[i] , w[i]); 
//			 }
//			 printf("\n\n\n\n\n");
		 return;
	 }
	 for(int k = 0 ; k <= 3 ; k++)
	 {
	 	tx = x + next[k][0];
	 	ty = y + next[k][1];
	 	if(tx < 0 ||tx > 5 || ty < 0 || ty > 5)
	 	{
	 		continue;
		 }
		 if(a[tx][ty] == 0 && book[tx][ty] == 0)
		 {
		 	
		 	book[tx][ty] = 1;
		 	top++;
		 	s[top].a = tx;
		 	s[top].b = ty;
		 	dfs(tx,ty , step+1);
		 	book[tx][ty] = 0;
		 	top--;
		 }
	 }
	 return;
}
int main()
{
	memset(book , 0 , sizeof(book));
	memset(q , -1 , sizeof(q));
	memset(w , -1 , sizeof(w));
	for(int i = 0 ; i < 5 ; i++)
	{
		for(int j = 0 ; j < 5 ; j++)
		{
			scanf("%d" , &a[i][j]);
		}
	}
	dfs(0 , 0 ,0);
	printf("(0, 0)\n");
	for(int i = 0 ; i <= ret ; i++)
	{
		if(q[i] != -1 && w[i] != -1)
		{
			printf("(%d, %d)\n" , q[i] , w[i]);
		}
	}


	return 0;
}