日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/4/5 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 标题 穿越雷区 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语言描述的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。