Advertisement
cmiN

srm472 div2 - 1000

Sep 7th, 2011
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <set>
  4. using namespace std;
  5.  
  6.  
  7. class RectangularIsland {
  8.    
  9.     static const int N = 4, dx[N], dy[N];
  10.     long long** table, out, fav;
  11.     int steps, width, height;
  12.     queue<pair<int, int> > cells;
  13.    
  14.     void back()
  15.     {
  16.         set<pair<int, int> > toAdd;
  17.         while (steps-- > 0) { // for each step
  18.             for (set<pair<int, int> >::iterator it = toAdd.begin(); it != toAdd.end(); ++it) {
  19.                 cells.push(*it); // update queue
  20.             }
  21.             toAdd.clear();
  22.             while (!cells.empty()) { // retrieve all positions in this state
  23.                 pair<int, int> cell = cells.front();
  24.                 cells.pop();
  25.                 for (int i = 0; i < N; ++i) {
  26.                     long long& tmp = table[cell.first + dx[i]][cell.second + dy[i]];
  27.                     if (tmp == -1) {
  28.                         out += table[cell.first][cell.second];
  29.                     } else {
  30.                         tmp += table[cell.first][cell.second]; // update number of steps
  31.                         toAdd.insert(make_pair(cell.first + dx[i], cell.second + dy[i]));
  32.                     }
  33.                 }
  34.                 table[cell.first][cell.second] = 0;
  35.             }
  36.         }
  37.     }
  38.    
  39.     void print_table()
  40.     {
  41.         for (int i = 0; i <= height + 1; ++i) {
  42.             for (int j = 0; j <= width + 1; ++j) {
  43.                 cout << table[i][j] << " ";
  44.             }
  45.             cout << endl;
  46.         }
  47.     }
  48.    
  49.     public:
  50.     double theProbablity(int width, int height, int y, int x, int steps)
  51.     {
  52.         this->width = width;
  53.         this->height = height;
  54.         this->steps = steps;
  55.         out = fav = 0;
  56.         table = new (nothrow) long long*[height + 2];
  57.         int i, j; // memory allocation
  58.         for (i = 0; i <= height + 1; ++i) table[i] = new (nothrow) long long[width + 2];
  59.         for (i = 0; i <= width + 1; ++i) {
  60.             table[0][i] = -1;
  61.             table[height + 1][i] = -1;
  62.         } // create the margin [/\]_[\/]
  63.         for (i = 0; i <= height + 1; ++i) {
  64.             table[i][0] = -1;
  65.             table[i][width + 1] = -1;
  66.         }
  67.         for (i = 1; i <= height; ++i) {
  68.             for (j = 1; j <= width; ++j) {
  69.                 table[i][j] = 0; // fill the rest
  70.             }
  71.         }
  72.         x = height - x;
  73.         y = y + 1; // mark the start cell
  74.         table[x][y] = 1;
  75.         cells.push(make_pair(x, y));
  76.         back();
  77.         //print_table();
  78.         for (i = 1; i <= height; ++i) {
  79.             for (j = 1; j <= width; ++j) {
  80.                 fav += table[i][j]; // fill the rest
  81.             }
  82.         }
  83.         //cout << fav << " " << out;
  84.         return (double)fav / (fav + out);
  85.     }
  86. };
  87.  
  88. const int RectangularIsland::dx[] = {0, 0, -1, 1};
  89. const int RectangularIsland::dy[] = {1, -1, 0, 0};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement