第一题:

public static void main(String[] args) {Scanner scanner = new Scanner(System.in);while (scanner.hasNext()) {//输入int n = scanner.nextInt();scanner.nextLine();int row = scanner.nextInt();scanner.nextLine();//用于存储亲属关系的map<son-father>HashMap<Integer, Integer> relation = new HashMap<>();for (int i = 0; i < row; i++) {String s = scanner.nextLine();String[] split = s.split("\\s+");int father = Integer.parseInt(split[0]);for (int j = 1; j < split.length; j++) {int son = Integer.parseInt(split[j]);relation.put(son, father);}}int m1 = scanner.nextInt();int m2 = scanner.nextInt();List<Integer> m1List, m2List;m1List = new ArrayList<>();m2List = new ArrayList<>();getList(m1, relation, m1List);getList(m2, relation, m2List);int res = calculateDis(m1List, m2List);System.out.println(res);}}private static int calculateDis(List<Integer> m1List, List<Integer> m2List) {for (int i = 0; i < m1List.size(); i++) {Integer num1 = m1List.get(i);for (int j = 0; j < m2List.size(); j++) {Integer num2 = m2List.get(j);if(num1.equals(num2)){return i+j;}}}return -1;}private static void getList(int m, HashMap<Integer, Integer> relation, List<Integer> list) {list.add(m);if (relation.containsKey(m)) {getList(relation.get(m), relation, list);}}

第二题:

   static int[] dx = {0, -1, 0, 1};static int[] dy = {1, 0, -1, 0};static boolean[] direction = {false, true, false, true};//上下定义为true,横向定义为falseprivate static int getShortDis(char[][] grid, int[] start, int[] end) {int m = grid.length, n = grid[0].length;boolean[][] visited = new boolean[m][n];//按照距离升序排列PriorityQueue<Node> nodes = new PriorityQueue<>((a, b) -> {return a.dis - b.dis;});//添加初始节点nodes.add(new Node(start[0], start[1], 0, null));while (!nodes.isEmpty()) {Node poll = nodes.poll();//已经遍历过则跳过if (visited[poll.x][poll.y]) continue;visited[poll.x][poll.y] = true;//如果途中找到了结果直接返回if (poll.x == end[0] && poll.y == end[1]) return poll.dis;for (int i = 0; i < 4; i++) {int numSteps = 0;int x = poll.x+dx[i], y = poll.y+dy[i];//如果发生方向的转变需要额外增加1步if (poll.isUpDown != null && poll.isUpDown != direction[i]) {numSteps++;}//搜索目标结果while (x >= 0 && x  < m && y  >= 0 && y  < n && (grid[x ][y ] == 'B' || grid[x][y ] == 'E')) {numSteps++;//如果已访问则不参与排序if (!visited[x][y]) nodes.add(new Node(x, y, poll.dis + numSteps, i == 1 || i == 3));x+=dx[i];y+=dy[i];}}}return -1;}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);while (scanner.hasNext()) {int row = scanner.nextInt();int col = scanner.nextInt();scanner.nextLine();char[][] grid = new char[row][col];int[] start = new int[2];int[] end = new int[2];for (int i = 0; i < row; i++) {String s = scanner.nextLine();for (int j = 0; j < col; j++) {grid[i][j] = s.charAt(j);if (grid[i][j] == 'S') {start[0] = i;start[1] = j;} else if (grid[i][j] == 'E') {end[0] = i;end[1] = j;}}}System.out.println(getShortDis(grid, start, end));}}static class Node {int x, y, dis;Boolean isUpDown;public Node(int x, int y, int dis, Boolean isUpDown) {this.x = x;this.y = y;this.dis = dis;this.isUpDown = isUpDown;}}

第三题

   static int[] dx = {1, -1, 0, 0};static int[] dy = {0, 0, 1, -1};public static void main(String[] args) {Scanner scanner = new Scanner(System.in);while (scanner.hasNext()) {int m = scanner.nextInt();int n = scanner.nextInt();int begin_x = scanner.nextInt() - 1;int begin_y = scanner.nextInt() - 1;int[][] grid = new int[m][n];for (int i = 0; i < m; i++) {scanner.nextLine();for (int j = 0; j < n; j++) {grid[i][j] = scanner.nextInt();}}PriorityQueue<Node> queue = new PriorityQueue<>((a, b) -> {if (a.dis == b.dis) {if (a.x == b.x) {return a.y - b.y;}return a.x - b.x;}return a.dis - b.dis;});Node begin = new Node(begin_x, begin_y, 0);queue.add(begin);boolean[][] visited = new boolean[m][n];Direction[][] direction = new Direction[m][n];//记录方向的数组 0123分别表示上左右下while (!queue.isEmpty()) {Node poll = queue.poll();if (visited[poll.x][poll.y]) continue;if (grid[poll.x][poll.y] == 1 && (direction[poll.x][poll.y].y == poll.y)) {//只能从竖直方向进入Deque<Integer> path = getPath(direction, begin_x, begin_y, poll.x, poll.y);System.out.println(path.toString());return;}visited[poll.x][poll.y] = true;for (int i = 0; i < 4; i++) {//计算步数int numSteps = 0;int x = poll.x + dx[i], y = poll.y + dy[i];while (x >= 0 && y >= 0 && x < m && y < n && (grid[x][y] == 0 || grid[x][y] == 1)) {numSteps++;if(!visited[x][y]){direction[x][y] = new Direction(poll.x, poll.y);Node node = new Node(x, y, numSteps + poll.dis);queue.add(node);}x += dx[i];y += dy[i];}}}System.out.println(-1 + " " + -1);}}private static Deque<Integer> getPath(Direction[][] direction, int begin_x, int begin_y, int end_x, int end_y) {Deque<Integer> res = new LinkedList<>();int x = end_x, y = end_y;res.push(end_y + 1);res.push(end_x + 1);while (x != begin_x || y != begin_y) {Direction dir = direction[x][y];int cur_x = dir.x, cur_y = dir.y;while (x != cur_x || y != cur_y) {if (x < cur_x) {x++;} else if (y < cur_y) {y++;} else if (x > cur_x) {x--;} else {y--;}res.push(y + 1);res.push(x + 1);}x = dir.x;y = dir.y;}return res;}static class Node {int x, y, dis;public Node(int x, int y, int dis) {this.x = x;this.y = y;this.dis = dis;}}static class Direction {int x;int y;public Direction(int x, int y) {this.x = x;this.y = y;}}

9.21华为笔试第二题

X代表不能走,S代表驿站,可以切换为马或者兵(需要耗费1步),驿站代表可以切换为兵或者马,马走日字,兵走上下左右,.代表正常道路,问从0,0到(m-1,n-1)所使用的最小步数

static char[][] grid;static int m, n;static boolean[][] usedHorse;static boolean[][] usedSoldier;static int[][] horseDirect = new int[][]{{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {-2, 1}, {-2, -1}, {2, 1}, {2, -1}};static int[][] soldierDirect = new int[][]{{0, 1}, {0, -1}, {-1, 0}, {1, 0}};public static void main(String[] args) {Scanner scanner = new Scanner(System.in);while (scanner.hasNext()) {m = scanner.nextInt();n = scanner.nextInt();scanner.nextLine();grid = new char[m][n];for (int i = 0; i < m; i++) {String s = scanner.nextLine();for (int j = 0; j < n; j++) {grid[i][j] = s.charAt(j);}}usedHorse = new boolean[m][n];usedSoldier = new boolean[m][n];PriorityQueue<Node> nodes = new PriorityQueue<>((a, b) -> {return a.dis - b.dis;});if (grid[0][0] == 'S') {nodes.add(new Node(0, 0, 0, true));nodes.add(new Node(0, 0, 1, false));} else if (grid[0][0] == '.') {nodes.add(new Node(0, 0, 0, true));} else {System.out.println(-1);return;}while (!nodes.isEmpty()) {Node poll = nodes.poll();int curX = poll.x, curY = poll.y;boolean curState = poll.isSoldier;if (usedSoldier[curX][curY] && curState||(usedHorse[curX][curY]&&!curState)) continue;System.out.println("<"+curX+","+curY+">-->"+curState+"-->"+poll.dis);if (curState) {usedSoldier[curX][curY] = true;} else {usedHorse[curX][curY] = true;}if (curX == m - 1 && curY == n - 1) {System.out.println(poll.dis);return;}if (curState) {for (int[] dir : soldierDirect) {int step = 0;int x = dir[0] + curX, y = dir[1] + curY;while (isValid(x, y) && grid[x][y] != 'X') {step++;if (grid[x][y] == 'S' && !usedHorse[x][y]) {nodes.add(new Node(x, y, poll.dis + step + 1, false));}if (!usedSoldier[x][y]) nodes.add(new Node(x, y, poll.dis + step, true));x += dir[0];y += dir[1];}}} else {for (int[] dir : horseDirect) {int step = 0;int x = dir[0] + curX, y = dir[1] + curY;while (isValid(x, y) && grid[x][y] != 'X') {step++;if (grid[x][y] == 'S' && !usedSoldier[x][y]) {nodes.add(new Node(x, y, poll.dis + step + 1, true));}if (!usedHorse[x][y]) nodes.add(new Node(x, y, poll.dis + step, false));x += dir[0];y += dir[1];}}}}System.out.println(-1);}}static boolean isValid(int x, int y) {return x >= 0 && y >= 0 && x < m && y < n;}static class Node {int x, y, dis;boolean isSoldier;public Node(int m, int n, int dis, boolean isSoldier) {this.x = m;this.y = n;this.dis = dis;this.isSoldier = isSoldier;}}

测试输入

9 9
.........
.....XXX.
.....X.X.
.....X.X.
.....X.XS
XXXXXX.XX
.........
.........
.........
<0,0>-->true-->0
<0,1>-->true-->1
<1,0>-->true-->1
<0,2>-->true-->2
<1,1>-->true-->2
<2,0>-->true-->2
<1,2>-->true-->3
<2,1>-->true-->3
<3,0>-->true-->3
<0,3>-->true-->3
<0,4>-->true-->4
<2,2>-->true-->4
<1,3>-->true-->4
<3,1>-->true-->4
<4,0>-->true-->4
<2,3>-->true-->5
<1,4>-->true-->5
<3,2>-->true-->5
<4,1>-->true-->5
<0,5>-->true-->5
<4,2>-->true-->6
<2,4>-->true-->6
<3,3>-->true-->6
<0,6>-->true-->6
<3,4>-->true-->7
<4,3>-->true-->7
<0,7>-->true-->7
<4,4>-->true-->8
<0,8>-->true-->8
<1,8>-->true-->9
<2,8>-->true-->10
<3,8>-->true-->11
<4,8>-->true-->12
<4,8>-->false-->13
<5,6>-->false-->14
<3,6>-->false-->14
<6,7>-->false-->14
<8,6>-->false-->15
<2,4>-->false-->15
<7,7>-->false-->15
<7,5>-->false-->15
<4,4>-->false-->15
<2,8>-->false-->15
<6,4>-->false-->15
<6,8>-->false-->15
<4,6>-->false-->15
<8,8>-->false-->15
15

2022.9.7华为笔试相关推荐

  1. 2022校招:华为笔试

    三题120分钟,分值=100+200+300.需要自己处理输入. 华为是谁? 华为是全球领先的信息与通信技术( ICT)解决方案供应商,专注于 ICT 领域,坚持稳 健经营.持续创新.开放合作,在电信 ...

  2. 2022.04.20华为笔试

    严正声明:转载请注明出处!!! 总结一下做题的过程: 刚开始以为第一题很简单,因为忽略了一个很重要的条件,再去读题发现没注意到那个要点,然后开始重来,发现下不了笔,整个人都傻了.整个一大惨败,前一个小 ...

  3. Seeker的奇妙求职历险(华为笔试)

    矩阵报数 题目: 给出一个旋转矩阵,M行N列,左上角为(0,0),右下角为(M-1,N-1),从左上角开始计数,顺时针从外圈开始,外圈遍历完之后再遍历内圈,直到遍历完所有的点. 返回所有个位数为7且十 ...

  4. 华为 社招 C语言笔试,华为笔试C语言笔试题之3

    <华为笔试C语言笔试题之3>由会员分享,可在线阅读,更多相关<华为笔试C语言笔试题之3(10页珍藏版)>请在人人文库网上搜索. 1.C 语言笔试题之34. static 有什么 ...

  5. 2020.9.9华为笔试记忆:KMP+记忆化搜索+字典树

    2020.9.9华为笔试 当然,出现在我博客中的笔试都不是我自己的笔试(人家也不给我发笔试链接,小声bibi,诶,好像我也没投,hhhahahha 记者:为什么要做笔试? 我:生活无聊了喏,肯定要做啊 ...

  6. 2019华为笔试 找终点

    [算法][华为]2019华为笔试 找终点:给定一个正整数数组,最大为100个成员,从第一个成员开始,走到数组最后一个成员最少的步骤数, https://blog.csdn.net/qinglingLS ...

  7. 华为笔试时发现golang ACM模式输入的一个坑

    华为笔试时发现golang ACM模式输入的一个坑 golang ACM模式常用输入方法(scan,bufio):https://zhuanlan.zhihu.com/p/551393704 问题 第 ...

  8. 【2019华为笔试】召唤师的技能——圆排列,翻转和项链排列

    题目描述: dota游戏里面,召唤师可以控制冰雷火三种元素,并通过元素组合产生新的技能.现在我们修改了张新的地图, 地图中他能够控制n种元素, 并且将m个元素围成一个圈组成一 个新技能(这m个元素通过 ...

  9. 彭鑫老师等编著的教材《现代软件工程基础》获2022年度教育部-华为“智能基座”优秀教材奖...

    2022年度教育部-华为"智能基座"优秀教学资源遴选结果发布,彭鑫老师等编著的教材<现代软件工程基础>入选优秀教材奖! 2020年9月,在教育部的领导下,华为与72所高 ...

最新文章

  1. 最新必读图神经网络论文
  2. [转]在ITunes播放中前进、后退五秒的快捷键
  3. 086_访问html元素
  4. 简单网络管理协议(SNMP)实现linux系统信息网络监控
  5. python大数据_python大数据
  6. 轻松生成小程序分享海报
  7. [CF1107E]Vasya and Binary String【区间DP】
  8. 线程的五大状态及转换
  9. spring mvc + ajax上传文件,页面局部刷新
  10. 帮你理解vue的数据绑定的流程
  11. 五种MATLAB画圆方式程序
  12. 深度解读 - TDD(测试驱动开发)
  13. 密码编码学与网络安全讲的是什么
  14. 直接使用Jlink仿真器下载程序至Freescale K60系列 MCU
  15. iOS 模拟器安装APP,在电脑上运行ios的app
  16. 莫纳什大学招收AI/CV/ML/计算生物信息学和生物医学方向全奖博士研究生
  17. kubernetes 入门实践-搭建集群
  18. JQuery实现表单验证(注册页面)
  19. 雨听 | 英语学习笔记(十一)~作文范文:公园的免费入口
  20. 网络安全关于Windows下BAT脚本使用

热门文章

  1. mmcls多标签分类实战(三):多标签分类指标
  2. Seq2Seq模型实现(Decoder部分)
  3. k8s教程05(Kubernetes Ingress)
  4. SQL注入之——宽字节与二阶注入
  5. 毕设教学 单片机光照强度计使用
  6. 面试常见IT术语的英文日文英语日语翻译(不断更新中)
  7. png图片RGB转txt文本,然后再转回来jpg
  8. 陶瓷基覆铜板性能要求与标准
  9. mpi tcp连接报错_MPI通讯协议5 - 常见的工业通讯协议有哪些
  10. python定时器 循环_Python系列之循环定时器