二维数组迷宫趣题
Posted Richard_i
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二维数组迷宫趣题相关的知识,希望对你有一定的参考价值。
import java.util.Scanner;
/**
* A代表人物
* 核心思路,交换元素下标位置,走出迷宫
*/
public class TwoDimensionalMap
public static void main(String[] args)
char[][] map=
'*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*',,
'*','|','|','|','|','|','|','*','|','|','|','|','|','|','|','|','*','|','|','|','*',,
'*','|','*','*','*','*','|','*','*','*','*','*','*','|','|','|','*','|','*','|','*',,
'*','|','*','*','|','|','|','|','|','*','*','*','*','*','*','|','*','|','*','|','*',,
'*','|','*','*','|','*','|','*','*','*','|','|','|','|','|','|','|','*','*','|','*',,
'*','|','*','*','|','*','|','*','*','*','*','*','*','*','*','*','|','*','*','|','*',,
'*','|','*','*','|','*','|','*','|','|','|','|','|','|','|','*','|','*','*','|','*',,
'*','|','*','|','|','*','|','*','|','*','*','*','*','*','|','*','|','*','*','|','*',,
'*','|','*','|','*','*','|','*','|','*','*','*','*','*','|','*','|','*','*','|','*',,
'*','|','*','|','*','*','|','*','|','|','|','|','*','*','|','*','|','*','*','|','*',,
'*','|','*','|','*','*','|','*','*','*','*','|','*','*','|','*','|','|','*','|','*',,
'*','|','*','|','*','*','|','*','*','*','*','|','*','*','|','*','|','|','*','|','*',,
'*','|','*','|','|','|','|','|','|','|','|','|','*','*','|','*','*','|','*','|','*',,
'*','|','*','*','*','*','|','*','*','*','*','*','*','*','|','*','*','|','*','|','*',,
'*','|','*','*','*','*','|','*','*','*','*','*','*','*','|','*','|','|','*','|','*',,
'*','|','|','|','*','*','|','*','*','*','*','|','|','|','|','*','|','|','|','|','*',,
'*','*','*','|','*','*','*','*','*','*','*','|','*','*','|','|','|','|','*','|','*',,
'*','*','*','|','|','|','|','|','|','*','*','|','*','*','*','*','*','|','*','|','*',,
'*','*','*','*','*','*','*','*','|','*','*','|','*','*','*','*','*','|','*','|','*',,
'*','*','*','|','|','|','|','|','|','*','*','|','*','*','*','*','*','|','*','|','*',,
'*','*','*','|','*','*','*','*','*','*','|','|','|','|','|','*','*','|','*','|','*',,
'*','*','*','|','*','|','|','|','|','|','|','*','*','*','|','|','|','|','*','|','*',,
'*','*','*','|','*','*','*','*','*','*','*','*','*','*','*','*','*','*','*','A','*',
;
// System.out.println(map.length); // 行 23
// System.out.println(map[0].length); // 列 21列
// 人物A的索引
char userA;
int startX = 22;
int startY = 19;
// System.out.println("人物" + map[startX][startY]);
// 出口索引
int endX = 22;
int endY = 3;
// 遍历地图
for (int i = 0; i < map.length ; i++)
for (int j = 0; j < map[i].length; j++)
System.out.print(map[i][j] + " ");
System.out.println();
while (true)
Scanner scanner = new Scanner(System.in);
System.out.println("请通过WASD键位,移动人物走出迷宫");
// 返回 char指定索引处的值。 意为用户输入的首字母
char user = scanner.next().charAt(0);
switch (user)
// 向上
case 'w': case 'W':
/*
人物A往上则startX-1,判断是否为*(墙)
交换数组下标位置,完成移动
*/
if (map[startX-1][startY] != '*')
userA = map[startX][startY];
map[startX][startY] = map[startX-1][startY];
map[startX-1][startY] = userA;
startX--;
break;
case 'a': case 'A':
// 向左
if (map[startX][startY-1] != '*')
userA = map[startX][startY];
map[startX][startY] = map[startX][startY-1];
map[startX][startY-1] = userA;
startY--;
break;
case 's': case 'S':
// 向下
/*
人物A往上则startX+1,往下有下标越界的风险,需要对其进行控制
startX+1 < map.length ,不能大于数组列数
交换数组下标位置,完成移动
*/
if ((startX+1) < map.length && map[startX+1][startY] != '*')
userA = map[startX][startY];
map[startX][startY] = map[startX+1][startY];
map[startX+1][startY] = userA;
startX++;
break;
case 'd': case 'D':
// 向左
if (map[startX][startY+1] != '*')
userA = map[startX][startY];
map[startX][startY] = map[startX][startY+1];
map[startX][startY+1] = userA;
startY++;
break;
//用户操作遍历地图,刷新地图显示人物当前位置
for (int i = 0; i < map.length ; i++)
for (int j = 0; j < map[i].length ; j++)
System.out.print(map[i][j] + " ");
System.out.println();
// 人物索引下标等于出口下标时,跳出循环,结束
if (startX == endX && startY == endY)
System.out.println("恭喜通过,即将进入下一关卡...");
break;
以上是关于二维数组迷宫趣题的主要内容,如果未能解决你的问题,请参考以下文章