• 闲来无事帮人写作业


    迷宫问题。easy的。用递归和非递归。

    /*
    * 问题描述:
    * 说明老鼠走迷宫是递回求解的基本题型,我们在二维阵列中使用1表示迷宫墙壁,使用2来表
    * 示老鼠的行走路径,试以程式求出由入口至出口的路径。
    *
    * 若更改迷宫数据,需要修改行数:MAP_Y,列数:MAP_X即可.另外迷宫的最外层都必须设置为墙壁,
    * 起始点和终点定位可修改。
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <stack>

    using namespace std;

    #define MAP_X 11
    #define MAP_Y 12

    #define WALL 1
    #define PASS 0
    #define FOOT 2
    #define MARK 3
    /*
    * 坐标系统:
    * +----------------->(X)
    * |
    * | (x,y)
    * |
    * |
    * |
    * (Y)
    */
    typedef struct _pos{
    int x;
    int y;
    }pos;

    //起始位置:第1行1列
    static int start[2]= {1,1};

    //结束位置:第MAX_Y-2行MAP_X-2列
    static int end[2]= {MAP_Y-2,MAP_X-2};
    static int finish=0;

    /*
    * 迷宫最外一层为边界墙壁,从[1,1]起点到[MAP_X,MAP_Y]终点
    *
    */
    /*
    测试迷宫
    设定MAP_X=7,MAX_Y=7即可
    int m[MAP_X][MAP_Y] =
    {
    {WALL, WALL, WALL, WALL, WALL, WALL, WALL},
    {WALL, PASS, PASS, PASS, PASS, PASS, WALL},
    {WALL, PASS, WALL, PASS, WALL, PASS, WALL},
    {WALL, PASS, PASS, WALL, PASS, WALL, WALL},
    {WALL, WALL, PASS, WALL, PASS, WALL, WALL},
    {WALL, PASS, PASS, PASS, PASS, PASS, WALL},
    {WALL, WALL, WALL, WALL, WALL, WALL, WALL}
    };
    */

    /*
    *行数(MAP_Y)12*列数(MAP_X)11
    *成功迷宫
    */
    int m[MAP_Y][MAP_X] = {
    {WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL},/*一行*/
    {WALL, PASS, PASS, PASS, PASS, PASS, WALL, WALL, WALL, WALL, WALL},
    {WALL, WALL, WALL, WALL, WALL, PASS, WALL, PASS, WALL, WALL, WALL},
    {WALL, WALL, WALL, WALL, WALL, PASS, WALL, WALL, WALL, WALL, WALL},
    {WALL, PASS, WALL, PASS, WALL, PASS, WALL, PASS, PASS, PASS, WALL},
    {WALL, PASS, WALL, PASS, WALL, PASS, WALL, PASS, WALL, PASS, WALL},
    {WALL, PASS, PASS, PASS, WALL, PASS, WALL, PASS, WALL, PASS, WALL},
    {WALL, PASS, WALL, WALL, WALL, PASS, WALL, PASS, WALL, PASS, WALL},
    {WALL, PASS, WALL, PASS, PASS, PASS, WALL, PASS, WALL, PASS, WALL},
    {WALL, PASS, PASS, PASS, WALL, WALL, WALL, PASS, PASS, PASS, WALL},
    {WALL, WALL, WALL, PASS, PASS, PASS, PASS, PASS, PASS, PASS, WALL},
    {WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL}
    };

    /*
    * 范例,失败迷宫
    */
    int mg2[MAP_Y][MAP_X] = {
    {WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL},/*一行*/
    {WALL, PASS, PASS, PASS, PASS, PASS, WALL, WALL, WALL, WALL, WALL},
    {WALL, WALL, WALL, WALL, WALL, PASS, WALL, PASS, WALL, WALL, WALL},
    {WALL, WALL, WALL, WALL, WALL, PASS, WALL, WALL, WALL, WALL, WALL},
    {WALL, PASS, WALL, PASS, WALL, PASS, WALL, PASS, PASS, PASS, WALL},
    {WALL, PASS, WALL, PASS, WALL, PASS, WALL, PASS, WALL, PASS, WALL},
    {WALL, PASS, PASS, PASS, WALL, PASS, WALL, PASS, WALL, PASS, WALL},
    {WALL, PASS, WALL, WALL, WALL, PASS, WALL, PASS, WALL, PASS, WALL},
    {WALL, PASS, WALL, PASS, PASS, PASS, WALL, PASS, WALL, PASS, WALL},
    {WALL, PASS, PASS, PASS, WALL, WALL, WALL, PASS, PASS, WALL, WALL},
    {WALL, WALL, WALL, PASS, PASS, PASS, PASS, PASS, WALL, PASS, WALL},
    {WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL}
    };

    /*
    * 打印迷宫
    * w=11,h=12
    */
    void display(int m[MAP_Y][MAP_X],int w,int h)
    {
    int i,j;
    /*一层层打印*/
    for(i=0; i<h; i++) {
    /*一列列打印*/
    for(j=0; j<w; j++) {
    if(m[i][j]==WALL) {
    /*draw wall*/
    printf("");//
    } else if(m[i][j]==FOOT) {
    /*draw foot*/
    printf("");
    } else if(m[i][j]==PASS) {
    /*draw road*/
    printf("");
    } else {
    /*标记过的路径*/
    printf("TT");
    }
    }
    printf("\n");
    }
    }

    /*
    * 迷宫的试探,使用递归实现。
    *
    */
    int visit(int m[MAP_Y][MAP_X],int x,int y)
    {
    m[x][y]=FOOT;

    /*当前位置到达end点,探路成功!*/
    if( (x == end[0]) && (y == end[1]) ) {
    finish=1;
    }

    /*往右走,若通路,向右一步;若不通,继续*/
    if(!finish&&m[x][y+1]==PASS) {
    printf("右{%d %d} ",x,y+1);
    visit(m,x,y+1);
    }
    /*往左探路*/
    if(!finish&&m[x][y-1]==PASS) {
    printf("左{%d %d} ",x,y-1);
    visit(m,x,y-1);
    }
    /*往下探路*/
    if(!finish&&m[x+1][y]==PASS) {
    printf("下{%d %d} ",x+1,y);
    visit(m,x+1,y);
    }

    /*往上探路*/
    if(!finish&&m[x-1][y]==PASS) {
    printf("上{%d %d} ",x-1,y);
    visit(m,x-1,y);
    }
    if(finish!=1) {
    m[x][y]=PASS;
    }

    return finish;
    }

    /*
    * 迷宫的试探,使用非递归实现。
    *
    */

    int visit2(int m[MAP_Y][MAP_X])
    {
    stack<pos> mystack;

    /*当前位置设置*/
    pos cur_pos;
    cur_pos.x = start[0];
    cur_pos.y = start[1];

    do{
    if ((cur_pos.x == end[1]) && (cur_pos.y == end[0]))
    {
    mystack.push(cur_pos);
    break;
    }

    /*往右走,若通路,向右一步;若不通,继续*/
    if(m[cur_pos.y][cur_pos.x+1]==PASS) {
    //printf("右{%d %d} ",cur_pos.y,cur_pos.x+1);
    /*当前点压栈,设置当前点为右边邻接点*/
    m[cur_pos.y][cur_pos.x] = MARK;//标记为已走过
    mystack.push(cur_pos);
    cur_pos.x = cur_pos.x+1;
    continue;
    }

    /*往左探路*/
    if(m[cur_pos.y][cur_pos.x-1]==PASS) {
    //printf("左{%d %d} ",cur_pos.y,cur_pos.x-1);
    /*当前点压栈,设置当前点为左边邻接点*/
    m[cur_pos.y][cur_pos.x] = MARK;//标记为已走过
    mystack.push(cur_pos);
    cur_pos.x = cur_pos.x-1;
    continue;
    }

    /*往下探路*/
    if(m[cur_pos.y+1][cur_pos.x]==PASS) {
    //printf("下{%d %d} ",cur_pos.y+1,cur_pos.x);
    /*当前点压栈,设置当前点为下边邻接点*/
    m[cur_pos.y][cur_pos.x] = MARK;//标记为已走过
    mystack.push(cur_pos);
    cur_pos.y = cur_pos.y+1;
    continue;
    }

    /*往上探路*/
    if(m[cur_pos.y-1][cur_pos.x]==PASS) {
    //printf("上{%d %d} ",cur_pos.y-1,cur_pos.x);
    /*当前点压栈,设置当前点为上边邻接点*/
    m[cur_pos.y][cur_pos.x] = MARK;//标记为已走过
    mystack.push(cur_pos);
    cur_pos.y = cur_pos.y-1;
    continue;
    }

    /*周围无可走之路,回退*/
    if(mystack.size() > 0){
    m[cur_pos.y][cur_pos.x] = MARK;//标记为已走过
    cur_pos = mystack.top();
    mystack.pop();
    //getchar();
    continue;
    }else{
    /*无法回退,搜索失败*/
    return -1;
    }
    }while (!mystack.empty());

    if(mystack.empty()){
    return -1;
    }

    /*探寻成功,设置路径*/
    while (!mystack.empty())
    {
    cur_pos = mystack.top();
    m[cur_pos.y][cur_pos.x] = FOOT;//标记为已走过
    mystack.pop();
    }

    return 0;
    }

    int main()
    {
    /*使用递归方法*/
    //if(visit(m,start[0],start[1])==0) {
    /*使用堆栈方法*/
    printf("--------------------地图1-------------------\n\n");
    display(m,MAP_X,MAP_Y);
    if(visit2(m) < 0){
    printf("\n没通路!");
    //display(m,MAP_X,MAP_Y);
    } else {
    printf("\n找到出口!\n\n");
    display(m,MAP_X,MAP_Y);
    }

    printf("\n--------------------地图2-------------------\n\n");
    display(mg2,MAP_X,MAP_Y);
    if(visit2(mg2) < 0){
    printf("\n没通路!");
    //display(mg2,MAP_X,MAP_Y);
    } else {
    printf("\n找到出口!\n\n");
    display(mg2,MAP_X,MAP_Y);
    }

    system("pause > NULL");
    return 0;
    }



  • 相关阅读:
    Windows 上如何安装Sqlite
    StringBuffer类讲解
    Android Debug Bridge(adb)百度
    0117调试 (Logout调试)
    android cmd adb shell 出现 Device offline的状态下
    菜鸟:为什么要用不同的包 android
    0105Activity+Intent
    adb shell 后感
    0116sqlite
    iptables
  • 原文地址:https://www.cnblogs.com/yixiaoyang/p/2208297.html
Copyright © 2020-2023  润新知