网易互娱数据研发工程师笔试
三道算法题,第一题明明逻辑没问题,但只过了 30%, 第二题 BFS, 但是一个输入格式让我无语。。。scanner 用的太少了
第二题,贴个垃圾代码,没有线上测,求点评,一般的 BFS 能过多少??
public static void main(String[] args) {
int[][] directions = new int[][]{{0,1},{0,-1},{1,0},{-1,0}};
Queue<int[]> queue = new LinkedList<>();
boolean[][] visited = new boolean[n][n];
queue.add(start);
visited[start[0]][start[1]] = true;
int res = 0;
while (!queue.isEmpty()){
int size = queue.size();
for (int i = 0; i < size; i++) {
int[] top = queue.poll();
int x = top[0], y = top[1];
if (g[x][y] <= '9' && g[x][y] >= '1') res += g[x][y] - '0';
for (int k = 0; k < 4; k++) {
int newX = x + directions[k][0];
int newY = y + directions[k][1];
if (newX < 0 || newX >= n || newY < 0 || newY >= n || visited[newX][newY] || g[newX][newY] == '#') continue;
visited[newX][newY] = true;
queue.add(new int[]{newX, newY});
}
}
}
}
}
