新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 棧的妙用-實現迷宮問題

        棧的妙用-實現迷宮問題

        作者: 時間:2016-12-01 來源:網絡 收藏

        /*used to store the used place*/
        int mark[NEW_MAZE_ROWS][NEW_MAZE_COLS];

        void mark_init()
        {
        int i = 0,j = 0;
        for(i = 0; i < NEW_MAZE_ROWS ; ++ i)
        for(j = 0; j < NEW_MAZE_COLS ; ++ j)
        mark[i][j] = 0;
        }
        intmark_stack_size(int maze[NEW_MAZE_ROWS][NEW_MAZE_COLS])
        {
        int i = 0,j = 0;

        int size = 0;
        for(i = 0; i < NEW_MAZE_ROWS; ++ i)
        for (j = 0; j < NEW_MAZE_COLS ; ++ j)
        {
        if(!maze[i][j])
        size ++;
        }
        return size;
        }

        coordinate nextposition(element a,int dir)
        {
        coordinate b;
        b.col = a.col + move[dir].horiz;
        b.row = a.row + move[dir].vert;

        return b;
        }

        int maze_out()
        {
        element temp;
        coordinatenextp;

        /*Test the stack is not empty*/
        while(top >= 0)
        {
        /*pop a element*/
        temp = *(stack+top);
        top --;

        /*find on eight directions*/
        while(temp.dir < 8)
        {
        /*get the possible next positions*/
        nextp = nextposition(temp,temp.dir);
        /*next direction*/
        temp.dir ++;

        /*success conditions*/
        if(nextp.row == EXIT_ROW &&
        nextp.col == EXIT_COL)
        {
        /*save current position*/
        stack[++top] = temp;

        /*save the exit pointion*/
        stack[++top].row = EXIT_ROW;
        stack[top].col = EXIT_COL;
        stack[top].dir = 0;

        /*exit*/
        return 1;
        }

        /*the new position is ok and does not wake*/
        if(maze[nextp.row][nextp.col] ==0 &&
        mark[nextp.row][nextp.col] == 0)
        {
        /*mark means that this way has been waked*/
        mark[nextp.row][nextp.col] = 1;

        /*
        *push a element in stack
        *save current position and direction
        *if this way is failed, used to this position to start new way.
        */
        stack[++top] = temp;

        /*go to the new position as current position*/
        temp.row = nextp.row;
        temp.col =nextp.col;
        temp.dir = 0;
        }
        }
        }
        /*failed*/
        return 0;
        }

        int main()
        {
        int i = 0;
        /*inital the mark array*/
        mark_init();
        initial_move();

        /*create stack*/
        stack = (element*)malloc(mark_stack_size(maze)*sizeof(element));
        /*if failed*/
        if(stack == NULL)
        return 0;
        /*push a element in stack*/
        top ++;
        (stack+top)->col = 1;
        (stack+top)->row = 1;
        (stack+top)->dir = 0;

        if(maze_out())
        {
        while(i <= top)
        {
        printf("(%d,%d,%d)",stack[i].row,stack[i].col,stack[i].dir);
        i ++;
        }
        // printf("(%d,%d)",EXIT_ROW,EXIT_COL);

        }
        /*free the memory*/
        free(stack);
        /*point to the NULL*/
        stack = NULL;
        return 1;
        }

        測試結果:

        在迷宮問題中,棧主要實現了對滿足條件坐標以及方向值(0-7,分別表示一個具體的方向)的動態保存能夠保證路勁的一致性,也就是先入后出的特性。


        上一頁 1 2 下一頁

        關鍵詞: 堆棧迷宮問

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 宜章县| 深水埗区| 隆子县| 封丘县| 安阳市| 双鸭山市| 嘉定区| 阳春市| 清苑县| 滨海县| 美姑县| 清远市| 凤凰县| 墨竹工卡县| 南京市| 德格县| 平凉市| 和静县| 张家口市| 新巴尔虎左旗| 郓城县| 仙游县| 莱阳市| 固镇县| 天镇县| 五原县| 湄潭县| 绥芬河市| 兴海县| 泌阳县| 阿坝| 红原县| 万全县| 宜君县| 怀仁县| 黄梅县| 定日县| 津南区| 惠安县| 怀化市| 夹江县|