迷宫
一个迷宫,只能向右向下移动,获得可以到达目标点的路径
#include<iostream>
using namespace std;
int mg[10][12] = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1 },
{ 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
struct Point{
Point(){}
Point(int x, int y) :x(x), y(y){}
int x;
int y;
}path[100];
void Go(Point point, int lenth){
path[lenth] = point;
if (point.x == 9 || point.y == 11 || mg[point.x][point.y] == 1)
return;
if (mg[point.x][point.y] == 2){
printf("\n getPaht!!! \n");
for (int i = 0; i <= lenth; i++){
printf("(%d,%d) ,",path[i].x,path[i].y);
}
return;
}
if (mg[point.x + 1][point.y] != 1){
Go(Point(point.x + 1, point.y), lenth + 1);
}
if (mg[point.x][point.y + 1] != 1){
Go(Point(point.x, point.y + 1), lenth + 1);
}
}
void main(){
printf("\n Bagin !!! %d \n", mg[8][10]);
Go(Point(1,1),0);
system("pause");
}