Advertisement
cmiN

srm297 div1 p2

Feb 29th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. struct Coord {
  8.     int l, c;
  9.     Coord()
  10.     {
  11.         l = c = 0;
  12.     }
  13.     Coord(int l, int c)
  14.     {
  15.         this->l = l;
  16.         this->c = c;
  17.     }
  18. };
  19.  
  20.  
  21. class CageTheMonster {
  22.  
  23.     public:
  24.     typedef vector<string> vs_t;
  25.     typedef vector<int> vi_t;
  26.     typedef vector<vi_t> vv_t;
  27.     typedef vector<Coord> vC_t;
  28.     vv_t mat; // 0-clear/put 1-wall/ff 3-monster
  29.     vC_t pos; // start places
  30.     int cnt;
  31.     bool escaped;
  32.     Coord start; // start pos
  33.  
  34.     int get_nr(char x)
  35.     {
  36.         switch (x) {
  37.             case '.': return 0;
  38.             case '#': return 1;
  39.             case 'F': return 1; // not used
  40.             case '^': return 2;
  41.         }
  42.         return -1;
  43.     }
  44.  
  45.     bool on_margin(const Coord& tmp)
  46.     {
  47.         if (tmp.l == 0 || tmp.l == mat.size() - 1 || tmp.c == 0 || tmp.c == mat[0].size() - 1) return true;
  48.         return false;
  49.     }
  50.  
  51.     void escape(int l, int c)
  52.     {
  53.         if (mat[l][c] == 1 || mat[l][c] == 3 || escaped) return; // blocked, already explored or escaped
  54.         if (on_margin(Coord(l, c))) {
  55.             escaped = true;
  56.             return;
  57.         }
  58.         mat[l][c] = 3; // mark place
  59.         escape(l + 1, c);
  60.         escape(l - 1, c);
  61.         escape(l, c + 1);
  62.         escape(l, c - 1);
  63.         mat[l][c] = 0; // availability purpose
  64.     }
  65.  
  66.     void test()
  67.     {
  68.         /// Show `mat` at a certain state.
  69.         for (int i = 0; i < mat.size(); ++i) {
  70.             for (int j = 0; j < mat[0].size(); ++j) cout << mat[i][j];
  71.             cout << endl;
  72.         }
  73.     }
  74.  
  75.     void back(int force) /* N^7 (the impossible worst) */
  76.     {
  77.         if (force == cnt) return;
  78.         escaped = false;
  79.         escape(start.l, start.c);
  80.         if (!escaped) {
  81.             cnt = force;
  82.             return;
  83.         }
  84.         for (int i = 0; i < mat.size() && force + 1 < cnt; ++i) {
  85.             // pick a force field path
  86.             vi_t section; // backup path
  87.             if (i != start.l) { // Ox
  88.                 section = mat[i]; // copy last path
  89.                 mat[i] = vi_t(mat[0].size(), 1); // fill path
  90.                 //if (force == 0 && i == 3) test();
  91.                 back(force + 1);
  92.                 mat[i] = section;
  93.             }
  94.         }
  95.         for (int i = 0; i < mat[0].size() && force + 1 < cnt; ++i) {
  96.             // pick a force field path
  97.             vi_t section; // backup path
  98.             if (i != start.c) { // Oy
  99.                 section.clear(); // same shit here
  100.                 for (int j = 0; j < mat.size(); ++j) {
  101.                     section.push_back(mat[j][i]);
  102.                     mat[j][i] = 1;
  103.                 }
  104.                 back(force + 1);
  105.                 for (int j = 0; j < mat.size(); ++j) {
  106.                     mat[j][i] = section[j];
  107.                 }
  108.             }
  109.         }
  110.     }
  111.  
  112.     int capture(vs_t lab)
  113.     {
  114.         cnt = 4; // init
  115.         // make int mat
  116.         mat.resize(lab.size());
  117.         for (int i = 0; i < mat.size(); ++i) {
  118.             mat[i].resize(lab[0].size());
  119.             for (int j = 0; j < mat[0].size(); ++j) {
  120.                 mat[i][j] = get_nr(lab[i][j]);
  121.                 // save possible places
  122.                 if (mat[i][j] == 2) {
  123.                     pos.push_back(Coord(i, j));
  124.                     mat[i][j] = 0;
  125.                 }
  126.             }
  127.         }
  128.         // get rid of margins
  129.         vC_t tmp; // temp vec
  130.         for (int i = 0; i < pos.size(); ++i) {
  131.             if (!on_margin(pos[i])) tmp.push_back(pos[i]);
  132.         }
  133.         pos = tmp;
  134.         if (!pos.size()) return -1; // impossible
  135.         for (int i = 0; i < pos.size(); ++i) {
  136.             start = pos[i];
  137.             back(0);
  138.         }
  139.         return cnt;
  140.     }
  141. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement