3月10日阿里笔试第一题
import java.util.*;
public class Main {
static int[] ans = new int[2];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//while (in.hasNextInt()) {// 注意,如果输入是多个测试用例,请通过while循环处理多个测试用例
// int a = in.nextInt();
// int b = in.nextInt();
// System.out.println(a + b);
//}
// int m = in.nextInt();
// int n = in.nextInt();
// int k = in.nextInt();
// in.nextLine();
// int index = 0;
int m = 3, n = 4, k = 4;
char[][] map = new char[m][n];
// while(index < map.length) {
// String str = in.nextLine();
// char[] chars = str.toCharArray();
// System.out.println(str);
// map[index] = chars;
//
// ++index;
// }
map[0] = new char[]{'@', ',', '.', '.'};
map[1] = new char[]{'.', '#', '.', '.'};
map[2] = new char[]{'.', '.', '.', '#'};
// int[] directs = new int[k];
// index = 0;
// while(index < directs.length) {
// String str = in.nextLine();
// if("EAST".equals(str)) {
// directs[index] = 1;
// ++index;
// } else if("SOUTH".equals(str)) {
// directs[index] = 2;
// ++index;
// } else if("WEST".equals(str)) {
// directs[index] = 3;
// ++index;
// } else if("NORTH".equals(str)) {
// directs[index] = 4;
// ++index;
// }
// }
// System.out.println(Arrays.toString(directs));
int[] directs = new int[]{1, 2, 3, 4};
boolean[][] visited = new boolean[m][n];
for (int i = 0; i < m; i++) {
System.out.println(Arrays.toString(map[i]));
}
int mi = 0, mj = 0;
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
if(map[i][j] == '@') {
mi = i;
mj = j;
break;
}
}
}
dfs(map, directs, mi, mj, k, visited);
System.out.println(Arrays.toString(ans));
}
private static void dfs(char[][] map, int[] directs, int m, int n, int k, boolean[][] visited) {
if(m < 0 || n < 0 || m >= map.length || n >= map[0].length || '#' == (map[m][n])) {
return ;
}
System.out.println(m + " " + n);
for(int i = 0; i < k; ++i) {
int direct = directs[i];
if(direct == 1) {
while(n < map[0].length && ('#' != (map[m][n]) && !visited[m][n])) {
System.out.println(map[m][n]);
visited[m][n] = true;
++n;
}
if(n > 0) {
--n;
}
}
if(direct == 2) {
while(m < map.length && ('#' != (map[m][n]) && !visited[m][n])) {
visited[m][n] = true;
++m;
}
if(m > 0) {
--m;
}
// --m;
}
// System.out.println(m + " " + n);
if(direct == 3) {
while(n >= 0 && ('#' != (map[m][n]) && !visited[m][n])) {
visited[m][n] = true;
--n;
}
if(n < map[0].length - 1) {
++n;
}
}
if(direct == 4) {
while (m >= 0 && ('#' != (map[m][n]) && !visited[m][n])) {
visited[m][n] = true;
--m;
}
if(m < map.length) {
++m;
}
// ++m;
}
System.out.println(m + " " + n);
}
ans[0] = m;
ans[1] = n;
return ;
}
} ***了,读输入调了半天,第一题题目倒是挺简单


