华为OD机试真题 - 找单词
static int[][] DISTANCE = {{1,0},{-1,0},{0,1},{0,-1}};
static String ans = null;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = Integer.parseInt(in.nextLine());
String[][] ch = new String[num][num];
for (int i = 0; i < num; i++) {
ch[i] = in.nextLine().split(",");
}
String word = in.nextLine();
System.out.println(word);
getResult(word, ch);
}
public static void getResult(String word, String[][] map) {
boolean[][] visited = new boolean[map.length][map.length];
StringBuilder out = null;
String path = "";
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
out = new StringBuilder();
dfs(word, visited, map, i, j, out, 0, path);
}
}
System.out.println(ans);
}
public static boolean dfs(String word, boolean[][] visited, String[][] map, int i, int j, StringBuilder out, int depth, String path) {
if (word.equals(path)) {
ans = out.delete(out.length() - 1, out.length()).toString();
return true;
}
if (i >= map.length || j >= map.length || i < 0 || j < 0)
return false;
if (visited[i][j] == true)
return false;
if (word.charAt(depth) != map[i][j].charAt(0))
return false;
visited[i][j] = true;
out.append(i + ",");
out.append(j + ",");
for (int k = 0; k < DISTANCE.length; k++) {
if(dfs(word, visited, map, i + DISTANCE[k][0], j + DISTANCE[k][1], out, depth + 1, path + map[i][j]))
return true;
}
out.delete(out.length() - 4, out.length());
visited[i][j] = false;
return false;
}
static String ans = null;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = Integer.parseInt(in.nextLine());
String[][] ch = new String[num][num];
for (int i = 0; i < num; i++) {
ch[i] = in.nextLine().split(",");
}
String word = in.nextLine();
System.out.println(word);
getResult(word, ch);
}
public static void getResult(String word, String[][] map) {
boolean[][] visited = new boolean[map.length][map.length];
StringBuilder out = null;
String path = "";
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
out = new StringBuilder();
dfs(word, visited, map, i, j, out, 0, path);
}
}
System.out.println(ans);
}
public static boolean dfs(String word, boolean[][] visited, String[][] map, int i, int j, StringBuilder out, int depth, String path) {
if (word.equals(path)) {
ans = out.delete(out.length() - 1, out.length()).toString();
return true;
}
if (i >= map.length || j >= map.length || i < 0 || j < 0)
return false;
if (visited[i][j] == true)
return false;
if (word.charAt(depth) != map[i][j].charAt(0))
return false;
visited[i][j] = true;
out.append(i + ",");
out.append(j + ",");
for (int k = 0; k < DISTANCE.length; k++) {
if(dfs(word, visited, map, i + DISTANCE[k][0], j + DISTANCE[k][1], out, depth + 1, path + map[i][j]))
return true;
}
out.delete(out.length() - 4, out.length());
visited[i][j] = false;
return false;
}
全部评论
相关推荐
10-31 10:37
南京邮电大学 移动通讯工程师 点赞 评论 收藏
分享
09-24 10:31
重庆理工大学 算法工程师
丿南烟丶:黑白模板吧,不要这样花哨的。
主要成就太空了,和获奖融在一起,写一两行就行了。
职业技能不要这样排,就传统的掌握精通什么什么然后举例补充的一些重要技术点。
自我介绍说实话也没啥用,可以删了。
把自己的两个项目方案细节补充上去,为什么这样设计,怎么设计,成果是什么按star法则来写
你要引导面试官来问你的技能和项目,你的获奖和自我介绍别人可能看都不看一眼或者不太在乎,重要的是展示你能干活的能力 点赞 评论 收藏
分享
查看6道真题和解析 点赞 评论 收藏
分享