/* FILE: lessieSlowManyL.cpp last change: 20-Aug-2012 author: Romeo Rizzi * This program is a solver for problem "lessie" in 03-Sept-2012 exam in Algorithms. */ #define NDEBUG // NDEBUG disabilita tutte le assert se definita prima di includere #include #ifndef NDEBUG # include // non voglio includerla quando Non compilo in modalita' DEBUG #endif #include #include using namespace std; double round(long double p) { return int(p*10000 +0.0001)/10000.0; } const int MAX_M = 1000; const int MAX_N = 1000; int m, n, qL, qH; bool wall[MAX_M+2][MAX_N+2]; // +2 perche' intendo bordare tutta la griglia in input con una cornice di celle muro long double prob_lessie_reaches[MAX_M+2][MAX_N+2]; // per evitare di dover trattare distintamente le celle ai margini della griglia /* template void displayMatrix(genType mat[MAX_M+2][MAX_N+2]) { for(int i = 1; i <= m; i++) { for(int j = 1; j <= n; j++) cout << mat[i][j] << " "; cout << endl; } cout << endl; } */ int main() { ifstream fin("input.txt"); assert(fin); ofstream fout("output.txt"); assert(fout); fin >> m >> n >> qL >> qH; for(int i = 0; i <= m+1; i++) for(int j = 0; j <= n+1; j++) { wall[i][j] = true; // mettiamo ovunque muro, in particolare sul contorno (dove servira' come sentinella) prob_lessie_reaches[i][j] = 0; } for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++) fin >> wall[i][j]; prob_lessie_reaches[1][1] = 1; for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++) if(!wall[i][j]) { if( (j >= n) | (wall[i-1][j+1]) ) prob_lessie_reaches[i][j] += prob_lessie_reaches[i-1][j]; else prob_lessie_reaches[i][j] += prob_lessie_reaches[i-1][j] / 2; if( (i >= m) | (wall[i+1][j-1]) ) prob_lessie_reaches[i][j] += prob_lessie_reaches[i][j-1]; else prob_lessie_reaches[i][j] += prob_lessie_reaches[i][j-1] / 2; } fout << round( prob_lessie_reaches[m][n] ) << endl; // displayMatrix(prob_lessie_reaches); int iL, jL; for(int count = 1; count <= qL; count++) { fin >> iL >> jL; for(int i = iL-1; i <= m+1; i++) for(int j = jL-1; j <= n+1; j++) prob_lessie_reaches[i][j] = 0; prob_lessie_reaches[iL][jL] = 1; for(int i = iL; i <= m; i++) for(int j = jL; j <= n; j++) if(!wall[i][j]) { if( (j >= n) | (wall[i-1][j+1]) ) prob_lessie_reaches[i][j] += prob_lessie_reaches[i-1][j]; else prob_lessie_reaches[i][j] += prob_lessie_reaches[i-1][j] / 2; if( (i >= m) | (wall[i+1][j-1]) ) prob_lessie_reaches[i][j] += prob_lessie_reaches[i][j-1]; else prob_lessie_reaches[i][j] += prob_lessie_reaches[i][j-1] / 2; } fout << round( prob_lessie_reaches[m][n] ) << endl; // displayMatrix(prob_lessie_reaches); } for(int i = 0; i <= m+1; i++) for(int j = 0; j <= n+1; j++) prob_lessie_reaches[i][j] = 0; prob_lessie_reaches[1][1] = 1; for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++) if(!wall[i][j]) { if( (j >= n) | (wall[i-1][j+1]) ) prob_lessie_reaches[i][j] += prob_lessie_reaches[i-1][j]; else prob_lessie_reaches[i][j] += prob_lessie_reaches[i-1][j] / 2; if( (i >= m) | (wall[i+1][j-1]) ) prob_lessie_reaches[i][j] += prob_lessie_reaches[i][j-1]; else prob_lessie_reaches[i][j] += prob_lessie_reaches[i][j-1] / 2; } int iH, jH; for(int count = 1; count <= qH; count++) { fin >> iH >> jH; fout << round( prob_lessie_reaches[iH][jH] ) << endl; } fout.close(); fin.close(); return 0; }