回溯走迷宫

#include<iostream>
#include"LinkedStack.cpp"
using namespace std;

class Point{
public:
    int x;
    int y;
public:
    Point():x(0),y(0){}
    Point(int i,int j):x(i),y(j){}
};

template<typename T>
int fun(int (*a)[10],int i,int j,LinkedStack<T>& sk)
{
    int cx,cy;
    Point p(0,0);
    for(int x=1;x>=-1;--x){
        for(int y=1;y>=-1;--y){
            cx = x+i;
            cy = y+j;
            if((x==0&&y==0)||cx<0||cy<0||cx>9||cy>9)
                continue;
            if(*(*(a+cx)+cy)==0){
                p = Point(cx,cy);
                sk.Push(p);
                *(*(a+cx)+cy)=1;
                if(cx==9&&cy==9)
                    return 1;
                if(fun(a,cx,cy,sk)){
                    return 1;
                }
            }
        }
    }
    sk.Pop(p);
    return 0;
}

int main()
{
    LinkedStack<Point> sk;
    int a[10][10]={
        1,1,1,1,0,1,1,1,1,1,
        0,1,1,0,1,1,0,0,0,1,
        1,0,0,1,0,1,1,0,0,1,
        1,1,1,0,1,1,1,1,0,1,
        1,1,1,0,1,0,1,0,1,1,
        1,1,1,0,0,1,0,1,1,1,
        1,1,0,1,1,1,0,1,1,1,
        1,1,1,0,1,1,0,1,1,1,
        1,1,1,1,1,1,1,0,0,1,
        1,1,1,1,1,1,1,1,1,0
    };
    Point pStart(1,1);
    sk.Push(pStart);
    fun(a,1,0,sk);
    while(not sk.IsEmpty()){
        sk.Pop(pStart);
        cout << "[" << pStart.x << "," << pStart.y << "] ";
    }
    cout << endl;
}

[9,9] [8,8] [8,7] [7,6] [6,6] [5,6] [4,5] [5,4] [4,3] [3,3] [2,2] [2,1] [1,1] 

你可能感兴趣的:(C++,c,C#,J#)