Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin("text.in");
- ofstream fout("text.out");
- ///*****************************
- #define NMAX 205
- char arr[NMAX][NMAX];
- int n, m;
- int startI, startJ;
- int dis[NMAX][NMAX];
- int stopI, stopJ;
- const short di[] = {-1, 0, 1, 0}, dj[] = {0, 1, 0, -1}, nd = 4;
- inline void mark()
- {
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= m; j++)
- dis[i][j] = INT_MAX;
- }
- inline void read()
- {
- fin >> n >> m;
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= m; j++)
- fin >> arr[i][j];
- fin >> startI >> startJ;
- }
- inline bool atBound(int i, int j)
- {
- return ((i == 1) || (j == 1) || (i == n) || (j == m));
- }
- inline bool isInside(int i, int j)
- {
- return (i >= 1 && i <= n && j >= 1 && j <= m);
- }
- inline void Lee(int i, int j)
- {
- queue<pair<int, int> > q;
- q.push(make_pair(i, j));
- dis[i][j] = 0;
- while (!q.empty())
- {
- i = q.front().first;
- j = q.front().second;
- q.pop();
- for (int dir = 0; dir < nd; dir++)
- {
- int nextI = i + di[dir];
- int nextJ = j + dj[dir];
- if (dis[nextI][nextJ] == INT_MAX && arr[nextI][nextJ] != 'O')
- {
- q.push(make_pair(nextI, nextJ));
- dis[nextI][nextJ] = dis[i][j] + 1;
- if (atBound(nextI, nextJ))
- {
- stopI = nextI;
- stopJ = nextJ;
- return;
- }
- }
- }
- }
- }
- inline void getAns(int i, int j)
- {
- int nextI, nextJ;
- stack<char> ans;
- while (dis[i][j])
- {
- for (int dir = 0; dir < nd; dir++)
- {
- int posNextI = i + di[dir];
- int posNextJ = j + dj[dir];
- if (!isInside(posNextI, posNextJ))
- continue;
- if (dis[posNextI][posNextJ] == dis[i][j] - 1)
- {
- nextI = posNextI;
- nextJ = posNextJ;
- }
- }
- if (nextI == i - 1)
- ans.push('S');
- else if (nextI == i + 1)
- ans.push('N');
- else if (nextJ == j + 1)
- ans.push('V');
- else if (nextJ == j - 1)
- ans.push('E');
- //cerr<<i<<' '<<j<<endl;
- i = nextI;
- j = nextJ;
- }
- fout << ans.size() << '\n';
- while (!ans.empty())
- {
- fout << ans.top();
- ans.pop();
- }//*/
- }
- int main()
- {
- read();
- mark();
- Lee(startI, startJ);//cerr<<stopI<<stopJ<<endl;
- //for (int i=1;i<=n;i++){for (int j=1;j<=m;j++)cerr<<dis[i][j]<<' ';cerr<<endl;}
- getAns(stopI, stopJ);
- //fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement