Cod sursa(job #2608394)

Utilizator Alex_BubBuburuzan Alexandru Alex_Bub Data 1 mai 2020 11:26:03
Problema Minesweeper Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <fstream>
#include <vector>
#include <iomanip>
#include <cmath>

using namespace std;

ifstream fin("minesweeper.in");
ofstream fout("minesweeper.out");

const int CMax = 276;
const double EPS = 0.001;

int H[25][25][25], N, M, ct;
double A[CMax + 5][CMax + 5], X[CMax + 5];

int main()
{
    fin >> N >> M;
    N = N * M;

    for(int a = 0; a <= N; a++)
        for(int b = 0; a + b <= N; b++)
        {
            int c = N - a - b;
            H[a][b][c] = ++ct;
        }

    for(int a = 0; a <= N; a++)
        for(int b = 0; a + b <= N; b++)
        {
            int c = N - a - b, Nod = H[a][b][c];
            A[Nod][Nod] = 1.0;

            if(c == N)
                continue;

            if(a) A[Nod][H[a - 1][b + 1][c]] = -1.0 * a / N;
            if(b) A[Nod][H[a][b - 1][c + 1]] = -1.0 * b / N;
            if(c) A[Nod][H[a + 1][b][c - 1]] = -1.0 * c / N;

            A[Nod][ct + 1] = 1.0;
        }
    N = ct;

    int i = 1, j = 1, k;
    double ct = 0;

    while(i <= N && j <= N)
    {
        for(k = i; k <= N; k++)
            if(fabs(A[k][j]) > EPS)
                break;

        if(k == N + 1)
        {
            j++;
            continue;
        }

        swap(A[i], A[k]);
        ct = A[i][j];

        for(k = j; k <= N + 1; k++)
            A[i][k] /= ct;

        for(int x = i + 1; x <= N; x++)
        {
            ct = A[x][j];

            for(int y = j; y <= N + 1; y++)
                A[x][y] -= ct * A[i][y];
        }
        i++, j++;
    }

    for(i = N; i > 0; i--)
        for(j = 1; j <= N; j++)
            if(fabs(A[i][j]) > EPS)
            {
                X[j] = A[i][N + 1];

                for(k = j + 1; k <= N; k++)
                    X[j] -= X[k] * A[i][k];

                break;
            }

    fout << fixed << setprecision(6) << X[N] << '\n';

    fin.close();
    fout.close();

    return 0;
}