예제 #1
0
파일: part2.cpp 프로젝트: nejyeah/cplusplus
void deepthFirstStackSearch(Maze &maze,pointT current,pointT exit){
    set<pointT> block;
    stack<pointT> pathStack;
    pathStack.push(current);
    while(true){
        pointT currentP=pathStack.top();
        block.insert(currentP);
        pointT around;
        bool flag=false;
        int i=0;
        while(i<4){
            around=currentP;
            switch(i){
                case 0:
                    around.col--;
                    break;
                case 1:
                    around.row--;
                    break;
                case 2:
                    around.col++;
                    break;
                default:
                    around.row++;
            }
            //判断该点合法
            if(maze.pointInBounds(around)&&!maze.isWall(currentP,around)&&block.count(around)==0){
                flag=true;
                break;
            }
            i++;
        }
        //有可走的子节点
        if(flag){
            if(around==exit){
                maze.drawMark(around,"Red");
                break;
            }
            else{
                pathStack.push(around);
            }
        }
        else{
            pathStack.pop();
        }
    }
    while(!pathStack.empty()){
        maze.drawMark(pathStack.top(),"Red");
        pathStack.pop();
    }
}
예제 #2
0
파일: part2.cpp 프로젝트: nejyeah/cplusplus
bool search(Maze &maze,set<pointT> &includePoint,pointT current,pointT exit){
    pointT around;
    includePoint.insert(current);
    int i=0;
    while(i<4){
        around=current;
        switch(i){
            case 0:
                around.col--;
                break;
            case 1:
                around.row--;
                break;
            case 2:
                around.col++;
                break;
            default:
                around.row++;
        }
        //判断该点合法
        if(maze.pointInBounds(around)&&!maze.isWall(current,around)&&includePoint.count(around)==0){
            if(around==exit){
                cout<<"get it"<<endl;
                maze.drawMark(around,"Red");

                return true;
            }
            else{
                bool flag=search(maze,includePoint,around,exit);
                if(flag){
                    maze.drawMark(around,"Red");
                    return true;
                }
            }
        }
        i++;
    }
    return false;
}
예제 #3
0
파일: part2.cpp 프로젝트: nejyeah/cplusplus
void breadthFirstSearch(Maze &maze,const pointT entry,const pointT exit){

    pointT current=entry;
    pointT around;
    //int i=search(maze,current,current,exit);

    queue< stack<pointT> > pathQueue;
    stack<pointT> currentStack;
    set<pointT> includePointT;

    currentStack.push(current);
    pathQueue.push(currentStack);

    while(true){

        //获取当前队列的最先进去的一个栈,栈顶的点作为当前点
        currentStack=pathQueue.front();
        current=currentStack.top();

        //将当前点插入set,保证不走重复路
        includePointT.insert(current);

        //获取当前点附近可走的点
        int i=0;
        bool flag=false;
        while(i<4){
            around=current;
            switch(i){
                case 0:
                    around.col--;
                    break;
                case 1:
                    around.row--;
                    break;
                case 2:
                    around.col++;
                    break;
                default:
                    around.row++;
            }
            i++;
            //判断该点是否合法
            if(maze.pointInBounds(around)){
                //int index=includePointT.count(around);
                if(!maze.isWall(current,around)&&includePointT.count(around)==0){
                    //判断是否是出口
                    if(around==exit){
                        cout<<"get it"<<endl;    

                        maze.drawMark(around,"Red");
                        flag=true;
                        break;
                    }
                    else{
                        stack<pointT> tem = currentStack;
                        tem.push(around);
                        pathQueue.push(tem);
                    }
                }
            }

        }
        //如果找到出口退出循环
        if(flag){
            //mark the path
            while(!currentStack.empty()){
                pointT top=currentStack.top();
                maze.drawMark(top,"Red");
                currentStack.pop();
            }
            break;
        }
        else{
            pathQueue.pop();    
        }

    }

}
예제 #4
0
파일: part2.cpp 프로젝트: nejyeah/cplusplus
void deepthFirstIteratorSearch(Maze &maze,pointT current,pointT exit){
    set<pointT> includePoint;
    includePoint.insert(current);
    bool flag=search(maze,includePoint,current,exit);
    if(flag) maze.drawMark(current,"Red");
}