微众银行9.23

请问第二道题怎么做呀,看题目都有点看不大懂。

#微众银行#
全部评论
如果是一样的话 就模拟就好了 一个二维数组然后遍历指令去行走 就和机器人走迷宫的题类似
点赞 回复 分享
发布于 2021-09-23 22:38
ort java.util.*; /* * 第一行四个整数 n, m, x, y,(0 < n, m <= 100,1 <= x, y <= 100000) 后面 n 行,每行 m 个数,形成一个方阵,表示各个格子位置的阻力数值,如果为 -1,表示该位置无法通过。 * 各个位置的数值范围为 [-1, 100000]。 最后一行一个字符串,由 hjkl 四种字母组成,表示指令序列。h 表示向左移动一格,j 表示向下移动一格, * k 表示向上移动一格,l 表示向右移动一格。字符串长度不超过 100000。 * * */ public class Main2 { public static int n,m,x,y; public static String step; public static int[][] map; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); m = scanner.nextInt(); x = scanner.nextInt(); y = scanner.nextInt(); map = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) map[i][j] = scanner.nextInt(); step = scanner.nextLine(); step = scanner.nextLine(); System.out.println(walk()); } public static int walk(){ int ans = 0; int[] pos = new int[]{0,0}; //位置 x y for (int i = 0; i < step.length(); i++){ // 第一步不改变方向 if (i == 0){ }else { if (step.charAt(i) == step.charAt(i - 1)){ //不改变方向 }else { ans += x; }//改变方向 } ans += next(pos,step.charAt(i)); } return ans; } public static int next(int[] pos,char des){ int[] tmp = new int[]{pos[0],pos[1]}; if (des == 'h'){ pos[0] -= 1; }else if (des == 'j') pos[1] += 1; else if (des == 'k') pos[1] -= 1; else if (des == 'l') pos[0] += 1; if (pos[0] <0 || pos[1] <0 || pos[0] >=m || pos[1] >=n || map[pos[1]][pos[0]] == -1){ pos[0] = tmp[0]; pos[1] = tmp[1]; return y; }else { return Math.max(map[tmp[1]][tmp[0]],map[pos[1]][pos[0]]); }
点赞 回复 分享
发布于 2021-09-23 22:40

相关推荐

评论
点赞
3
分享

创作者周榜

更多
牛客网
牛客企业服务