Advertisement
dmkozyrev

399.cpp

Apr 12th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <vector>
  3. #include <queue>
  4. #include <algorithm>
  5. #include <ctime>
  6.  
  7. const int INF = (int)1e9;
  8.  
  9. // Get current date/time, format is YYYY-MM-DD.HH:mm:ss
  10. const std::string currentDateTime() {
  11.     time_t     now = time(0);
  12.     struct tm  tstruct;
  13.     char       buf[80];
  14.     tstruct = *localtime(&now);
  15.     // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
  16.     // for more information about date/time format
  17.     strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
  18.     std:string s(buf);
  19.     if (s.back() == '\n') s.pop_back();
  20.     return s;
  21. }
  22.  
  23. struct Solver {
  24.     int nRows;
  25.     int nCols;
  26.     int map[128][128];
  27.     int lastAnswer;
  28.    
  29.     Solver(int nRows = 0, int nCols = 0) : nRows(nRows), nCols(nCols) {
  30.         for (int r = 0; r < nRows; ++r) {
  31.             map[r][0] = map[r][nCols-1] = INF;
  32.         }
  33.        
  34.         for (int c = 0; c < nCols; ++c) {
  35.             map[0][c] = map[nRows-1][c] = INF;
  36.         }
  37.        
  38.         for (int r = 1; r <= nRows-2; ++r) {
  39.             for (int c = 1; c <= nCols-2; ++c) {
  40.                 map[r][c] = 0;
  41.             }
  42.         }
  43.     }    
  44.    
  45.     bool isset_path() {
  46.         std::queue<std::pair<int, int>> q;
  47.         std::vector<std::pair<int, int>> s;
  48.         q.push({1, 1});
  49.         map[1][1] = 1;
  50.         while (!q.empty()) {
  51.             auto curr = q.front(); q.pop(); s.push_back(curr);
  52.             int r = curr.first, c = curr.second;
  53.             if (map[r+1][c] == 0) {
  54.                 map[r+1][c] = 1;
  55.                 q.push({r+1, c});
  56.             }
  57.             if (map[r-1][c] == 0) {
  58.                 map[r-1][c] = 1;
  59.                 q.push({r-1, c});
  60.             }
  61.             if (map[r][c+1] == 0) {
  62.                 map[r][c+1] = 1;
  63.                 q.push({r, c+1});
  64.             }
  65.             if (map[r][c-1] == 0) {
  66.                 map[r][c-1] = 1;
  67.                 q.push({r, c-1});
  68.             }
  69.         }
  70.         bool result = map[nRows-2][nCols-2];
  71.         backup();
  72.         return result;
  73.     }
  74.    
  75.     void backup() {
  76.         for (int r = 0; r < nRows; ++r) {
  77.             for (int c = 0; c < nCols; ++c) {
  78.                 map[r][c] = (map[r][c] == INF) ? INF : 0;
  79.             }
  80.         }
  81.     }
  82.    
  83.     int count() {
  84.         if (!isset_path()) {
  85.             return -1;
  86.         }
  87.         int r = 1, c = 1, dir = 0, answ = 0;
  88.         const int dr[4] = { 1, 0, -1,  0};
  89.         const int dc[4] = { 0, 1,  0, -1};
  90.         do {
  91.             map[r][c]++;
  92.             int min = std::min({map[r-1][c], map[r+1][c], map[r][c+1], map[r][c-1]});
  93.             if (map[r+dr[dir]][c+dc[dir]] != min) {
  94.                 for (int next = 0; next < 4; ++next) {
  95.                     if (map[r+dr[next]][c+dc[next]] == min) {
  96.                         dir = next;
  97.                         break;
  98.                     }
  99.                 }
  100.             }
  101.             ++answ;
  102.             r += dr[dir];
  103.             c += dc[dir];
  104.         } while (!(r == nRows-2 && c == nCols-2));
  105.         backup();
  106.         return lastAnswer = answ;
  107.     }
  108.    
  109.     bool next_generation() {
  110.         int max = count();
  111.         bool success = false;
  112.         for (int r = 1; r <= nRows-2; ++r) {
  113.             for (int c = 1; c <= nCols-2; ++c) {
  114.                 if (!((r == 1 && c == 1) || (r == nRows-2 && c == nCols-2))) {
  115.                     // Попробуем поменять:
  116.                     map[r][c] = INF - map[r][c];
  117.                     // Попробуем решить:
  118.                     int temp = count();
  119.                     // Проверим, не обновился ли текущий максимум:
  120.                     if (max < temp) {
  121.                         max = temp;
  122.                         success = true;
  123.                     } else { // Если не помогло, то вернем обратно:
  124.                         map[r][c] = INF - map[r][c];
  125.                     }
  126.                 }
  127.             }
  128.         }
  129.         return success;
  130.     }
  131.    
  132.     void print(std::string filename) {
  133.         auto file = fopen(filename.c_str(), "wt");
  134.         fprintf(file, "%d %d\n", nRows, nCols);
  135.         for (int r = 0; r < nRows; ++r) {
  136.             for (int c = 0; c < nCols; ++c) {
  137.                 fprintf(file, "%c", map[r][c] == INF ? '@' : ' ');
  138.             }
  139.             fprintf(file, "\n");
  140.         }
  141.         fprintf(file, "answ = %d", answer);
  142.         flose(file);
  143.     }
  144.    
  145.     void learning() {
  146.         uint64_t t = clock(); // 1000
  147.         int i = 0;
  148.         while (next_generation()) {
  149.             auto dt = clock() - t;
  150.             if (dt >= CLOCKS_PER_SEC) {
  151.                 char buf[101];
  152.                 sprintf(buf, "test%04d.txt", i++);
  153.                 print(buf);
  154.                 std::cout << currentDateTime() << ": " << lastAnswer << endl;
  155.                 t += dt;
  156.             }
  157.         }
  158.     }
  159.    
  160.     static Solver read() {
  161.         int nRows, nCols;
  162.         scanf("%d %d\n", &nRows, &nCols);
  163.        
  164.         Solver solver(nRows, nCols);
  165.        
  166.         for (int r = 0; r < nRows; ++r) {
  167.             char buf[101];
  168.             scanf("%[^\n]s", buf); scanf("\n");
  169.             for (int c = 0; c < nCols; ++c) {
  170.                 solver.map[r][c] = (buf[c] == '@') ? INF : 0;
  171.             }
  172.         }
  173.         return solver;
  174.     }
  175. };
  176.  
  177. int main() {
  178.     Solver solver(21, 31);
  179.     solver.learning();
  180.     return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement