Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2014
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <sstream>
  5. #include <string>
  6. using namespace std;
  7.  
  8. long long R, C;
  9. vector<vector<char> > maze;
  10. vector<vector<long long> > memoization;
  11. vector<pair<long long, long long> > visited;
  12.  
  13. long long findPath(long long x, long long y);
  14. long long min(long long a, long long b);
  15. void tracepath();
  16. void dispmaze();
  17.  
  18. int main(int argc, char** argv) {
  19.     long long startx, starty;
  20.     string s;
  21.     getline(cin, s);
  22.     stringstream ss(s);
  23.     ss >> R >> C;
  24.     //read input
  25.     for (long long i = 0; i < R; i++) {
  26.         vector<char> mazetmp;
  27.         vector<long long> memoitmp;
  28.         getline(cin, s);
  29.         for (long long j = 0; j < C; j++) {
  30.             mazetmp.push_back(s[j]);
  31.             memoitmp.push_back(-1);
  32.             if (s[j] == 'S') {
  33.                 startx = i;
  34.                 starty = j;
  35.             }
  36.         }
  37.         maze.push_back(mazetmp);
  38.         memoization.push_back(memoitmp);
  39.     }
  40.     //recurse
  41.     long long ans = findPath(startx, starty);
  42.     maze[startx][starty] = 'S';
  43.     //display answer
  44.     if (ans <= 0) {
  45.         cout << "No Path" << "\n";
  46.     } else {
  47.         dispmaze();
  48.     }
  49. }
  50.  
  51. long long findPath(long long x, long long y) {
  52.     if (x < 0 || y < 0) return -1000000;
  53.     if (x >= R || y >= C) return -1000000;
  54.     if (maze[x][y] == '#') return -1000000;
  55.     if (maze[x][y] == 'E') return 0;
  56.     if (memoization[x][y] != -1) {
  57.         visited.push_back(pair<long long, long long>(x, y));
  58.         return memoization[x][y];
  59.     }
  60.     maze[x][y] = '#';
  61.     long long up, down, left, right;
  62.     up = findPath(x - 1, y) + 1;
  63.     down = findPath(x + 1, y) + 1;
  64.     left = findPath(x, y - 1) + 1;
  65.     right = findPath(x, y + 1) + 1;
  66.     maze[x][y] = ' ';
  67.     long long ans = min(min(up, down), min(left, right));
  68.     memoization[x][y] = ans;
  69.     if (ans > 0) visited.push_back(pair<long long, long long>(x, y));
  70.     return ans;
  71. }
  72.  
  73. long long min(long long a, long long b) {
  74.     if (a <= 0) return b;
  75.     if (b <= 0) return a;
  76.     if (a < b) return a;
  77.     return b;
  78. }
  79.  
  80. void dispmaze() {
  81.     tracepath();
  82.     for (long long i = 0; i < R; i++) {
  83.         for (long long j = 0; j < C; j++) {
  84.             cout << maze[i][j];
  85.         }
  86.         cout << "\n";
  87.     }
  88. }
  89.  
  90. void tracepath() {
  91.     for (long long i = 0; i < visited.size() - 1; i++) {
  92.         maze[visited[i].first][visited[i].second] = '*';
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement