走迷宫
2016-06-26 06:41
253 查看
走迷宫,注意那两个getchar,第二个getchar是为了接受换行符。
#include <stdio.h> #include <stdlib.h> void show(int a[10][10]) { printf("----------------------------\n"); for (int i = 0; i < 10;i++) { for (int j = 0; j < 10;j++) { printf("%3d", a[i][j]); } printf("\n"); } } void main() { int a[10][10] = { {, 0, 0, 2, 0, 0, 0,0,0,0 }, { 0, 0, 2, 0 , 0, 0, 0, 0, 0,0 }, { 0, 0, 2, 2, 2, 0, 0, 0, 0, 0 }, { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0 }, { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 }, }; show(a); int x, y; x = 0; y = 0; a[x][y] = 1; show(a); while (1) { char ch = getchar(); getchar();//回车 switch (ch) { case 'a': if (y - 1 >= 0 && a[x][y - 1]!=2) { int temp = a[x][y - 1]; a[x][y - 1] = a[x][y]; a[x][y] = temp; y = y - 1; } break; case 'd': if (y + 1 <= 9 && a[x][y + 1] != 2) { int temp = a[x][y +1]; a[x][y + 1] = a[x][y]; a[x][y] = temp; y = y + 1; } break; case 'w': if (x - 1 >= 0 && a[x-1][y ] != 2) { int temp = a[x-1][y]; a[x-1][y ] = a[x][y]; a[x][y] = temp; x-=1; } break; case 's': if (x + 1 <= 9 && a[x + 1][y] != 2) { int temp = a[x + 1][y]; a[x + 1][y] = a[x][y]; a[x][y] = temp; x += 1; } break; default: break; } show(a); } system("pause"); }
相关文章推荐
- C++根据文本建立索引
- Leetcode #143 in cpp
- 欢迎使用CSDN-markdown编辑器
- 算法复杂度速查表
- 勾股定理一日一证连载34
- HTML5简介
- ## 暑期 后台学习+ C++ Primer 目录
- What an engineer can take away from Velocity 2016
- #leetcode #142 in cpp
- Matlab 代码注释
- 我的师傅叫追逐
- 从操作系统设计理念的变化看移动互联网对IT技术发展的影响
- Regional 做题记录 (49/50)
- Linux常用命令(三)
- Android之ActivityManage长用方法总结
- Unity Linux
- 使用JavaConfig方式-Spring 基础学习
- This implementaion is not part of the windows platform fips validated cryptographic algorithms.
- 集合类(并集、交集、差集操作)
- 使用Chartjs