Advertisement
STANAANDREY

labirint

Feb 11th, 2020
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream fin("text.in");
  4. ofstream fout("text.out");
  5. ///*****************************
  6. #define NMAX 205
  7.  
  8. char arr[NMAX][NMAX];
  9. int n, m;
  10. int startI, startJ;
  11. int dis[NMAX][NMAX];
  12. int stopI, stopJ;
  13.  
  14. const short di[] = {-1, 0, 1, 0}, dj[] = {0, 1, 0, -1}, nd = 4;
  15.  
  16. inline void mark()
  17. {
  18.     for (int i = 1; i <= n; i++)
  19.         for (int  j = 1; j <= m; j++)
  20.             dis[i][j] = INT_MAX;
  21. }
  22.  
  23. inline void read()
  24. {
  25.     fin >> n >> m;
  26.     for (int i = 1; i <= n; i++)
  27.         for (int j = 1; j <= m; j++)
  28.             fin >> arr[i][j];
  29.  
  30.     fin >> startI >> startJ;
  31. }
  32.  
  33. inline bool atBound(int i, int j)
  34. {
  35.     return ((i == 1) || (j == 1) || (i == n) || (j == m));
  36. }
  37.  
  38. inline bool isInside(int i, int j)
  39. {
  40.     return (i >= 1 && i <= n && j >= 1 && j <= m);
  41. }
  42.  
  43. inline void Lee(int i, int j)
  44. {
  45.     queue<pair<int, int> > q;
  46.     q.push(make_pair(i, j));
  47.     dis[i][j] = 0;
  48.  
  49.     while (!q.empty())
  50.     {
  51.         i = q.front().first;
  52.         j = q.front().second;
  53.         q.pop();
  54.  
  55.         for (int dir = 0; dir < nd; dir++)
  56.         {
  57.             int nextI = i + di[dir];
  58.             int nextJ = j + dj[dir];
  59.             if (dis[nextI][nextJ] == INT_MAX && arr[nextI][nextJ] != 'O')
  60.             {
  61.                 q.push(make_pair(nextI, nextJ));
  62.                 dis[nextI][nextJ] = dis[i][j] + 1;
  63.                 if (atBound(nextI, nextJ))
  64.                 {
  65.                     stopI = nextI;
  66.                     stopJ = nextJ;
  67.                     return;
  68.                 }
  69.             }
  70.         }
  71.     }
  72. }
  73.  
  74. inline void getAns(int i, int j)
  75. {
  76.     int nextI, nextJ;
  77.     stack<char> ans;
  78.     while (dis[i][j])
  79.     {
  80.         for (int dir = 0; dir < nd; dir++)
  81.         {
  82.             int posNextI = i + di[dir];
  83.             int posNextJ = j + dj[dir];
  84.  
  85.             if (!isInside(posNextI, posNextJ))
  86.                 continue;
  87.  
  88.             if (dis[posNextI][posNextJ] == dis[i][j] - 1)
  89.             {
  90.                 nextI = posNextI;
  91.                 nextJ = posNextJ;
  92.             }
  93.         }
  94.  
  95.         if (nextI == i - 1)
  96.             ans.push('S');
  97.         else if (nextI == i + 1)
  98.             ans.push('N');
  99.         else if (nextJ == j + 1)
  100.             ans.push('V');
  101.         else if (nextJ == j - 1)
  102.             ans.push('E');
  103.  
  104.         //cerr<<i<<' '<<j<<endl;
  105.  
  106.         i = nextI;
  107.         j = nextJ;
  108.     }
  109.  
  110.     fout << ans.size() << '\n';
  111.     while (!ans.empty())
  112.     {
  113.         fout << ans.top();
  114.         ans.pop();
  115.     }//*/
  116. }
  117.  
  118. int main()
  119. {
  120.     read();
  121.     mark();
  122.     Lee(startI, startJ);//cerr<<stopI<<stopJ<<endl;
  123.     //for (int i=1;i<=n;i++){for (int j=1;j<=m;j++)cerr<<dis[i][j]<<' ';cerr<<endl;}
  124.     getAns(stopI, stopJ);
  125.     //fout.close();
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement