Guest User

Untitled

a guest
Oct 23rd, 2017
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.65 KB | None | 0 0
  1. /*
  2.  ID: bradyawn
  3.  PROG: maze1
  4.  LANG: C++11
  5.  */
  6.  
  7. #include <iostream>
  8. #include <algorithm>
  9. #include <iomanip>
  10. #include <fstream>
  11. #include <vector>
  12. #include <string>
  13. #include <cmath>
  14. #include <map>
  15. #include <utility>
  16. #include <algorithm>
  17. #include <set>
  18. #include <ctime>
  19. #include <queue>
  20. #include <stack>
  21.  
  22. //#define inf cin
  23. //#define outf cout
  24.  
  25. using namespace std;
  26.  
  27. vector<string> pict;
  28.  
  29. int main()
  30. {
  31.     ifstream inf("maze1.in");
  32.     ofstream outf("maze1.out");
  33.     //cout << setprecision(10);
  34.    
  35.     int w, h;
  36.     inf >> w >> h;
  37.    
  38.     string take;
  39.     getline(inf, take); //move down a line
  40.    
  41.    
  42.     string ch;
  43.    
  44.     for (int i = 0; i < 2*h+1; i++)
  45.     {
  46.         getline(inf, ch);
  47.         pict.push_back(ch);
  48.        
  49.     }
  50.    
  51.     for (int i = 0; i < 2*h+1; i++)
  52.     {
  53.         outf << pict[i] << endl;
  54.     }
  55.    
  56.  
  57.     //Actual Solution
  58.    
  59.     vector<pair<int,int>> exit;
  60.    
  61.     //find exit
  62.    
  63.     for (int i = 0; i < 2*h+1; i++)
  64.     {
  65.         if (pict[i][0] == ' ')
  66.         {
  67.             exit.push_back({i, 1});
  68.         }
  69.         if (pict[i][2*w] == ' ')
  70.         {
  71.             exit.push_back({i, 2*w-1});
  72.         }
  73.     }
  74.     for (int i = 0; i < 2*w+1; i++)
  75.     {
  76.         if (pict[0][i] == ' ')
  77.         {
  78.             exit.push_back({1, i});
  79.         }
  80.         if (pict[2*h][i] == ' ')
  81.         {
  82.             exit.push_back({2*h-1, i});
  83.         }
  84.     }
  85.    
  86.    
  87.     //big to small, [i][j] --> [(i-1)/2][(j-1)/2]
  88.    
  89.     vector<vector<bool>> vis(h, vector<bool>(w));
  90.     vector<vector<int>> maze(h, vector<int>(w));
  91.    
  92.    
  93.    
  94.     queue<pair<int,int>> bfs;
  95.    
  96.    
  97.     //FIRST EXIT
  98.    
  99.     maze[(exit[0].first-1)/2][(exit[0].second-1)/2] = 1;
  100.     bfs.push(exit[0]);
  101.    
  102.    
  103.    
  104.     while (!bfs.empty())
  105.     {
  106.         pair<int,int> node = bfs.front();
  107.        
  108.         bfs.pop();
  109.        
  110.         int startRow, startCol;
  111.         startRow = node.first;
  112.         startCol = node.second;
  113.        
  114.         vis[(startRow-1)/2][(startCol-1)/2] = true;
  115.        
  116.         //North
  117.         if (pict[startRow-1][startCol] == ' ')
  118.         {
  119.             pair<int,int> next = {startRow-2, startCol};
  120.             if (next.first > 0 && !vis[(next.first-1)/2][(next.second-1)/2])
  121.             {
  122.                 bfs.push(next);
  123.                 maze[(next.first-1)/2][(next.second-1)/2] = maze[(startRow-1)/2][(startCol-1)/2] + 1;
  124.             }
  125.         }
  126.         //South
  127.         if (pict[startRow+1][startCol] == ' ')
  128.         {
  129.             pair<int,int> next = {startRow+2, startCol};
  130.             if (next.first < 2*h && !vis[(next.first-1)/2][(next.second-1)/2])
  131.             {
  132.                 bfs.push(next);
  133.                 maze[(next.first-1)/2][(next.second-1)/2] = maze[(startRow-1)/2][(startCol-1)/2] + 1;
  134.                
  135.             }
  136.         }
  137.         //East
  138.         if (pict[startRow][startCol+1] == ' ')
  139.         {
  140.             pair<int,int> next = {startRow, startCol+2};
  141.             if (next.second < 2*w && !vis[(next.first-1)/2][(next.second-1)/2])
  142.             {
  143.                 bfs.push(next);
  144.                 maze[(next.first-1)/2][(next.second-1)/2] = maze[(startRow-1)/2][(startCol-1)/2] + 1;
  145.                
  146.             }
  147.            
  148.         }
  149.         //West
  150.         if (pict[startRow][startCol-1] == ' ')
  151.         {
  152.             pair<int,int> next = {startRow, startCol-2};
  153.             if (next.second > 0 && !vis[(next.first-1)/2][(next.second-1)/2])
  154.             {
  155.                 bfs.push(next);
  156.                 maze[(next.first-1)/2][(next.second-1)/2] = maze[(startRow-1)/2][(startCol-1)/2] + 1;
  157.             }
  158.         }
  159.        
  160.        
  161.     }
  162.    
  163.    
  164.     // SECOND EXIT
  165.    
  166.    
  167.     maze[(exit[1].first-1)/2][(exit[1].second-1)/2] = 1;
  168.    
  169.     bfs.push(exit[1]);
  170.    
  171.     for (int i = 0; i < h; i++)
  172.         for (int j = 0; j < w; j++) vis[i][j] = 0;
  173.    
  174.     while (!bfs.empty())
  175.     {
  176.         pair<int,int> node = bfs.front();
  177.        
  178.         bfs.pop();
  179.        
  180.         int startRow, startCol;
  181.         startRow = node.first;
  182.         startCol = node.second;
  183.        
  184.         vis[(startRow-1)/2][(startCol-1)/2] = true;
  185.        
  186.         //North
  187.         if (pict[startRow-1][startCol] == ' ')
  188.         {
  189.             pair<int,int> next = {startRow-2, startCol};
  190.             if (next.first > 0 && !vis[(next.first-1)/2][(next.second-1)/2])
  191.             {
  192.                 int prev = maze[(next.first-1)/2][(next.second-1)/2];
  193.                 maze[(next.first-1)/2][(next.second-1)/2] = min(maze[(next.first-1)/2][(next.second-1)/2], maze[(startRow-1)/2][(startCol-1)/2] + 1);
  194.                 if (prev !=  maze[(next.first-1)/2][(next.second-1)/2]) bfs.push(next);
  195.                
  196.             }
  197.         }
  198.         //South
  199.         if (pict[startRow+1][startCol] == ' ')
  200.         {
  201.             pair<int,int> next = {startRow+2, startCol};
  202.             if (next.first < 2*h && !vis[(next.first-1)/2][(next.second-1)/2])
  203.             {
  204.                 int prev = maze[(next.first-1)/2][(next.second-1)/2];
  205.                 maze[(next.first-1)/2][(next.second-1)/2] = min(maze[(next.first-1)/2][(next.second-1)/2], maze[(startRow-1)/2][(startCol-1)/2] + 1);
  206.                 if (prev !=  maze[(next.first-1)/2][(next.second-1)/2]) bfs.push(next);
  207.                
  208.             }
  209.         }
  210.         //East
  211.         if (pict[startRow][startCol+1] == ' ')
  212.         {
  213.             pair<int,int> next = {startRow, startCol+2};
  214.             if (next.second < 2*w && !vis[(next.first-1)/2][(next.second-1)/2])
  215.             {
  216.                 int prev = maze[(next.first-1)/2][(next.second-1)/2];
  217.                 maze[(next.first-1)/2][(next.second-1)/2] = min(maze[(next.first-1)/2][(next.second-1)/2], maze[(startRow-1)/2][(startCol-1)/2] + 1);
  218.                 if (prev !=  maze[(next.first-1)/2][(next.second-1)/2]) bfs.push(next);
  219.                
  220.             }
  221.            
  222.         }
  223.         //West
  224.         if (pict[startRow][startCol-1] == ' ')
  225.         {
  226.             pair<int,int> next = {startRow, startCol-2};
  227.             if (next.second > 0 && !vis[(next.first-1)/2][(next.second-1)/2])
  228.             {
  229.                 int prev = maze[(next.first-1)/2][(next.second-1)/2];
  230.                 maze[(next.first-1)/2][(next.second-1)/2] = min(maze[(next.first-1)/2][(next.second-1)/2], maze[(startRow-1)/2][(startCol-1)/2] + 1);
  231.                 if (prev !=  maze[(next.first-1)/2][(next.second-1)/2]) bfs.push(next);
  232.                
  233.             }
  234.         }
  235.        
  236.        
  237.     }
  238.    
  239.     int ret = 0;
  240.    
  241.     for (int i = 0; i < h; i++)
  242.         for (int j = 0; j < w; j++) ret = max(ret, maze[i][j]);
  243.    
  244.     outf << ret << endl;
  245.    
  246.     return 0;
  247.    
  248. }
Add Comment
Please, Sign In to add comment