Cod sursa(job #839391)

Utilizator repp4raduRadu-Andrei Szasz repp4radu Data 21 decembrie 2012 19:09:55
Problema Minesweeper Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <fstream>
#include <iomanip>

#define NMAX 24
#define MAX 580
#define EPS 1e-6

using namespace std;

int N, M, L, V[NMAX][NMAX];
double A[MAX][MAX], X[MAX];

int main()
{
    ifstream in("minesweeper.in"); in>>N>>M; in.close();
    N *= M; M = 0;
    for(int G = 0; G <= N; G++)
        for(int S = 0; S + G <= N; S++) V[G][S] = ++M;
    L = ++M;
    M = 1;
    for(int G = 0; G <= N; G++)
        for(int S = 0; S + G <= N; S++, M++)
        {
            int I = N - G - S;
            A[M][M] = 1;
            if(I == N) continue;
            if(G) A[M][V[G - 1][S + 1]] -= (1.0 * G) / (1.0 * N);
            if(S) A[M][V[G][S - 1]] -= (1.0 * S) / (1.0 * N);
            if(I) A[M][V[G + 1][S]] -= (1.0 * I) / (1.0 * N);
            A[M][L] = 1;
        }
    for(int i = 1; i < L; i++)
    {
        if(A[i][i] > EPS || A[i][i] < -EPS)
        {
            for(int j = i + 1; j <= L; j++) A[i][j] /= A[i][i];
            A[i][i] = 1;
        }
        for(int l = i + 1; l < L; l++)
        {
            for(int j = i + 1; j <= L; j++) A[l][j] -= A[l][i] * A[i][j];
            A[l][i] = 0;
        }
    }
    for(int i = L; i; i--)
    {
        X[i] = A[i][L];
        for(int j = i + 1; j < L; j++) X[i] -= A[i][j] * X[j];
    }
    ofstream out("minesweeper.out"); out<<fixed<<setprecision(6)<<X[L - 1]<<"\n"; out.close();
    return 0;
}