Find Where to Expand in Minesweeper (Java)
Implement a function that turns revealed cells into -2 given a location the user wants to click.
For simplicity, only reveal cells that have 0 in them. If the user clicks on any other type of cell (for example, -1 / bomb or 1, 2, or 3), just ignore the click and return the original field. If a user clicks 0, reveal all other 0's that are connected to it.
Example 1:
Given field:
[[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, -1, 1, 0]]
Click location: (2, 2: row index = 2, column index = 2)
Resulting field:
[[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, -1, 1, 0]] (same as the given field)
Example 2:
Given field:
[[-1, 1, 0, 0],
[1, 1, 0, 0],
[0, 0, 1, 1],
[0, 0, 1, -1]]
Click location: (1, 3: row index = 1, column index = 3)
Resulting field:
[[-1, 1, -2, -2],
[1, 1, -2, -2],
[-2, -2, 1, 1],
[-2, -2, 1, -1]]
Your function should take:
- field: The given field as a 2D array
- num_rows / numRows: The number of rows in the given field
- num_cols / numCols: The number of columns in the given field
- given_i / givenI: The row index of the cell the user wants to click
- given_j / givenJ: The column index of the cell the user wants to click
풀이
이번것과 마찬가지로 지뢰찾기 문제이다.
지난번 문제를 풀어봤기에 좀 더 쉽게 풀기는 했는데 강사 개발자 코드하고는 좀 다르다.
클릭했을 때 우선 지뢰가 없는 셀(숫자 0)을 밟을 경우 인근의 0으로 되어있는 셀은 전부 -2로 바꿔주면 된다.
만약 클릭했을 때 지뢰가 없는 셀(숫자 0)이 아니라면 그냥 field 그대로 return 해주면 된다.
package com.company;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// NOTE: The following input values will be used for testing your solution.
int[][] field1 = {{0, 0, 0, 0, 0},
{0, 1, 1, 1, 0},
{0, 1, -1, 1, 0}};
System.out.println(Arrays.deepToString(click(field1, 3, 5, 2, 2)));
// [[0, 0, 0, 0, 0],
// [0, 1, 1, 1, 0],
// [0, 1, -1, 1, 0]]
System.out.println(Arrays.deepToString(click(field1, 3, 5, 1, 4)));
// [[-2, -2, -2, -2, -2],
// [-2, 1, 1, 1, -2],
// [-2, 1, -1, 1, -2]]
int[][] field2 = {{-1, 1, 0, 0},
{1, 1, 0, 0},
{0, 0, 1, 1},
{0, 0, 1, -1}};
click(field2, 4, 4, 0, 1);
// [[-1, 1, 0, 0],
// [1, 1, 0, 0],
// [0, 0, 1, 1],
// [0, 0, 1, -1]]
// click(field2, 4, 4, 1, 3) should return:
// [[-1, 1, -2, -2],
// [1, 1, -2, -2],
// [-2, -2, 1, 1],
// [-2, -2, 1, -1]]
}
// Implement your solution below.
public static int[][] click(int[][] field, int numRows, int numCols, int givenI, int givenJ) {
if (field[givenI][givenJ] != 0){
return field;
} else {
for (int i = givenI-numRows-1; i < givenI + numRows; i++){
for (int j = givenJ -numCols-1; j< givenJ +numCols; j++){
if (i >=0 && numRows > i &&
j >=0 && numCols > j &&
field[i][j] == 0) field[i][j] = -2;
}
}
}
return field;
}
}
강사 풀이
ArrayDeque를 통해서 풀이를 했다.
강사는 지난번 지뢰찾기랑 마찬가지로 9개 셀을 점검하는 방식을 사용했는데,
9개 셀을 점검하면서 0 이면 -2로 바꿔주고 0 인 셀의 index를 ArrayDeque 담아뒀다가 다시 점검하는 방식으로 풀이했다.
import java.util.ArrayDeque;
public class MS2 {
public static void main(String[] args) {
// NOTE: The following input values will be used for testing your solution.
int[][] field1 = {{0, 0, 0, 0, 0},
{0, 1, 1, 1, 0},
{0, 1, -1, 1, 0}};
// click(field1, 3, 5, 2, 2) should return:
// [[0, 0, 0, 0, 0],
// [0, 1, 1, 1, 0],
// [0, 1, -1, 1, 0]]
// click(field1, 3, 5, 1, 4) should return:
// [[-2, -2, -2, -2, -2],
// [-2, 1, 1, 1, -2],
// [-2, 1, -1, 1, -2]]
int[][] field2 = {{-1, 1, 0, 0},
{1, 1, 0, 0},
{0, 0, 1, 1},
{0, 0, 1, -1}};
// click(field2, 4, 4, 0, 1) should return:
// [[-1, 1, 0, 0],
// [1, 1, 0, 0],
// [0, 0, 1, 1],
// [0, 0, 1, -1]]
// click(field2, 4, 4, 1, 3) should return:
// [[-1, 1, -2, -2],
// [1, 1, -2, -2],
// [-2, -2, 1, 1],
// [-2, -2, 1, -1]]
}
// Implement your solution below.
public static int[][] click(int[][] field, int numRows, int numCols, int givenI, int givenJ) {
ArrayDeque<int[]> toCheck = new ArrayDeque<int[]>();
if (field[givenI][givenJ] == 0) {
field[givenI][givenJ] = -2;
int[] coordinates = {givenI, givenJ};
toCheck.add(coordinates);
}
else {
return field;
}
while(!toCheck.isEmpty()) {
int[] currentCoordinates = toCheck.remove();
int currentI = currentCoordinates[0];
int currentJ = currentCoordinates[1];
for (int i = currentI - 1; i < currentI + 2; i++) {
for (int j = currentJ - 1; j < currentJ + 2; j++) {
if (0 <= i && i < numRows && 0 <= j && j < numCols
&& field[i][j] == 0) {
field[i][j] = -2;
int[] coordinates = {i, j};
toCheck.add(coordinates);
}
}
}
}
return field;
}
}