This is my first competition so bear with me.
Task:
Write a function that has this prototype:
Code:
int solver(MineField &field, int startX, int startY);
startX and startY being a position in the field which has 0 adjacent mines.
This functions task is to, with a given field and number of mines, solve this.
This is done by calling GetPosition(int x, int y) and SetPosition(int x, int y). Both located in MineField class. GetPosition will return how many adjacent squares that are mines, or if that square is a mine, it will have the value -1. SetPosition will set the given position to -2, indicating a possible bomb.
Rules:
Your function must immediately return to the main function if GetPosition returns -1. Failure to do so will result in the code going to the trash-can.
Your function must return the number of calls to GetPostition it had made.
Your function will be called only once per test.
The width and height of the field will be max 20, and there will be max 85 mines.
Judgement:
Your code will be judged as follows:
Speed (5* points)
Number of calls to GetPosition (3* points)
How many mines cleared (10* points)
*I reserve the right to change the points as I see fit.
Contest will end may 28th. PM me your submissions.
The MineField class:
Code:
#include <vector>
#include <cstdlib>
class MineField
{
private:
std::vector<std::vector<char> > field;
int width;
int height;
int numMines;
public:
MineField() { width = 0; height = 0; }
~MineField() {}
void GenerateField(int w, int h, int m);
int GetWidth() { return width; }
int GetHeight() { return height; }
int GetNumMines() { return numMines; }
void SetPosition(int x, int y)
{
field[x][y] = -2;
}
char GetPosition(int x, int y)
{
return field[x][y];
}
};
void MineField::GenerateField(int w, int h, int m)
{
width = w;
height = h;
numMines = m;
int x;
int y;
int i;
field.clear();
field.resize(width);
for(i=0; i<width; i++)
field[i].resize(height, 0);
for(i=0; i<numMines; i++)
{
x = std::rand()%width;
y = std::rand()%height;
if(field[x][y] == -1)
{
i--;
continue;
}
field[x][y] = -1;
}
for(x=0; x<width; x++)
{
for(y=0; y<height; y++)
{
if(field[x][y] == -1)
continue;
int left = x-1;
int right = x+1;
int up = y-1;
int down = y+1;
if(left < 0)
left = 0;
if(right >= width)
right = width-1;
if(up < 0)
up = 0;
if(down >= height)
down = height-1;
if(field[left][down] == -1)
field[x][y]++;
if(field[left][y] == -1)
field[x][y]++;
if(field[left][up] == -1)
field[x][y]++;
if(field[x][down] == -1)
field[x][y]++;
if(field[x][up] == -1)
field[x][y]++;
if(field[right][down] == -1)
field[x][y]++;
if(field[right][y] == -1)
field[x][y]++;
if(field[right][up] == -1)
field[x][y]++;
}
}
}