注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
好几天没发面经了,发个两天前面的攒点RP 美国小哥,和之前的面经全都不一样,题目不算难,但考察范围很全,从代码风格到设计思维(包括返回值以什么形式等等)都有考察,感觉比有些喜欢出偏怪难题的面试好多了。 设计一个扫雷游戏。我一开始说要分两阶段,一阶段是布雷,一阶段是扫,然后小哥说布雷不用做了,假设你的constructor就传入了布好雷的matrix。(表示很失望,不能show一发O(k) partial Fisher-Yates随机算法了) 扫雷就是一个clickTile(int row, int c 路,继续探索新的区域。每次我们markTile()的时候检查hasWon(),我们就能确定有没有赢了。 做完还有十分钟左右,小哥表示非常满意(我自己也很满意,这也算是我目前电面遇到最复杂的题了,算法不难,但有很多设计思想自己还是都马上想到了),问了两个问题就结束了 - /**
- * Minesweeper Game
- * http://minesweeperonline.com/
- *
- * 1) Game should take in a pre-populated matrix representing a minesweeper board like:
- *
- * B X B X B = bomb
- * B B X X X = no bomb
- * X B X X
- * B X X X
- *
- * 2) Expose function clickTile(row, col) which clicks the tile and returns information about
- * the clicked tile (and surrounding tiles) that satisfies the following:
- *
- * - If neighboring bombs, return the # surrounding bombs
- * - If no neighboring bombs, return set of surrounding tiles with
- * neighboring bombs (and count of bombs)
- * - If bomb clicked, indicate to player they lose
- * - Should keep track of which tiles have been clicked
- *
- * Example Calls:
- * clickTile(0, 1) -> count of 4
- * clickTile(3, 3) -> (3, 2) has 1 surrounding bomb
- * (3, 3) has 0 surrounding bombs
- * (2, 2) has 2 surrounding bombs
- * (2, 3) has 0 surrounding bombs
- * (1, 2) has 2 surrounding bombs
- * (1, 3) has 1 surrounding bomb
- *
- */
- class Point {
- int x;
- int y;
- int count;
- Point(int x, int y, int count) {
- this.x = x;
- this.y = y;
- this.count = count;
- }
- }
- class ReturnType {
- boolean isAlive;
- List<Point> surroundings;
- ReturnType(boolean isAlive) {
- this.isAlive = isAlive;
- this.surroundings = new ArrayList<>();
- }
- }
- class Minesweeper {
- private final int[][] dirs = {
- { -1, -1 }, { -1, 0 }, { -1, 1 },
- { 0, -1 }, { 0, 1 },
- { 1, -1 }, { 1, 0 }, { 1, 1 }
- };
-
- private char[][] matrix;
- private int m;
- private int n;
-
- public Minesweeper(char[][] matrix) {
- this.matrix = matrix;
- this.m = matrix.length;
- this.n = matrix[0].length;
- }
-
- public ReturnType clickTile(int row, int col) {
- if (matrix[row][col] == 'B') {
- return new ReturnType(false);
- }
-
- int count = countNeighbors(row, col);
- if (count > 0) {
- Point here = new Point(row, col, count);
- ReturnType res = new ReturnType(true);
- res.surroundings.add(here);
- return res;
- }
-
- boolean[][] visited = new boolean[m][n];
- List<Point> surroundings = new ArrayList<>();
- visited[row][col] = true;
- dfs(row, col, visited, surroundings);
-
- ReturnType res = new ReturnType(true);
- res.surroundings = surroundings;
- return res;
- }
-
- private int countNeighbors(int x, int y) {
- int count = 0;
- for (int[] dir : dirs) {
- int xx = x + dir[0];
- int yy = y + dir[1];
-
- if (xx < 0 || xx >= m || yy < 0 || yy >= n) {
- continue;
- }
-
- if (matrix[xx][yy] == 'B') {
- count++;
- }
- }
- return count;
- }
-
- private void dfs(int x, int y, boolean[][] visited, List<Point> surroundings) {
- int count = countNeighbors(x, y);
- if (count > 0) {
- Point point = new Point(x, y, count);
- surroundings.add(point);
- }
-
- for (int[] dir : dirs) {
- int xx = x + dir[0];
- int yy = y + dir[1];
-
- if (xx < 0 || xx >= m || yy < 0 || yy >= n || visited[xx][yy] || matrix[xx][yy] == 'B') {
- continue;
- }
-
- visited[xx][yy] = true;
- dfs(xx, yy, visited, surroundings);
- }
- }
-
- public void markTileAsBomb(row, col);
- public boolean hasWon();
- }
复制代码 |