Advertisement
dmkozyrev

399.cpp

Apr 12th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 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_v = count(), max_r = -1, max_c = -1;
  111.         //printf("max = %d\n", max_v);
  112.         bool success = false;
  113.         for (int r = 1; r <= nRows-2; ++r) {
  114.             for (int c = 1; c <= nCols-2; ++c) {
  115.                 if (!((r == 1 && c == 1) || (r == nRows-2 && c == nCols-2))) {
  116.                     // Ïîïðîáóåì ïîìåíÿòü:
  117.                     map[r][c] = INF - map[r][c];
  118.                     // Ïîïðîáóåì ðåøèòü:
  119.                     int temp = count();
  120.                     //printf("temp = %d\n", temp);
  121.                     // Âåðíåì:
  122.                     map[r][c] = INF - map[r][c];
  123.                     // Ïðîâåðèì, íå îáíîâèëñÿ ëè òåêóùèé ìàêñèìóì:
  124.                     if (max_v <= temp) {
  125.                         max_v = temp;
  126.                         max_r = r;
  127.                         max_c = c;
  128.                         success = true;
  129.                     }
  130.                 }
  131.             }
  132.         }
  133.         if (success) {
  134.             map[max_r][max_c] = INF - map[max_r][max_c];
  135.             lastAnswer = max_v;
  136.         }
  137.         return success;
  138.     }
  139.    
  140.     void print(std::string filename) {
  141.         auto file = fopen(filename.c_str(), "wt");
  142.         fprintf(file, "%d %d\n", nRows, nCols);
  143.         for (int r = 0; r < nRows; ++r) {
  144.             for (int c = 0; c < nCols; ++c) {
  145.                 fprintf(file, "%c", map[r][c] == INF ? '@' : ' ');
  146.             }
  147.             fprintf(file, "\n");
  148.         }
  149.         fprintf(file, "answ = %d", lastAnswer);
  150.         fclose(file);
  151.     }
  152.    
  153.     void learning() {
  154.         uint64_t t = clock(); // 1000
  155.         int i = 0;
  156.         while (next_generation()) {
  157.             auto dt = clock() - t;
  158.             if (dt >= CLOCKS_PER_SEC) {
  159.                 char buf[101];
  160.                 sprintf(buf, "test%04d.txt", i++);
  161.                 print(buf);
  162.                 printf("%s: %9d\n", currentDateTime().c_str(), lastAnswer);
  163.                 fflush(stdout);
  164.                 t += dt;
  165.             }
  166.         }
  167.         char buf[101];
  168.         sprintf(buf, "test%04d.txt", i++);
  169.         print(buf);
  170.         printf("%s: %9d\n", currentDateTime().c_str(), lastAnswer);
  171.         fflush(stdout);
  172.     }
  173.    
  174.     static Solver read() {
  175.         int nRows, nCols;
  176.         scanf("%d %d\n", &nRows, &nCols);
  177.        
  178.         Solver solver(nRows, nCols);
  179.        
  180.         for (int r = 0; r < nRows; ++r) {
  181.             char buf[101];
  182.             scanf("%[^\n]s", buf); scanf("\n");
  183.             for (int c = 0; c < nCols; ++c) {
  184.                 solver.map[r][c] = (buf[c] == '@') ? INF : 0;
  185.             }
  186.         }
  187.         return solver;
  188.     }
  189. };
  190.  
  191. int main() {
  192.     //freopen("output.txt", "wt", stdout);
  193.     Solver solver(21, 31);
  194.     solver.learning();
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement