#华为机考# 求华为机试 7.13 题解
全部评论
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Third { static int M, N, minDist = Integer.MAX_VALUE; static int[][] parArea; static int direc1[] = {-1, 1, 0, 0}, direc2[] = {0, 0, 1, -1}; static List<Integer[]> res = null; public static void main(String[] args) { Scanner cin = new Scanner(System.in); M = cin.nextInt(); N = cin.nextInt(); int i1 = cin.nextInt(); int j1 = cin.nextInt(); parArea = new int[M + 1][N + 1]; boolean[][] visited = new boolean[M + 1][N + 1]; for (int i = 1; i < M + 1; i++) { for (int j = 1; j < N + 1; j++) { parArea[i][j] = cin.nextInt(); } } List<Integer[]> path = new ArrayList<>(); dfs(i1, j1, path, 1, visited); if (res == null) { System.out.print(-1 + " " + -1); return; } for (int i = 0; i < res.size(); i++) { System.out.print(res.get(i)[0] + " " + res.get(i)[1]); if (i < res.size() - 1) System.out.print(" "); } } public static void dfs(int curI, int curJ, List<Integer[]> path, int dist, boolean[][] visited) { if (parArea[curI][curJ] == 3 || parArea[curI][curJ] == 2) return; if (parArea[curI][curJ] == 1) { path.add(new Integer[]{curI, curJ}); if (dist < minDist) { res = new ArrayList<>(path); minDist = dist; }else if (dist == minDist && path.get(path.size() - 1)[0] < res.get(res.size() - 1)[0]) { res = new ArrayList<>(path); } path.remove(path.size() - 1); return; } visited[curI][curJ] = true; path.add(new Integer[]{curI, curJ}); for (int i = 0; i < 4; i++) { int nextI = curI + direc1[i], nextJ = curJ +direc2[i]; if (nextI >= 1 && nextI <= M && nextJ >= 1 && nextJ <= N && !visited[nextI][nextJ]) { if ((i == 2 || i == 3) dfs(nextI, nextJ, path, dist + 1, visited); } } path.remove(path.size() - 1); visited[curI][curJ] = false; } }
1 回复 分享
发布于 2022-07-14 16:51
import java.util.*; public class First { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); int[] weight = new int[n]; int[] time = new int[n]; for (int i = 0; i < n; i++) { weight[i] = cin.nextInt(); } for (int i = 0; i < n; i++) { time[i] = cin.nextInt(); } int[] a = {100, 80, 60}; int index = 0; Map<Integer, List<Integer[]>> map = new HashMap<>(); for (int j = 0; j <= 60; j++) { if (index < time.length && j != time[index]) continue; if (map.containsKey(j)) { List<Integer[]> tmpM = map.get(j); for (Integer[] is: tmpM) a[is[1]] += weight[is[0]]; map.remove(j); } while (index < time.length && j >= time[index]) { if (a[0] >= weight[index]) { a[0] -= weight[index]; List<Integer[]> tmpM = map.get(30 + time[index]); if (tmpM == null) { List<Integer[]> list = new ArrayList<>(); list.add(new Integer[]{index, 0}); map.put(30 + time[index], list); }else{ tmpM.add(new Integer[]{index, 0}); } index++; } else if (a[1] >= weight[index]) { a[1] -= weight[index]; List<Integer[]> tmpM = map.get(30 + time[index]); if (tmpM == null) { List<Integer[]> list = new ArrayList<>(); list.add(new Integer[]{index, 1}); map.put(30 + time[index], list); }else{ tmpM.add(new Integer[]{index, 1}); } index++; } else if (a[2] >= weight[index]) { a[2] -= weight[index]; List<Integer[]> tmpM = map.get(30 + time[index]); if (tmpM == null) { List<Integer[]> list = new ArrayList<>(); list.add(new Integer[]{index, 2}); map.put(30 + time[index], list); }else{ tmpM.add(new Integer[]{index, 2}); } index++; } else { time[index]++; int tmp = index + 1; while (tmp < time.length && time[tmp] <= time[index]) { time[tmp] = time[index]; tmp++; } } } } int[] b = {100 - a[0], 80 - a[1], 60 - a[2]}; System.out.println(b[0] + " " + b[1] + " " + b[2]); } }
1 回复 分享
发布于 2022-07-14 20:46
兄弟华为秋招笔试已经开始了嘛?
1 回复 分享
发布于 2022-07-14 01:59
华为机试_笔经面经_牛客网 https://www.nowcoder.com/discuss/985083
点赞 回复 分享
发布于 2022-07-14 15:57

相关推荐

04-11 21:31
四川大学 Java
野猪不是猪🐗:(ja)va学弟这招太狠了
点赞 评论 收藏
分享
头像
04-17 09:29
已编辑
湖南农业大学 后端
睡姿决定发型丫:本硕末9也是0offer,简历挂了挺多,只有淘天 美团 中兴给了面试机会,淘天二面挂,美团一面kpi面,中兴一面感觉也大概率kpi(虽然国企,但一面0技术纯聊天有点离谱吧)
点赞 评论 收藏
分享
评论
2
4
分享

创作者周榜

更多
牛客网
牛客企业服务