Aha Tim ChaiがJava1813に挑戦。迷路の3方向の動きを体験

アハティムチャイチャレンジ

1813.迷路を歩く3つの方向への移動

ここに画像の説明を挿入
ここに画像の説明を挿入

import java.util.Scanner;

/**
 * @author yinglongwu
 */
//1813. 走迷宫3-四向移动
public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		char[][] map = new char[][]{
			{'#','#','#','#','#','#'},
			{'#','O',' ','#',' ','E'},
			{'#',' ','#','#',' ','#'},
			{'#',' ',' ','#',' ','#'},
			{'#','#',' ',' ',' ','#'},
			{'#','#','#','#','#','#'},
		};
		int Ox = 1,Oy = 1;
		String step = scanner.next();
		char[] stepArr = step.toCharArray();
		for (int i = 0; i < stepArr.length; i++) {
			switch (stepArr[i]) {
			case 'w'://向上移动
				if (map[Ox-1][Oy]!='#') {
					map[Ox][Oy] = ' ';
					map[Ox-1][Oy] = 'O';
					Ox = Ox-1;
				}
				break;
			case 'a'://向左移动
				if (map[Ox][Oy-1]!='#') {
					map[Ox][Oy] = ' ';
					map[Ox][Oy-1] = 'O';
					Oy = Oy-1;
				}
				break;
			case 's'://向下移动
				if (map[Ox+1][Oy]!='#') {
					map[Ox][Oy] = ' ';
					map[Ox+1][Oy] = 'O';
					Ox = Ox+1;
				}
				break;
			case 'd'://向右移动
				if (map[Ox][Oy+1]!='#') {
					map[Ox][Oy] = ' ';
					map[Ox][Oy+1] = 'O';
					Oy = Oy+1;
				}
				break;
			default:
				break;
			}
		}
		
		//char类型数组的输出比较神奇,可以直接用数组名打印
		for (int i = 0; i < map.length; i++) {
			System.out.println(map[i]);
		}
	}

}

ここに画像の説明を挿入

318の元の記事を公開 44のような 訪問者20,000以上

おすすめ

転載: blog.csdn.net/qq_43594119/article/details/105150398