迷宫问题

main.cpp
    #include "function.h"
    char map[10][10] =
    {
        //10行10列的数组地图
        {'#','#','#','#','#','#','#','#','#','#'},//0
        {'#','0',' ',' ',' ','#',' ','#',' ',' '},//1
        {'#','#',' ','#','#','#',' ','#',' ','#'},//2
        {'#','#',' ',' ',' ',' ',' ','#',' ','#'},//3
        {'#','#','#',' ','#','#',' ','#',' ','#'},//4
        {'#',' ','#',' ',' ','#',' ','#',' ','#'},//5
        {'#',' ','#',' ',' ','#',' ','#',' ','#'},//6
        {'#',' ','#',' ',' ','#',' ','#',' ','#'},//7
        {'#',' ',' ',' ',' ','#',' ',' ',' ','#'},//8
        {'#','#','#','#','#','#','#','#','#','#'},//9
        //0   1   2   3   4   5   6   7   8   9
    };
    int startX = 1;
    int startY = 1;
    int endX = 1;
    int endY = 9;
    char direction = '0';
    while (map[endX][endY] != '0') {
        printMap(map);
        scanf("\n%c", &direction);
        switch (direction) {
            case 'w':
                if (map[startX - 1][startY] == ' ') {
                    map[startX - 1][startY] = '0';
                    map[startX][startY] = ' ';
                    startX = startX - 1;
                } else {
                    printf("此路不通\n");
                }
                break;
            case 's':
                if (map[startX + 1][startY] == ' ') {
                    map[startX + 1][startY] = '0';
                    map[startX][startY] = ' ';
                    startX = startX + 1;
                } else {
                    printf("此路不通\n");
                }
                break;
            case 'a':
                if (map[startX][startY - 1] == ' ') {
                    map[startX][startY - 1] = '0';
                    map[startX][startY] = ' ';
                    startY = startY - 1;
                } else {
                    printf("此路不通\n");
                }
                break;
            case 'd':
                if (map[startX][startY + 1] == ' ') {
                    map[startX][startY + 1] = '0';
                    map[startX][startY] = ' ';
                    startY = startY + 1;
                } else {
                    printf("此路不通\n");
                }
                break;
            default:
                printf("请输入正确方向!!!");
                break;
        }

    }
    printf("恭喜,成功通过迷宫!!!\n");
function.cpp
void printMap( char ( *map)[10] ) {
    for (int i = 0; i < 10; i++) {
        for (int j = 0 ; j < 10; j++) {
            printf("%c", map[i][j]);
        }
        printf("\n");
    }
    printf("请按 wsad 进行上下左右方向的移动:\n");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值