/* FILE: lessieSlowManyH.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_getting_home_from[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_getting_home_from[i][j] = 0; } for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++) fin >> wall[i][j]; prob_getting_home_from[m][n] = 1; for(int i = m; i >= 1; i--) for(int j = n; j >= 1; j--) if(!wall[i][j]) { if( (wall[i][j+1]) && (!wall[i+1][j]) ) prob_getting_home_from[i][j] = prob_getting_home_from[i+1][j]; if( (wall[i+1][j]) && (!wall[i][j+1]) ) prob_getting_home_from[i][j] = prob_getting_home_from[i][j+1]; if( (!wall[i+1][j]) && (!wall[i][j+1]) ) prob_getting_home_from[i][j] += ( prob_getting_home_from[i+1][j] + prob_getting_home_from[i][j+1] ) /2; } fout << round( prob_getting_home_from[1][1] ) << endl; // displayMatrix(prob_getting_home_from); int iL, jL; for(int count = 1; count <= qL; count++) { fin >> iL >> jL; fout << round( prob_getting_home_from[iL][jL] ) << endl; } int iH, jH; for(int count = 1; count <= qH; count++) { fin >> iH >> jH; for(int i = 0; i <= iH+1; i++) for(int j = 0; j <= jH+1; j++) prob_getting_home_from[i][j] = 0; prob_getting_home_from[iH][jH] = 1; for(int i = iH; i >= 1; i--) for(int j = jH; j >= 1; j--) if(!wall[i][j]) { if( (wall[i][j+1]) && (!wall[i+1][j]) ) prob_getting_home_from[i][j] += prob_getting_home_from[i+1][j]; if( (wall[i+1][j]) && (!wall[i][j+1]) ) prob_getting_home_from[i][j] += prob_getting_home_from[i][j+1]; if( (!wall[i+1][j]) && (!wall[i][j+1]) ) prob_getting_home_from[i][j] += ( prob_getting_home_from[i+1][j] + prob_getting_home_from[i][j+1] ) /2; } fout << round( prob_getting_home_from[1][1] ) << endl; // displayMatrix(prob_getting_home_from); } fout.close(); fin.close(); return 0; }