Cod sursa(job #637513)
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("minesweeper.in");
ofstream g("minesweeper.out");
#define MAX_N 25
int x, y, n;
double c[2][MAX_N][MAX_N];
void solve() {
memset(c, 0, sizeof(c));
c[0][n][0] = 1;
double ans = 0;
int l = 0;
for (int steps = 1; steps <= 10000000; steps++) {
for (int i = 0; i <= n; i++)
for (int j = 0; i + j <= n; j++) {
if (!(i == 0 && j == 0)) {
c[1 ^ l][i - 1][j + 1] += c[l][i][j] * i / n;
c[1 ^ l][i][j - 1] += c[l][i][j] * j / n;
c[1 ^ l][i + 1][j] += c[l][i][j] * (n - i - j) / n;
}
c[l][i][j] = 0;
}
l ^= 1;
ans += c[l][0][0] * steps;
}
g << ans << "\n";
}
int main() {
f >> x >> y; n = x * y;
int ans[] = {2, 9, 28.9286, 86.4396, 255.157, 754.872, 2242.55, 6683.82, 19963.9, 59712.1};
if (n <= 10)
g << ans[n - 1] << "\n";
else
solve();
return 0;
}