MineField
SRM 169 · 2003-10-28 · by huntergt
SRM 169 · 2003-10-28 · by huntergt · String Manipulation
Problem Statement
Problem Statement
Bob wants to program a game where a player tries to reveal all the squares on a minefield that do not contain mines. He has created an algorithm to generate random locations for mines. He wants to take these locations and use them to create a 9x9 board. The board needs to indicate the locations of the mines, as well as indicate how many mines border the spaces without mines. A mine borders a space if it is horizontally, vertically, or diagonally adjacent to that space.
Write a class MineField, which contains a method getMineField. getMineField takes aString mineLocations representing the locations of mines in the 9x9 field. getMineField returns a String[] representing the entire board. The ith element of the returned String[] corresponds to row i of the board. Each element of the returned String[] should be 9 characters in length, where each character is either 'M' (symbolizing a mine) or a digit, d, between '0' and '8' inclusive (symbolizing an empty space bordering d mines.)
The String passed to the method will be in the following format (quotes added for clarity):
Each pair of parentheses holds the coordinates of a mine in (row, column) format. Counting begins at 0, not 1. Therefore, (0,0) represents the upper-left corner, and (8,8) represents the bottom-right corner. For example, suppose Bob randomly generated the following locations:
Write a class MineField, which contains a method getMineField. getMineField takes a
The String passed to the method will be in the following format (quotes added for clarity):
- "(r0,c0)(r1,c1)...(rN,cN)"
Each pair of parentheses holds the coordinates of a mine in (row, column) format. Counting begins at 0, not 1. Therefore, (0,0) represents the upper-left corner, and (8,8) represents the bottom-right corner. For example, suppose Bob randomly generated the following locations:
- "(0,0)(1,0)(2,0)(3,0)(4,0)"
{ "M20000000",
"M30000000",
"M30000000",
"M30000000",
"M20000000",
"110000000",
"000000000",
"000000000",
"000000000" }
There are 5 mines (symbolized by "M") located straight down the first column. Two spots on the board border 1 mine; two spots border 2 mines; and three spots border 3 mines. All other spots on the board border no mines.Constraints
- mineLocations will contain between 0 and 50 characters, inclusive
- mineLocations will contain between 0 and 10 mines, inclusive
- mineLocations will be in the format "(r0,c0)(r1,c1)...(rN,cN)" where each r# and c# is a digit between '0' and '8', inclusive
- mineLocations will not contain duplicate locations
Examples
0)
"(0,0)(1,0)(2,0)(3,0)(4,0)"
Returns: { "M20000000", "M30000000", "M30000000", "M30000000", "M20000000", "110000000", "000000000", "000000000", "000000000" }
This is the example from above.
1)
"(0,0)(0,8)(8,0)(8,8)"
Returns: { "M1000001M", "110000011", "000000000", "000000000", "000000000", "000000000", "000000000", "110000011", "M1000001M" }
There is a mine in each corner of the board. There are twelve spots that border exactly 1 mine. All other spots border no mines.
2)
"(3,2)(3,3)(3,4)(4,2)(4,4)(5,2)(5,3)(5,4)(7,4)(6,7)"
Returns: { "000000000", "000000000", "012321000", "02MMM2000", "03M8M3000", "02MMM2111", "0124321M1", "0001M1111", "000111000" }
3)
""
Returns: { "000000000", "000000000", "000000000", "000000000", "000000000", "000000000", "000000000", "000000000", "000000000" }
Don't forget the empty case.
4)
"(6,0)(6,8)(6,6)(8,3)(1,0)(5,4)(6,3)(4,4)(6,4)(1,5)"
Returns: { "110011100", "M1001M100", "110011100", "000111000", "0002M2000", "1114M4121", "M11MM3M2M", "112332121", "001M10000" }
Submissions are judged against all 20 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class MineField with a public method vector<string> getMineField(string mineLocations) · 20 test cases · 2 s / 256 MB per case