欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

标题 穿越雷区 java_【蓝桥杯】穿越雷区-java语言描述

发布时间:2025/4/5 71 豆豆
生活随笔 收集整理的这篇文章主要介绍了 标题 穿越雷区 java_【蓝桥杯】穿越雷区-java语言描述 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

标题:穿越雷区X星的坦克战车很奇怪,它必须交替地穿越正能量辐射区和负能量辐射区才能保持正常运转,否则将报废。 某坦克需要从A区到B区去(A,B区本身是安全区,没有正能量或负能量特征),怎样走才能路径最短?已知的地图是一个方阵,上面用字母标出了A,B区,其它区都标了正号或负号分别表示正负能量辐射区。 例如: A + - + - - + - - + - + + + - + - + - + B + - + -坦克车只能水平或垂直方向上移动到相邻的区。数据格式要求:输入第一行是一个整数n,表示方阵的大小, 4<=n<100 接下来是n行,每行有n个数据,可能是A,B,+,-中的某一个,中间用空格分开。 A,B都只出现一次。要求输出一个整数,表示坦克从A区到B区的最少移动步数。 如果没有方案,则输出-1例如: 用户输入: 5 A + - + - - + - - + - + + + - + - + - + B + - + -则程序应该输出: 10资源约定: 峰值内存消耗(含虚拟机) < 512M CPU消耗

< 2000ms请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。 注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。 注意:主类的名字必须是:Main,否则按无效代码处理。import java.util.Scanner;public class Main {

public static int xA, yA, xB, yB;// 记录 A和 B的坐标

public static char[][] map;

public static int[][] book;

public static int n;

private static int stepMin = Integer.MAX_VALUE;

public static int[][] next = new int[][] { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

n = sc.nextInt();

map = new char[n][n];

book = new int[n][n];

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

char temp = sc.next().charAt(0);

if (temp == '+' || temp == '-') {

map[i][j] = temp;

} else if (temp == 'A') {

xA = i;

yA = j;

map[i][j] = 'A';

} else if (temp == 'B') {

xB = i;

yB = j;

map[i][j] = 'B';

}

}

}

book[xA][yA] = 1;

dfs(xA, yA, '0', 0);

System.out.println(stepMin);

}

private static void dfs(int x, int y, char ch, int step) {

// TODO Auto-generated method stub

if (x == xB && y == yB) {

if (stepMin > step) {

stepMin = step;

}

return;

}

int tx, ty;

for (int i = 0; i <= 3; i++) {

tx = x + next[i][0];

ty = y + next[i][1];

if (tx < 0 || ty < 0 || ty >= n || tx >= n) {

continue;

}

if (book[tx][ty] == 0 && map[tx][ty] != ch) {

book[tx][ty] = 1;

dfs(tx, ty, map[tx][ty], step + 1);

book[tx][ty] = 0;

}

}

}}

$(function () {

$('pre.prettyprint code').each(function () {

var lines = $(this).text().split('\n').length;

var $numbering = $('

$(this).addClass('has-numbering').parent().append($numbering);

for (i = 1; i <= lines; i++) {

$numbering.append($('

').text(i));

};

$numbering.fadeIn(1700);

});

});

总结

以上是生活随笔为你收集整理的标题 穿越雷区 java_【蓝桥杯】穿越雷区-java语言描述的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。