Cod sursa(job #638047)

Utilizator mihai_floreaFlorea Mihai Alexandru mihai_florea Data 20 noiembrie 2011 18:22:21
Problema Minesweeper Scor 10
Compilator cpp Status done
Runda .com 2011 Marime 1 kb
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>

const int TRIALS = 200;
const int STEPS = 50;

int n, m, state[30][30];

int randint(int maxval)
{
	return rand() % (maxval+1);
	while (1)
	{
		int x = rand() & 31;
		if (x <= maxval) return x;
	}
}

int sim(int steps)
{
	memset(state, 0, sizeof(state));
	int nr = 0;
	for (int step = 1; step <= steps; ++step)
	{
		int x = randint(n-1);
		int y = randint(m-1);
		if (state[x][y] == 2) --nr;
		state[x][y] = (state[x][y] + 1) % 3;
		if (state[x][y] == 2) ++nr;
		if (nr == n*m && step != steps) return 0;
	}
	return nr == n*m ? 1 : 0;
}

int main()
{
	freopen("minesweeper.in", "r", stdin);
	freopen("minesweeper.out", "w", stdout);
	
	scanf("%d%d", &n, &m);

	srand(time(NULL));
	
	double E = 0;
	for (int i = 0; i < STEPS; ++i)
	{
		double p = 0;
		for (int j=0; j < TRIALS; ++j)
			p += sim(2*n*m + 3*i);
		p /= TRIALS;
		
		E += p * (2*n*m + 3*i);
	}
	
	printf("%lf\n", E);
	return 0;
}