Cod sursa(job #1848492)

Utilizator ade_tomiEnache Adelina ade_tomi Data 16 ianuarie 2017 08:57:58
Problema Minesweeper Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include  <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int eps = 1e-6;
const int nmax = 24;
bool zero (double x)
{

    return (x > - eps) && (x < eps);
}
int v[nmax][nmax], n, m;
double a[nmax * nmax][nmax * nmax];

int main ()
{

    ifstream cin ("minesweeper.in");
    ofstream cout ("minesweeper.out");
    cin >> n >> m;
    n *= m;
    int k = 0;
    for (int i = 0; i <= n; i++)
    {

        for (int j = 0; j <= n - i; j++)
        {

            k ++;
            v[i][j] = k;
        }
    }
    for (int i = 0; i < n; i++)
        for (int j = 0; j <= n - i; j++)
        {

            a[v[i][j]][v[i][j]] = -1.0;
            a[v[i][j]][v[i][j + 1]] = 1.0 * (n - i - j) / n;
            a[v[i][j]][v[i + 1][j - 1]] = 1.0 * j / n;
            a[v[i][j]][v[i - 1][j]] = 1.0 * i / n;
            a[v[i][j]][k + 1] = -1.0;
        }
    a[k][k] =  1.0;
    for (int col = 1; col <= k; col++)
    {

        if (zero(a[col][col]))
        {
            int lin = k + 1;
            while (zero(a[lin][col]) && lin <= k + 1)
                lin++;
            swap(a[lin], a[col]);
        }
        for (int i = 1; i <= k; i++)
        {

            if (i == col)
                continue;
            double kp = a[i][col] * 1.0 / a[col][col];
            for (int j = 1; j <= k + 1; j++)
                a[i][j] -= kp * a[col][j];
        }
    }
    cout << setprecision(6) << (a[1][k + 1] / a[1][1]) << "\n";
    return 0;
}