Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ID: bradyawn
- PROG: maze1
- LANG: C++11
- */
- #include <iostream>
- #include <algorithm>
- #include <iomanip>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <cmath>
- #include <map>
- #include <utility>
- #include <algorithm>
- #include <set>
- #include <ctime>
- #include <queue>
- #include <stack>
- //#define inf cin
- //#define outf cout
- using namespace std;
- vector<string> pict;
- int main()
- {
- ifstream inf("maze1.in");
- ofstream outf("maze1.out");
- //cout << setprecision(10);
- int w, h;
- inf >> w >> h;
- string take;
- getline(inf, take); //move down a line
- string ch;
- for (int i = 0; i < 2*h+1; i++)
- {
- getline(inf, ch);
- pict.push_back(ch);
- }
- for (int i = 0; i < 2*h+1; i++)
- {
- outf << pict[i] << endl;
- }
- //Actual Solution
- vector<pair<int,int>> exit;
- //find exit
- for (int i = 0; i < 2*h+1; i++)
- {
- if (pict[i][0] == ' ')
- {
- exit.push_back({i, 1});
- }
- if (pict[i][2*w] == ' ')
- {
- exit.push_back({i, 2*w-1});
- }
- }
- for (int i = 0; i < 2*w+1; i++)
- {
- if (pict[0][i] == ' ')
- {
- exit.push_back({1, i});
- }
- if (pict[2*h][i] == ' ')
- {
- exit.push_back({2*h-1, i});
- }
- }
- //big to small, [i][j] --> [(i-1)/2][(j-1)/2]
- vector<vector<bool>> vis(h, vector<bool>(w));
- vector<vector<int>> maze(h, vector<int>(w));
- queue<pair<int,int>> bfs;
- //FIRST EXIT
- maze[(exit[0].first-1)/2][(exit[0].second-1)/2] = 1;
- bfs.push(exit[0]);
- while (!bfs.empty())
- {
- pair<int,int> node = bfs.front();
- bfs.pop();
- int startRow, startCol;
- startRow = node.first;
- startCol = node.second;
- vis[(startRow-1)/2][(startCol-1)/2] = true;
- //North
- if (pict[startRow-1][startCol] == ' ')
- {
- pair<int,int> next = {startRow-2, startCol};
- if (next.first > 0 && !vis[(next.first-1)/2][(next.second-1)/2])
- {
- bfs.push(next);
- maze[(next.first-1)/2][(next.second-1)/2] = maze[(startRow-1)/2][(startCol-1)/2] + 1;
- }
- }
- //South
- if (pict[startRow+1][startCol] == ' ')
- {
- pair<int,int> next = {startRow+2, startCol};
- if (next.first < 2*h && !vis[(next.first-1)/2][(next.second-1)/2])
- {
- bfs.push(next);
- maze[(next.first-1)/2][(next.second-1)/2] = maze[(startRow-1)/2][(startCol-1)/2] + 1;
- }
- }
- //East
- if (pict[startRow][startCol+1] == ' ')
- {
- pair<int,int> next = {startRow, startCol+2};
- if (next.second < 2*w && !vis[(next.first-1)/2][(next.second-1)/2])
- {
- bfs.push(next);
- maze[(next.first-1)/2][(next.second-1)/2] = maze[(startRow-1)/2][(startCol-1)/2] + 1;
- }
- }
- //West
- if (pict[startRow][startCol-1] == ' ')
- {
- pair<int,int> next = {startRow, startCol-2};
- if (next.second > 0 && !vis[(next.first-1)/2][(next.second-1)/2])
- {
- bfs.push(next);
- maze[(next.first-1)/2][(next.second-1)/2] = maze[(startRow-1)/2][(startCol-1)/2] + 1;
- }
- }
- }
- // SECOND EXIT
- maze[(exit[1].first-1)/2][(exit[1].second-1)/2] = 1;
- bfs.push(exit[1]);
- for (int i = 0; i < h; i++)
- for (int j = 0; j < w; j++) vis[i][j] = 0;
- while (!bfs.empty())
- {
- pair<int,int> node = bfs.front();
- bfs.pop();
- int startRow, startCol;
- startRow = node.first;
- startCol = node.second;
- vis[(startRow-1)/2][(startCol-1)/2] = true;
- //North
- if (pict[startRow-1][startCol] == ' ')
- {
- pair<int,int> next = {startRow-2, startCol};
- if (next.first > 0 && !vis[(next.first-1)/2][(next.second-1)/2])
- {
- int prev = maze[(next.first-1)/2][(next.second-1)/2];
- 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);
- if (prev != maze[(next.first-1)/2][(next.second-1)/2]) bfs.push(next);
- }
- }
- //South
- if (pict[startRow+1][startCol] == ' ')
- {
- pair<int,int> next = {startRow+2, startCol};
- if (next.first < 2*h && !vis[(next.first-1)/2][(next.second-1)/2])
- {
- int prev = maze[(next.first-1)/2][(next.second-1)/2];
- 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);
- if (prev != maze[(next.first-1)/2][(next.second-1)/2]) bfs.push(next);
- }
- }
- //East
- if (pict[startRow][startCol+1] == ' ')
- {
- pair<int,int> next = {startRow, startCol+2};
- if (next.second < 2*w && !vis[(next.first-1)/2][(next.second-1)/2])
- {
- int prev = maze[(next.first-1)/2][(next.second-1)/2];
- 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);
- if (prev != maze[(next.first-1)/2][(next.second-1)/2]) bfs.push(next);
- }
- }
- //West
- if (pict[startRow][startCol-1] == ' ')
- {
- pair<int,int> next = {startRow, startCol-2};
- if (next.second > 0 && !vis[(next.first-1)/2][(next.second-1)/2])
- {
- int prev = maze[(next.first-1)/2][(next.second-1)/2];
- 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);
- if (prev != maze[(next.first-1)/2][(next.second-1)/2]) bfs.push(next);
- }
- }
- }
- int ret = 0;
- for (int i = 0; i < h; i++)
- for (int j = 0; j < w; j++) ret = max(ret, maze[i][j]);
- outf << ret << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment