Created
June 27, 2018 04:52
-
-
Save jianminchen/3826e16751a9e6c3b25614b0b7684390 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://en.wikipedia.org/wiki/Minesweeper_(video_game) | |
Implement minesweep alogirthm | |
M*N | |
1000 * 1000 , 3 mines , mine position, you like to fill non-mines with numbers | |
time complexity and space complexity. | |
preprocessing all mines-> at most 8 neighbor has | |
k * 8 -> time complexity -> duplication here -> k worst case: m * n | |
for (int i=0; i < M; ++i){ | |
for (int j=0; i < N; ++j){ | |
if (arr[i][j] == '*'){ | |
arr[i][j+1] = // | |
} | |
} | |
} | |
X 1 * 1 X | |
1 2 2 1 X | |
1 * 2 1 X | |
1 2 * 1 X | |
X 1 1 1 X | |
//M - row | |
//N - column | |
//K - no of mines | |
/// | |
//? should we call it sweep | |
char[][] placeMines(int rows, int columns, int KMines){ | |
} | |
// let say we need to fill matrix with large number of mines M*N-2 mines | |
//Approach: | |
int k =0; | |
Random rand = new Random(); | |
while(k<Kmines) | |
{ | |
int idx= rand.nextInt(rows *column); | |
if(grid[idx/M][idx%N]=='X) | |
{ | |
grid[rowRandom][colRandom] = '*'; | |
k++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment