A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yll 中级黑马   /  2015-11-22 19:59  /  1576 人查看  /  18 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


#include <stdio.h>
#define COL 6
#define ROW 6

void printMap(char map[ROW][COL]){

    for (int i=0; i<ROW; i++) {
        for (int j=0; j<COL; j++) {
            printf("%c",map[i][j]);
        }
        printf("\n");
    }

}

void personMove(char map[ROW][COL],int oldX,int oldY,int newX,int newY){
   
    char temp;
    temp = map[oldX][oldY];
    map[oldX][oldY]=map[newX][newY];
    map[newX][newY]=temp;
}
int main(int argc, const char * argv[]) {
    //定义地图
    char map[ROW][COL]={
        {'#','#','#','#','#','#'},
        {'#','0','#','#',' ',' '},
        {'#',' ','#','#',' ','#'},
        {'#',' ',' ','#',' ','#'},
        {'#','#',' ',' ',' ','#'},
        {'#','#','#','#','#','#'},
        
    };
    //保存用户输入的方向
    char derection;
    //定义变量保存小人当前的位置
    int currentX=1;
    int currentY=1;
    //定义变量 保存路
    char street=' ';
   
   
    //先 打印一遍地图
    printMap(map);
    printf("请控制小人移动:w.上  s.下  a.左  d.右 q.退出\n" );
   

    while (1) {
        scanf("%c",&derection);
        getchar();//scanf("%c",&ch);//吸收多余的\n
        switch (derection) {
            case 'w':
            case 'W':
                if (map[currentX-1][currentY]==street) {
                    personMove(map,currentX,currentY,currentX-1,currentY);
                    currentX--;
                }
                break;
            case 's':
            case 'S':
                if (map[currentX+1][currentY]==street) {
                    personMove(map,currentX,currentY,currentX+1,currentY);
                    currentX++;
                }
               
                break;
            case 'a':
            case 'A':
                if (map[currentX][currentY-1]==street) {
                   personMove(map,currentX,currentY,currentX,currentY-1);
                    currentY--;
                }
               
                break;
            case 'd':
            case 'D':
               
                if (map[currentX][currentY+1]==street) {
                   personMove(map,currentX,currentY,currentX,currentY+1);
                    currentY++;
                }
                break;
            case 'q':
            case 'Q':
                return 0;
                break;
               
            default:
                break;
        }
        
        //重绘地图
        printMap(map);
        
        
        
        if (currentY==5) {
            printf("恭喜你!走出迷宫~(づ ̄ 3 ̄)づ\n");
            break;
        }
        
        
        
    }
    return 0;
}

18 个回复

倒序浏览
yll 中级黑马 2015-11-22 20:00:57
沙发
迷宫 伪代码



    /*
     ****************  定义变量  *******************
     1 定义变量,地图,存储用户输入的方向,小人的位置
     2 先打印一遍地图
     3 提示用户游戏玩法
     
     
     
     *************  要进行循环控制   *****************
     4 接受用户输入的方向
     5 判断用户输入了什么方向
     
     
     
     ***********   判断小人能否移动  *****************
     6 判断小人是否能移动  核心是要知道小人的下一位置是否是路
                        是路 让小人开始移动 移动的核心:小人和路进行交换
                             重新记录小人的当前位置
                        不识路 什么也不干
     
     
     
     ************  判断是否走出来   ******************
     判断y的值是否==5
        提示 走出迷宫了
        break  游戏要结束
     
    */
回复 使用道具 举报
明天自习的时候试试哈。谢谢楼主。
回复 使用道具 举报
yll 中级黑马 2015-11-23 08:45:41
板凳
刘邓诏初 发表于 2015-11-22 21:41
明天自习的时候试试哈。谢谢楼主。

0.0我还是新手 ~ 请指教`~
回复 使用道具 举报
好高级阿
回复 使用道具 举报
楼主威武啊!
回复 使用道具 举报
MrK 初级黑马 2015-11-23 22:31:16
7#
路过 帮顶。。。。。。。
回复 使用道具 举报
这么6啊
回复 使用道具 举报
Mr.Yan 中级黑马 2015-11-23 23:32:57
9#
很多函数还并没有学到,只能默默地膜拜,等我长成之日再回首!{:3_63:}
回复 使用道具 举报
额,这迷宫还是比较简单的。
回复 使用道具 举报
yll 中级黑马 2015-12-4 09:50:43
11#




/*
折半查找  实在一个有序的数组中 查找



*/

#include <stdio.h>

int searchItem(int arr[],int len,int key){

    //定义变量
   
    int low=0,high=len,mid;
   
    //循环
    while (low<=high) {
      
   
   
   
    //计算mid的位置
     mid = (low+high)/2;
   
    //判断key  a[mid]
        if (key>arr[mid]) {
              //key>a[mid]   low=mid+1
            low = mid+1;
        }else if (key<arr[mid]){
          //key<a[mid]   high=mid-1
            high = mid-1;
        }else{
            //key==a[mid]  return mid;

            return mid;
        }
   
  
   
   
    }
    return -1;
}



int insertItemLoc(int arr[],int len,int key){
   
    //定义变量
   
    int low=0,high=len,mid;
   
    //循环
    while (low<=high) {
        
        
        
        
        //计算mid的位置
        mid = (low+high)/2;
        
        //判断key  a[mid]
        if (key>arr[mid]) {
            //key>a[mid]   low=mid+1
            low = mid+1;
        }else if (key<arr[mid]){
            //key<a[mid]   high=mid-1
            high = mid-1;
        }else{
            //key==a[mid]  return mid;
            
            return mid+1;
        }
        
        
        
        
    }
    return low;
}

int main(int argc, const char * argv[]) {
    int a[]={3,4,12,20,21,23,28,45,67,100};
    //查找20 key= 20
//    int location = searchItem(a, 10, 3);
//    printf("location=%d\n",location);

    int loc = insertItemLoc(a, 10, 898989);
    printf("loc=%d\n",loc);
   
   
   
    return 0;
}
回复 使用道具 举报
yll 中级黑马 2015-12-4 09:52:58
12#
构建二维数组,并且遍历 显示



#include <stdio.h>

int main(int argc, const char * argv[]) {
    int score[5][3]={{80,75,92},{61,65,71},{59,63,70},{85,87,90},{76,77,85}};
    for (int i=0; i<5; i++) {
        for (int j=0; j<3; j++) {
            printf("%d\t",score[i][j]);
        }
        printf("\n");
    }
   
   
    return 0;
}
回复 使用道具 举报
楼主是妹子
回复 使用道具 举报
这个迷宫挺好的
回复 使用道具 举报
666666      
回复 使用道具 举报
好高端的感觉
回复 使用道具 举报
6666666
回复 使用道具 举报
哈哈哈,不错,我来学习一下
回复 使用道具 举报
哈哈哈,不错,我来学习一下
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马