Advertisement
dmkozyrev

399.cpp

Apr 12th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <vector>
  3. #include <queue>
  4. #include <algorithm>
  5.  
  6. const int INF = (int)1e9;
  7.  
  8. struct Solver {
  9.     int nRows;
  10.     int nCols;
  11.     int map[128][128];
  12.    
  13.     Solver(int nRows = 0, int nCols = 0) : nRows(nRows), nCols(nCols) {
  14.         for (int r = 0; r < nRows; ++r) {
  15.             map[r][0] = map[r][nCols-1] = INF;
  16.         }
  17.        
  18.         for (int c = 0; c < nCols; ++c) {
  19.             map[0][c] = map[nRows-1][c] = INF;
  20.         }
  21.        
  22.         for (int r = 1; r <= nRows-2; ++r) {
  23.             for (int c = 1; c <= nCols-2; ++c) {
  24.                 map[r][c] = 0;
  25.             }
  26.         }
  27.     }    
  28.    
  29.     bool isset_path() {
  30.         std::queue<std::pair<int, int>> q;
  31.         std::vector<std::pair<int, int>> s;
  32.         q.push({1, 1});
  33.         map[1][1] = 1;
  34.         while (!q.empty()) {
  35.             auto curr = q.front(); q.pop(); s.push_back(curr);
  36.             int r = curr.first, c = curr.second;
  37.             if (map[r+1][c] == 0) {
  38.                 map[r+1][c] = 1;
  39.                 q.push({r+1, c});
  40.             }
  41.             if (map[r-1][c] == 0) {
  42.                 map[r-1][c] = 1;
  43.                 q.push({r-1, c});
  44.             }
  45.             if (map[r][c+1] == 0) {
  46.                 map[r][c+1] = 1;
  47.                 q.push({r, c+1});
  48.             }
  49.             if (map[r][c-1] == 0) {
  50.                 map[r][c-1] = 1;
  51.                 q.push({r, c-1});
  52.             }
  53.         }
  54.         bool result = map[nRows-2][nCols-2];
  55.         while (!s.empty()) {
  56.             int r = s.back().first, c = s.back().second;
  57.             map[r][c] = 0;
  58.             s.pop_back();
  59.         }
  60.         return result;
  61.     }
  62.    
  63.     int count() {
  64.         if (!isset_path()) {
  65.             return -1;
  66.         }
  67.         int r = 1, c = 1, dir = 0, answ = 0;
  68.         const int dr[4] = { 1, 0, -1,  0};
  69.         const int dc[4] = { 0, 1,  0, -1};
  70.         do {
  71.             map[r][c]++;
  72.             int min = std::min({map[r-1][c], map[r+1][c], map[r][c+1], map[r][c-1]});
  73.             if (map[r+dr[dir]][c+dc[dir]] != min) {
  74.                 for (int next = 0; next < 4; ++next) {
  75.                     if (map[r+dr[next]][c+dc[next]] == min) {
  76.                         dir = next;
  77.                         break;
  78.                     }
  79.                 }
  80.             }
  81.             ++answ;
  82.             r += dr[dir];
  83.             c += dc[dir];
  84.         } while (!(r == nRows-2 && c == nCols-2));
  85.         return answ;
  86.     }
  87.    
  88.     static Solver read() {
  89.         int nRows, nCols;
  90.         scanf("%d %d\n", &nRows, &nCols);
  91.        
  92.         Solver solver(nRows, nCols);
  93.         for (int r = 0; r < nRows; ++r) {
  94.             char buf[101];
  95.             scanf("%[^\n]s", buf); scanf("\n");
  96.             for (int c = 0; c < nCols; ++c) {
  97.                 solver.map[r][c] = (buf[c] == '@') ? INF : 0;
  98.             }
  99.         }
  100.         return solver;
  101.     }
  102. };
  103.  
  104. int main() {
  105.     freopen("input.txt", "rt", stdin);
  106.     auto solver = Solver::read();
  107.     printf("%d", solver.count());
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement