• 迷宫


    #include <stdio.h>
    
    void print(char cs[6][6])
    {
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                printf("%c", cs[i][j]);
            }
            printf("
    ");
        }
    }
    
    int isSuccess(int x, int y)
    {
        if (x == 1 && y == 5) {
            return 1;
        }
        return 0;
    }
    
    int main(int argc, const char * argv[]) {
        
        
        
        char cs[6][6] = {{'#','#','#','#','#','#'},
                         {'#','O',' ','#',' ',' '},
                         {'#',' ','#','#',' ','#'},
                         {'#',' ',' ','#',' ','#'},
                         {'#','#',' ',' ',' ','#'},
                         {'#','#','#','#','#','#'}};
        print(cs);
        
        char c;
        
        int x = 1, y = 1;
        
        while (1) {
            int flag = 0;
            printf("请输入方向(上w,下s,左a,右d)
    ");
            scanf("%c", &c);
            getchar();
            
            if (c != 'w' && c != 's' && c != 'a' && c != 'd') {
                printf("输入错误!
    ");
                continue;
            }
            
            switch (c) {
                case 'w':  // 上
                    if (cs[x-1][y] == ' ') {
                        cs[x-1][y] = cs[x][y];
                        cs[x][y] = ' ';
                        x--;
                        print(cs);
                        flag = isSuccess(x,y);
                    }else{
                        printf("此路不通w!!
    ");
                    }
                    break;
                case 's': // 下
                    if (cs[x+1][y] == ' ') {
                        cs[x+1][y] = cs[x][y];
                        cs[x][y] = ' ';
                        x++;
                        print(cs);
                        flag = isSuccess(x,y);
                    }else{
                        printf("此路不通s!!
    ");
                    }
                    break;
                case 'a': //左
                    if (cs[x][y-1] == ' ') {
                        cs[x][y-1] = cs[x][y];
                        cs[x][y] = ' ';
                        y--;
                        print(cs);
                        flag = isSuccess(x,y);
                    }else{
                        printf("此路不通a!!
    ");
                    }
                    break;
                case 'd': // 右
                    if (cs[x][y+1] == ' ') {
                        cs[x][y+1] = cs[x][y];
                        cs[x][y] = ' ';
                        y++;
                        print(cs);
                        flag = isSuccess(x,y);
                    }else{
                        printf("此路不通d!!
    ");
                    }
                    break;
                    
                default:
                    break;
            }
            
            if (flag) {
                printf("恭喜你走出迷宫!!");
                break;
            }
        }
        
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Python操作MySQL数据库
    Spark学习--Structured Streaming
    YumRepo Error: All mirror URLs are not using ftp, http[s] or file.
    Spark学习--SparkStreaming
    Spark学习--SparkSQL04
    Spark学习--SparkSQL03
    Spark学习--SparkSQL02
    Bootstrap简介
    Linux Shell系列教程之(一)Shell简介
    Linux命令之必须掌握的十条命令
  • 原文地址:https://www.cnblogs.com/liuqblog/p/4896280.html
Copyright © 2020-2023  润新知